You are on page 1of 30

Name – Bhushan Ramesh Jadhav

Batch- 5 Group -1
Reg no. – 201020051
Date- 3|03|2021

CPPS PRACTICAL 3
Topic – To study different control structures
PART 1
Aim : Write a c++ program to check whether the entered number is
even or odd. Use if else statement.

Theory :
if else – It is a control structure of selection/ branching type.
In an if statement, condition is a value or an expression that is
used to determine which code block is executed, and the curly
braces acts as “begin” and “end” markers.
Syntax –
if(condition)
{ //code to be executed if condition is true.
}
Else
{ //code to be executed if condition is false.
}
Program:
#include<iostream>
using namespace std;
int main()
{
int num,rem;
cout<<"enter the number";
cin>>num;
rem=num%2;
if (rem==0)
{
cout<<num<<" :is an even integer"<<endl;
}
else
{
cout<<num<<" :is an odd integer"<<endl;
}
return 0;
}
Output:

Algorithm:

1. Start
2. Taking an integer input from user
3. Calculating the modulus value of the no.
4. Printing even is modulus is 0 , printing odd if modulus is 1
5. End
Flowchart: Start

Input number

Rem=num%2

rem=0?

yes no
Print even Print odd

End
Conclusion:

From the above program we can conclude how to check condition in c++
program. We can check the condition using if else statement . Here we can
check condition only once. If the condition is true, then statement 1 will be
executed otherwise statement 2 will executed.
PART 2:
Aim : Write a c++ program using nested if to find the maximum of three entered
numbers.

Theory : nested if else - In nested if else we can embed any number of if


statements within outer if statement. This is called nested if.

Diagram-

Test False False block of statements


expression

True

True block of statements Statements


Program:
#include<iostream>
using namespace std;
int main()
{
int num1,num2,num3;
cout<<" Enter 1st number:"<<endl;
cin>>num1;
cout<<" Enter 2nd number:"<<endl;
cin>>num2;
cout<<" Enter 3rd number:"<<endl;
cin>>num3;
if(num1>=num2)
{
if(num1>=num3)
{
cout<<num1<<"is largest number.";
}
else{
cout<<num3<<"is largest number.";
}
}
else{
if(num2>=num3)
{
cout<<num2<<"is largest number.";
}
else
{
cout<<num3<<"is largest number.";
}
}
return 0;
}

Output:
Algorithm:-

1] Start
2] Declare variables num1,num2,num3.
3] Take integer input for the variables num1,num2,num3.
4] If num1>num2 and num1>num3 ,
display num1 is the largest number.
Else display num3 is the largest number.
Else if num2>num3 ,
display num2 is the largest number.
Else display num3 is the largest number.
5] End.

Flowchart:
Start

Input num1,
num2,num3

No Is yes
num1>num
2

Is no no Is
num2>nu num1>nu
m3? m3?

Print num2 Print num3


Print num1
is the is the largest
is the largest
largest no.

End
Conclusion:
If we have to check more than one condition , then we have to use nested if else control
structure. In this we put second condition within first condition ,if 1st condition is true
then we will enter into second condition if second condition is also true then code written
in sec condition will execute otherwise code written in else will execute. If 1st condition is
false then code written in else will execute.
PART 3-
Aim :
Write a c++ program to check whether given character is vowel, consonant , integer
or special character using else – if ladder.

Theory :
Multiple if else – Multiple if else is normally used in making multipath
decisions. In this all conditions are tested and the respective block is evaluated,
corresponding satisfaction of a condition. If none of the condition returns true ,the
else part is evaluated.

Syntax:
if(condition 1)
Statement 1;
else if(condition 2)
statement 2;
else if(condition 3)
statement 3;
….
else if(condition n)
Statement n;
Else
default statement;
statement (…) ;

Program:

#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character: ";
cin>>ch;
if(((ch>='a')||(ch>='A'))&&((ch<='z')||(ch<='Z')))
{
if((ch=='a')||(ch=='A')||(ch=='e')||(ch=='E')|| (ch=='I')||(ch=='i')||(ch=='o')||
(ch=='O')||(ch=='u')||(ch=='U' ))
{
cout<<ch<<" is a vowel“<<endl;
}
else
{
cout<<ch<<" is a consonant"<<endl;
}
}
else if((ch>='0') && (ch<='9'))
{
cout<<ch<<" is a number"<<endl;
}
else
{
cout<<ch<<" is a special character"<<endl;
}
return 0;
}
Output:

Algorithm –

