Computer Programming Lab 4
Lab 03:
Decision Statements
Source Code:
#include <iostream>
using namespace std;
int main()
{
char X;
cout<<"X= ";
cin>>X;
if(X=='a')
{
cout<<"the input alphabet is a vowel ";
}
else if (X=='e')
{
cout<<"the input alphabet is a vowel";
}
else if(X=='i')
{
cout<<"the input alphabet is a vowel";
}
else if (X=='o')
{
cout<<"the input alphabet is a vowel";
}
else if (X=='u')
{
cout<<"the input alphabet is a vowel";
}
else
{
cout<<"the input alphabet is not a vowel";
}
return 0;
}
Result:
Source Code:
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"integer: ";
cin>>x;
if (x%2==0)
{
cout<<"the number is even";
}
else
{
cout<<"the number is odd";
}
return 0;
}
Example: Input is 4… Print “April”
Source Code:
#include <iostream>
using namespace std;
int main()
{
int month;
cout<<"month= ";
cin>>month;
if (month==1)
{
cout<<"January";
}
else if(month==2)
{
cout<<"February";
}
else if(month==3)
{
cout<<"March";
}
else if(month==4)
{
cout<<"April";
}
else if(month==5)
{
cout<<"May";
}
else if(month==6)
{
cout<<"June";
}
else if(month==7)
{
cout<<"july";
}
else if(month==8)
{
cout<<"August";
}
else if(month==9)
{
cout<<"September";
}
else if(month==10)
{
cout<<"October";
}
else if(month==11)
{
cout<<"November";
}
else if(month==12)
{
cout<<"December";
}
else
{
cout<<"Your are out of world";
}
return 0;
}
Source Code:
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"enter Month: ";
cin>>x;
switch(x)
{
case 1:
cout<<"january";
break;
case 2:
cout<<"february";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"Novcember";
break;
case 12:
cout<<"Decmber";
break;
default:
cout<<"You are Out of This World";
}
return 0;
}
Source Code:
#include <iostream>
using namespace std;
int main()
{
double x,y,z,Sum;
cout<<"x= ";
cin>>x;
cout<<"y= ";
cin>>y;
cout<<"z= ";
cin>>z;
Sum=x+y+z;
cout<<"Sum= "<<Sum<<endl;
if (x<y && x<z)
{
cout<<"the smallest number is "<<x<<endl;
}
else if (y<x && y<z)
{
cout<<"the smallest number is "<<y<<endl;
}
else
{
cout<<"the smallest number is "<<z<<endl;
}
if (x>y && x>z)
{
cout<<" the largest number is "<<x<<endl;
}
else if (y>z && y>x)
{
cout<<"the largest number is " <<y<<endl;
}
else
{
cout<<"the largest number is "<<z<<endl;
}
return 0;
}
Source Code:
Exercise 1:
Write a C++ Program that read
a float (amount) and an int (num) input from user. Add both (amount and num)
numbers and store them in two different int (a) and float (b) variables.
Display the outputs of a and b variables. Also if outputs are greater than 16
print “value of a & b > 16” else print “value of a & b < 16”.
Source Code:
#include <iostream>
using namespace std;
int main()
{
float amount,b;
int num,a;
cout<<"amount= ";
cin>>amount;
cout<<"num= ";
cin>>num;
a= amount+num;
b= amount+num;
cout<<"the value of a=
"<<a<<endl;
cout<< "the value of b=
"<<b<<endl;
if(a>16 && b>16)
{
cout<<"value of a & b >
16"<<endl;
}
else
{
cout<<"value of a & b <
16"<<endl;
}
return 0;
}
Result:
Exercise 2:
Write a C++ Program that read
three input from user and returns the smallest one.
Source Code:
#include <iostream>
using namespace std;
int main()
{
double
a,b,c;
cout<<"A=
";
cin>>a;
cout<<"B=
";
cin>>b;
cout<<"C=
";
cin>>c;
if (a<b
&& a<c)
{
cout<<"The
smallest no is "<<a<<endl;
}
else if (b<c && b<a)
{
cout<<"the smallest no is
"<<b<<endl;
}
else
{
cout<<"the smallest no
is"<<c<<endl;
}
return 0;
}
Result:
Exercise 3:
Write a C++ Program that read
an alphabet (e.g. a, b, c, d...z) and display whether the input alphabet is a
vowel (i.e. a, e, i, o, u) or consonant. Source Code:
#include <iostream>
using namespace std;
int main()
{
char X;
cout<<"X= ";
cin>>X;
if(X=='a')
{
cout<<"the input alphabet is a vowel ";
}
else if (X=='e')
{
cout<<"the input alphabet is a vowel";
}
else if(X=='i')
{
cout<<"the input alphabet is a vowel";
}
else if (X=='o')
{
cout<<"the input alphabet is a vowel";
}
else if (X=='u')
{
cout<<"the input alphabet is a vowel";
}
else
{
cout<<"the input alphabet is not a vowel";
}
return 0;
}
Result:
Exercise 4:
Write a C++ Program that read
a number from user .Test the user entered number whether it is EVEN or ODD.
Print the message that number is EVEN or ODD. #include <iostream>
using namespace std;
int main()
{
int x;
cout<<"integer: ";
cin>>x;
if (x%2==0)
{
cout<<"the number is even";
}
else
{
cout<<"the number is odd";
}
return 0;
}
Result:
Exercise 5:
Write a C++ Program that read
an integer input in between (1 to 12) and store it month_of_year. Print the
corresponding month of year. Use else if statement. Example: Input is 4… Print “April”
Source Code:
#include <iostream>
using namespace std;
int main()
{
int month;
cout<<"month= ";
cin>>month;
if (month==1)
{
cout<<"January";
}
else if(month==2)
{
cout<<"February";
}
else if(month==3)
{
cout<<"March";
}
else if(month==4)
{
cout<<"April";
}
else if(month==5)
{
cout<<"May";
}
else if(month==6)
{
cout<<"June";
}
else if(month==7)
{
cout<<"july";
}
else if(month==8)
{
cout<<"August";
}
else if(month==9)
{
cout<<"September";
}
else if(month==10)
{
cout<<"October";
}
else if(month==11)
{
cout<<"November";
}
else if(month==12)
{
cout<<"December";
}
else
{
cout<<"Your are out of world";
}
return 0;
}
Result:
Exercise 6:
Rewrite exercise 5 using
switch statement. Source Code:
#include <iostream>
using namespace std;
int main()
{
int x;
cout<<"enter Month: ";
cin>>x;
switch(x)
{
case 1:
cout<<"january";
break;
case 2:
cout<<"february";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"Novcember";
break;
case 12:
cout<<"Decmber";
break;
default:
cout<<"You are Out of This World";
}
return 0;
}
Result:
Exercise 7:
Write a C++ Menu driven
program that allows a user to enter 3 numbers and then choose between findings
the smallest, largest, sum or average. Use else if statement to determine what
action to take. Source Code:
#include <iostream>
using namespace std;
int main()
{
double x,y,z,Sum;
cout<<"x= ";
cin>>x;
cout<<"y= ";
cin>>y;
cout<<"z= ";
cin>>z;
Sum=x+y+z;
cout<<"Sum= "<<Sum<<endl;
if (x<y && x<z)
{
cout<<"the smallest number is "<<x<<endl;
}
else if (y<x && y<z)
{
cout<<"the smallest number is "<<y<<endl;
}
else
{
cout<<"the smallest number is "<<z<<endl;
}
if (x>y && x>z)
{
cout<<" the largest number is "<<x<<endl;
}
else if (y>z && y>x)
{
cout<<"the largest number is " <<y<<endl;
}
else
{
cout<<"the largest number is "<<z<<endl;
}
return 0;
}
Exercise 8:
Rewrite exercise 7 using
switch statement.Source Code:
#include<iostream>
using namespace std;
int main()
{
float a,b,c;
int option;
cout<<"Enter any
three values:"<<endl;
cin>>a>>b>>c;
cout<<"'a' for
Smallest: "<<endl;
cout<<"'b' for
Largest:"<<endl;
cout<<"'c' for
Sum:"<<endl;
cout<<"'d' for
Average:"<<endl;
cout<<"Enter one of
the above choices: ";
cin>>option;
switch(option)
{
case 1:
if(a<b && a<c)
{
cout<<"The smallest
num is: "<<a<<endl;
}
else if(b<a && b<c)
{
cout<<"The smallest num
is: "<<b<<endl;
}
else
cout<<"The smallest num is: "<<c<<endl;
break;
case 2:
if(a>b && a>c)
{
cout<<"The largest num is: "<<a<<endl;
}
else if(b>a && b>c)
{
cout<<"The largest num is:
"<<b<<endl;
}
else
cout<<"The largest num is: "<<c<<endl;
break;
case 3:
break;
case 4:
cout<<"Avearage of Values are:
"<<(a+b+c)/3<< endl;
break;
}
return 0;
}
Comments
Post a Comment