Coding Exercises: Conditional Statements and Programming Examples, Exercises of Computer Fundamentals

Various coding exercises focused on conditional statements and programming examples using c/c++. It includes converting statements to conditional statements, writing and executing code, and creating a program that lets users enter an integer value and validates the input. Also, there is a simple menu program and a program for player selection in federal colleges.

Typology: Exercises

2011/2012

Uploaded on 07/04/2012

waniaa
waniaa 🇵🇰

7 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 DCIS-PIEAS
Computer Fundamental
Lab-05
Convert the following statements to C/C++ conditional statements (Use relational +
logical operators)
Given that A,B,C,D are all integers having some initial value (which is not know)
Example
Statement Conditional Statement
A,B,C and D are all positive numbers A>0 && B>0 && C>0 && D>0
Convert the following into conditional statements for A,B,C and D
A, B, C, and D all are negative numbers
A and B are positive numbers or C and D are negative numbers
A is even B is odd C is positive and D is negative
Any of A,B,C or D is divisible by 5
A or B is even and C is odd
A and B are greater than 20 or C and D are less than 100
Docsity.com
pf3

Partial preview of the text

Download Coding Exercises: Conditional Statements and Programming Examples and more Exercises Computer Fundamentals in PDF only on Docsity!

1 DCIS-PIEAS

[email protected]

Computer Fundamental

Lab-

Convert the following statements to C/C++ conditional statements (Use relational +
logical operators)

Given that A,B,C,D are all integers having some initial value (which is not know) Example Statement Conditional Statement A,B,C and D are all positive numbers A>0 && B>0 && C>0 && D>

Convert the following into conditional statements for A,B,C and D

A, B, C, and D all are negative numbers

A and B are positive numbers or C and D are negative numbers

A is even B is odd C is positive and D is negative

Any of A,B,C or D is divisible by 5

A or B is even and C is odd

A and B are greater than 20 or C and D are less than 100

2 DCIS-PIEAS

[email protected]

Write the following code, execute it and compare the output

Program-01 (character input) #include <stdio.h> #include <conio.h> #include <iostream.h> main() {

char ans;

cout<<”Is today your birthday? (Y/N) : “; cin>>ans;

if(ans==’y’ || ans == ‘Y’) cout<<” Happy Birthday To You “<<endl ; else cout<<”Have a nice day”<<endl; getch(); }

Program-02 (Simple while loop) #include <stdio.h> #include <conio.h> #include <iostream.h> main() {

int a=10; while(a<1){ cout<<”Value of a = “<<a; a--; }

getch(); }

Program-03 (Conditional while loop) #include <stdio.h> #include <conio.h> #include <iostream.h> main() { int a=10; while(a%2!=0){

cout<<”Enter Value of a = “; cin>>a; cout<<endl; } getch(); }

Program-

Write a program that lets user enter an integer value between 1 and 10, the program validates the input , if the value entered is between 1 and 10 the program prints the value entered otherwise the program takes the input again , the program ends on legal values otherwise keep taking input.

Sample Program Output
Enter a number between 1 and 10 --> 56
Number is outside range 1-10. Please re-
enter
Enter a number between 1 and 10 --> 6
The number is 6

Program-05 (Simple Menu program)

#include <stdio.h> #include <conio.h> #include <iostream.h> main() {

char choice,finish;

do{ cout<<”What do you want to know “<<endl; cout<<”1-My Name”<<endl; cout<<”2-My Roll no”<<endl; choice=getch(); if(choice==’1’) cout<<”My name is ---“<<endl;

if(choice == ‘2’) cout<<”My roll number is ---“<<endl;