1] Start
2] Declare character type variable ch.
3] Take input in ch from user
4] Check if ch is a vowel , if yes then print vowel otherwise print consonant.
5] Check if ch is an integer if yes print integer otherwise print special
character
6] End.
start

Input ch

If
(ch>=a),
(ch>A)
&(ch<=z),
(ch<=Z)

If
(ch>=0), If (ch==a),(ch==A),
(ch<=9) (ch==e),(ch==E),
(ch=i),(ch==I)
no yes (ch==o),(ch==O),
(ch==u),(ch==U)

Print ch is Print ch
special is an
charecter integer
Print ch is a Print ch
consonant is vowel

End
Conclusion :
From above program I got to know “==” is a arithmetic operator used as equal to, “&&”
is logical and, “” are relational operators used to compare .In this program we have used
multiple if else statements. Also we have declared variable using character data type.
PART 4
Aim : Write a c++ program to accept marks of five subjects for a student. Calculate the
total percentage of marks and , also decide grade of students depending on percentage
using if-else ladder.

Program:

#include<iostream>
using namespace std;
int main()
{
int a1,a2,a3,a4,a5,total;
float per;
cout<<"enter marks of 5 subjects -";
cin>>a1>>a2>>a3>>a4>>a5;
total= a1+a2+a3+a4+a5;
cout<<" Totak marks obtained ="<<total<<endl;
per= (total/ 5.0);
cout<<"Percentage is :"<<per<<endl;
if(per>=80)
{
cout<<"Your grade is A";
}
else if(per>=60&&per<80)
{
cout<<"Your grade is B";
}
else if(per>=40&&per<60)
{
cout<<"Your grade is C";
}
Else
{
cout<<"Your grade is D";
}
return 0;
}

Output:
Algorithm:

1] Start
2] Declare a1,a2,a3,a4,a5,total,per
3] Take input for values of a1,a2,a3,a4,a5 from user
4] total= a1+a2+a3+a4+a5
5] per= total/5
6] Print total and percentage
7] According to the percentage allot grades to the students.
8] End
start

Input a1,a2,a3,a4,a5

Total=a1+a2+a3+a4+a5

Per=total/5.0

If yes Print
per>=80 grade A
no
If yes
per>=60& Print
& per<80 grade B
no
If yes
per>=40& Print
&per<60 grade c
no
Print grade
d

Print per and total

End
Conclusion :

In the above program using if-else ladder and by accepting marks of students for 5
subjects we calculated sum and percentage and then gave grades according to the
percentage.
PART 5
Aim :
Write c++ program for a menu driven calculator using switch case.

Theory :
Switch statement – It allows us to execute a block of code amongst many
alternatives. The expression is evaluated once and compared with values of each case
label. If there is no match them default label is executed.

Syntax: –
switch (expression)
{
case value 1:
//block of code;
break;
case value 2:
//block of code;
break;
case value N:
//block of code;
break;
default
//block of code;
break;
}

Program:

#include<iostream>
using namespace std;
int main()
{
char op;
int num1,num2;
cout<<"/////MENU/////"<<endl;
cout<<"ADDITION"<<endl;
cout<<"SUBTRACTION"<<endl;
cout<<"MULTIPLICATION"<<endl;
cout<<"DIVISION"<<endl;
cout<<" Enter operator either + or - or * or /";
cin>>op;
cout<<"Enter two operands";
cin>>num1>>num2;
switch (op)
{
case '+':
cout<<num1+num2;
break;
case '-‘:
cout<<num1-num2;
break;
case '*':
cout<<num1*num2;
break;
case '/':
cout<<num1/num2;
break;
default :
cout<<"Error! Operator is not correct";
break;
}
return 0;
}
Output:

Algorithm
1]Start
2] Declare char op, num1,num2;
3] Input op
4] Input num1 , num2
5] switch(op)
6] if case '+':
then print 'num1+num2‘
break;
if case '-':
then print 'num1-num2‘
break;
if case '*':
then print 'num1*num2‘
break;
if case '/':
then print 'num1/num2‘
break;
Else
default case:

print 'Error ! operator not used ‘

7] Stop

Flowchart:
Start

Input op ,
num1 ,
num2

Switch op

Case Print
‘+’ num1+nu
m2

Case Print
‘-’ num1-
num2
Case Print
‘*’ num1*num
2
Case Print
‘/’ num1/num2

default Print error

end
Conclusion:
In this we used switch statement . In this program,we took character input and two
integer input . Then we asked the user which arithmetical operations he had to do on
these two numbers. Likewise according to the users demand that operations are done
on numbers and outputs are obtained. If characters other than +,-,×,/ are given by the
user then the default statement gets executed.

You might also like