You are on page 1of 4

Computing For Engineers

Lab Activity 1: Control Structures 1


Objectives:

To be familiar with the control structures type: repetition (looping) and if else statement.
Learning Outcomes: At the end of the session, the students are able:-

To understand and use the control structures in programming.


To understand the concept of input output stream, while loop and if else statement.
Apparatus:

PC with C++ compiler


Sample Code1: If else
The program code shown below uses the if else to identify even and odd number.

#include <iostream> //library for io stream


#include <string> //library for string

using namespace std; //to be used with iostream

int main(){
int num;

cout<<"Enter a number:"<<endl;
cin>>num;

if(num %2==0){
cout<<"The number"<<num<<"is even"<<endl;
}
if(num %2!=0){
cout<<"The number"<<num<<"is odd"<<endl;
}

system("pause"); //freeze your output window,it will print out "press any key
to continue"

Computing For Engineers


Control Structures II
Sample Code 2: While loop
The program code below shows how to display the number in sequence using while loop.
#include <iostream> //library for io stream
#include <string> //library for string

using namespace std; //to be used with iostream

int main(){
int y=0;

while(y<=10)
{
cout<<"The no is: "<<y<<endl;
y++;
}

system("pause"); //freeze your output window,it will print out "press any key
to continue"

Sample Code 3: While Loop and If Else statement


The program code by using combination of while loop and if else statement.
#include <iostream> //library for io stream
#include <string> //library for string

using namespace std; //to be used with iostream

int main(){
int count=1;
int A,B=0;

cout<< "No" <<"\t"<< "Sum of B\t" <<"Type"<< endl; // \t for 1 tab


cout<<"============================"<<endl;
while(count<11){
if(B>0 && B<=20){
cout<< count << "\t"<< B << "\t\tTypeA"<<endl;
}
else{
cout<< count << "\t"<< B << "\t\tTypeB"<<endl;
}
B=B+count;
count++;
}
system("pause"); //freeze your output window,it will print out "press any key
to continue"

Computing For Engineers


Control Structures II
Question 1

Construct a complete C++ program from the following conditions;

.1- User must enter "start" to start the program.

2- User are able to enter any characters and integer number.

3- If user entered character between 'a' to 'g' or number between 1 to 10.789, please display
"Level 1".

4- If user entered character between 'h' to 'm' and number between 11 to 20.789, please
display "Level 2".

5- If user entered other characters or other numbers, please display "Level 3".

Question 2
By using 1 while loop, construct a C++ program to display the following output;

Computing For Engineers


Control Structures II
Question 3
By using the sample code, construct a complete C++ program to display the following data;

x y A = x+y B = x+y Type


1 10 11 11 Odd
2 9 11 22 Even
3 8 11 33 Odd
4 7 11 44 Even
5 6 11 55 Odd
6 5 11 66 Even
7 4 11 77 Odd
8 3 11 88 Even
9 2 11 99 Odd
10 1 11 110 Even

Sample of your output as per bellow figure.

Computing For Engineers


Control Structures II

You might also like