Sunteți pe pagina 1din 25

Conditional statements

Activity 2
# include <iostream>
using namespace std;
int main()
{
int num1,num2;
cout<<"enter the number"<<endl;
cin>>num1;
cout<<"enter the number"<<endl;
cin>>num2;
if(num1>=num2)
{
cout<<"num1 is larger "<<endl;
}
else
{
cout<<"num2 is larger"<<endl;
}
system("pause");
return 0;
}

Activity 6
# include <iostream>
using namespace std;
int main()
{
int num;
cout<<"these are your five favourtes drink press 1-5 to choose any one of
these"<<endl;
cout<<"\n 1.sprite\n 2.dew\n 3.string\n 4.water\n 5.coke"<<endl;
cin>>num;
if(num==1)
{
cout<<"sprite"<<endl;
}
else if(num==2)
{
cout<<"dew"<<endl;
}
else if(num==3)
{
cout<<"string"<<endl;
}
else if(num==4)
{
cout<<"water"<<endl;
}
else if(num==5)
{
cout<<"coke"<<endl;
}
system("pause");
return 0;
}

Activity 7
# include <iostream>
using namespace std;
int main()
{
int num;
cout<<"there are seven days in a week"<<endl;
cout<<"press 1-7 to choose your best day of the week"<<endl;
cout<<"\n 1.Sunday\n 2.Monday\n 3.Tuesday\n 4.Wednesday\n 5.Thursday\n 6.Friday\n
7.Saturday"<<endl;
cin>>num;
if(num==1)
{
cout<<"Day is Sunday"<<endl;
}
else if(num==2)
{
cout<<"Day is Monday"<<endl;
}
else if(num==3)
{
cout<<"Day is Tuesday"<<endl;
}
else if(num==4)
{
cout<<"Day is Wednesday"<<endl;
}
else if(num==5)
{
cout<<"Day is Thursday"<<endl;
}
else if(num==6)
{
cout<<"Day is Friday"<<endl;
}
else if(num==7)
{
cout<<"Day is Saturday"<<endl;
}
system("pause");
return 0;
}

Activity 8
# include <iostream>
using namespace std;
int main()
{
int m;
cout<<"Plaese enter the value of m"<<endl;
cin>>m;
if(m<0)
{
cout<<"n=-1"<<endl;
}
else if(m==0)
{
cout<<"n=0"<<endl;
}
else if(m>0)
{
cout<<"n=1"<<endl;
}
system("pause");
return 0;
}

Activity 9
# include <iostream>
using namespace std;
int main()
{
int age;
int vote_age=18;
cout<<"Please enter your age"<<endl;
cin>>age;
if(age>=vote_age)
{
cout<<"You are eligible for vote"<<endl;
}
else
{
cout<<"Sorry,you are not eligible for vote"<<endl;
int rem_years=vote_age-age;
cout<<"You will be eligible for vote after "<<rem_years<<"years"<<endl;
}
system("pause");
return 0;
}

Question 1:
Write a program that asks user to enter a value and displays whether that value is Even
or Odd.
# include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Please enter the num"<<endl;
cin>>num;
if (num%2==0)
{
cout<<"The number is EVEN"<<endl;
}
else
{
cout<<"The number is odd"<<endl;
}
system("pause");
return 0;
}

Question 2:
The area of a rectangle is the rectangle’s length times its width. Write a program that asks
for the length and width of two rectangles. The program should tell the user which
rectangle has the greater area, or if the areas are the same.

# include <iostream>
using namespace std;
int main()
{
int len1,width1,area1;
cout<<"Enter the length and width of the First Rectangle"<<endl;
cout<<"Enter the length"<<endl;
cin>>len1;
cout<<"Enter the width"<<endl;
cin>>width1;
area1=len1*width1;
cout<<"Area of the First Rectangle is"<<area1<<endl<<endl;
int len2,width2,area2;
cout<<"Enter the length and width of the Second Rectangle"<<endl;
cout<<"Enter the length"<<endl;
cin>>len2;
cout<<"Enter the width"<<endl;
cin>>width2;
area2=len2*width2;
cout<<"Area of the Second Rectangle is"<<area2<<endl<<endl;
if(area1==area2)
{
cout<<"BOTH Areas are Same"<<endl;
}
else if (area1>area2)
{
cout<<"Area of First rectangle is large"<<endl;
}
else
{
cout<<"Area of second rectangle is large"<<endl;
}

system("pause");
return 0;
}
Question 3:
Write a program to input a single character and print a message “It is vowel” if it is a
vowel. Otherwise print message “It is a consonant”. Use if-else structure and OR (||)
operator only.

# include <iostream>
using namespace std;

int main()
{
char alpha;

cout<<"Enter any alphabet and I will tell you either it is constant or


vowel"<<endl;
cin>>alpha;
if ((alpha=='a')||(alpha=='e')||(alpha=='i')||(alpha=='o')||(alpha=='u'))
{
cout<<"This alphabet is vowel"<<endl;
}
else
cout<<"consonet"<<endl;

system("pause");
return 0;
}

Question 4:
A school conducts a 100 mark exam for its student and grades them as follows:
Grade:

Grade A: Marks>75
Grade B: 75>Marks>60
Grade C: 60>Marks>50
Grade D: 50>Marks>40

Write a program to calculate the grades for a student, using if. Else statement. User will
enter marks and program will output corresponding grade.

# include <iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter the marks of the student for his grade"<<endl;
cin>>marks;
if(marks>=75)
{
cout<<"Grade A"<<endl;
}
else if(marks>=60)
{
cout<<"Grade B"<<endl;
}
else if(marks>=50)
{
cout<<"Grade C"<<endl;
}
else if(marks>=40)
{
cout<<"Grade D"<<endl;
}

system("pause");
return 0;
}

Question 5:
The colors red, blue, and yellow are known as the primary colors because they cannot be
made by mixing other colors. When you mix two primary colors, you get a secondary
color, as shown here:
When you mix red and blue, you get purple.
When you mix red and yellow, you get orange.
When you mix blue and yellow, you get green.
Write a program that prompts the user to enter the names of two primary colors to mix.
If the user enters anything other than “red,” “blue,” or “yellow,” the program should
display an error message. Otherwise, the program should display the name of the
secondary color that results by mixing two primary colors.
# include <iostream>
# include <string>
using namespace std;
int main()
{
string a,b;
cout<<"Enter the names of two colours"<<endl;
cin>>a;
cin>>b;
if(a=="red"&&b=="blue"||b=="red"&&a=="blue")
{
cout<<"The secondary colour is "<<"PURPLE"<<endl;
}
else if(a=="red"&&b=="yellow"||a=="yellow"&&b=="red")
{
cout<<"The secondary colour is "<<"ORANGE"<<endl;
}
else if(a=="blue"&&b=="yellow"||a=="yellow"&&b=="blue")
{
cout<<"The secondary colour is "<<"GREEN"<<endl;
}
else
cout<<"THE COLOUR YOU WRITE IS NOT IN THE GIVEN LIST"<<endl;
system("pause");
return 0;
}
Question 6:
Write a program that asks for the names of three runners and the time it took each of
them to finish a race. The program should display who came in first, second, and third
place.
# include <iostream>
# include <string>
using namespace std;
int main()
{
string a,b,c;
float t1,t2,t3;
cout<<"Enter the names of three runnners\n"<<endl;
cin>>a>>b>>c;
cout<<"\nEnter time of three runner in seconds"<<endl;
cout<<"t1,t2,t3"<<endl;
cin>>t1>>t2>>t3;
if(t1<0||t2<0||t3<0)
{
cout<<"time should be positive"<<endl;
}
else if(t1>t2&&t2>t3)
{
cout<<"The first is"<<a<<endl;
cout<<"The second is"<<b<<endl;
cout<<"The third is"<<c<<endl;
}
else if(t1>t2&&t3>t2&&t1>t3)
{
cout<<"The first is"<<a<<endl;
cout<<"The second is"<<c<<endl;
cout<<"The third is"<<b<<endl;
}
else if(t2>t1&&t2>t3&&t1>t3)
{
cout<<"The first is"<<b<<endl;
cout<<"The second is"<<a<<endl;
cout<<"The third is"<<c<<endl;
}
else if(t2>t1&&t2>t3&&t3>t1)
{
cout<<"The first is"<<b<<endl;
cout<<"The second is"<<c<<endl;
cout<<"The third is"<<a<<endl;
}
else if(t3>t1&&t3>t2&&t1>t2)
{
cout<<"The first is"<<c<<endl;
cout<<"The second is"<<a<<endl;
cout<<"The third is"<<b<<endl;
}
else if(t3>t1&&t3>t2&&t2>t1)
{
cout<<"The first is"<<c<<endl;
cout<<"The second is"<<b<<endl;
cout<<"The third is"<<a<<endl;
}
system("pause");
return 0;
}

3. Magic Dates
The date June 10, 1960, is special because when we write it in the following format, the
month times the day equals the year. 6/10/60 Write a program that asks the user to
enter a month (in numeric form), a day, and a twodigit year. The program should then
determine whether the month times the day is equal to the year. If so, it should display
a message saying the date is magic. Otherwise, it should display a message saying the
date is not magic.

# include<iostream>
using namespace std;
int main()
{
int month,day,year;
cout<<"Please enter the month in numeric form"<<endl;
cin>>month;
cout<<"Please enter the day in numeric form"<<endl;
cin>>day;
cout<<"Please write a two digit year"<<endl;
cin>>year;

if(month*day==year)
{
cout<<"The date is magic"<<endl;
cout<<month<<"/" <<day<<"/"<<year<<endl;
}
else
{
cout<<"The date is not magic"<<endl;
cout<<month<<"/" <<day<<"/"<<year<<endl;
}
system("pause");
return 0;
}
5. Book Club Points
An online book club awards points to its customers based on the number of books purchased each
month. Points are awarded as follows:
Books Purchased Points Earned
0 0
1 5
2 15
3 30
4or more 60
Write a program that asks the user to enter the number of books purchased this month and then
displays the number of points awarded.

# include <iostream>
using namespace std;
int main()
{
int books;
cout<<"Please enter the number of books you purchased in this month"<<endl;
cin>>books;
if(books==0)
{
cout<<"YOU EARNED 0 POINTS"<<endl;
}
else if(books==1)
{
cout<<"YOU EARNED 5 POINTS"<<endl;
}
else if(books==2)
{
cout<<"YOU EARNED 15 POINTS"<<endl;
}
else if(books==3)
{
cout<<"YOU EARNED 30 POINTS"<<endl;
}
else if(books>=4)
{
cout<<"YOU EARNED 60 POINTS"<<endl;
}

system("pause");
return 0;
}

7. Time Calculator
Write a program that asks the user to enter a number of seconds.
• There are 86400 seconds in a day. If the number of seconds entered by the user is greater than or
equal to 86400, the program should display the number of days in that many seconds.
• There are 3600 seconds in an hour. If the number of seconds entered by the user is less than 86400,
but is greater than or equal to 3600, the program should display the number of hours in that many
seconds.
• There are 60 seconds in a minute. If the number of seconds entered by the user is less than 3600, but
is greater than or equal to 60, the program should display the number of minutes in that many seconds.

# include<iostream>
using namespace std;
int main()
{
double seconds,days,hours,minutes;
cout<<"Enter the seconds"<<endl;
cin>>seconds;
days=seconds*1/86400;
if(seconds>=86400)
{
cout<<"The days in that seconds are "<<days<<"days"<<endl;
}
hours=seconds*1/3600;
if(seconds<86400&&seconds>=3600)
{
cout<<"The hours in that seconds are "<<hours<<"hours"<<endl;
}
minutes=seconds*1/60;
if(seconds<3600&&seconds>=60)
{
cout<<"The minutes in that seconds are "<<minutes<<"minutes"<<endl;
}
system("pause");
return 0;
}

8. Math Tutor
This is a modification of the math tutor problem in Chapter 3. Write a program that can be
used as a math tutor for a young student. The program should display two random numbers
between 10 and 50 that are to be added, such as:

24
+ 12
——
The program should then wait for the student to enter the answer. If the answer is
correct, a message of congratulations should be printed. If the answer is incorrect, a
message should be printed showing the correct answer.

# include <iostream>
# include<cstdlib>
# include <ctime>
using namespace std;
int main()
{
int num1,num2;
int sum;
unsigned seed;
seed=time(0);
srand(seed);
cout<<"Enter the sum of two numbers "<<endl;
num1=10+rand()%50;
num2=10+rand()%50;

cout<<" "<<num1<<endl;
cout<<"+"<<num2<<endl;
cout<<"..........."<<endl;
cin>>sum;
if(sum==num1+num2)
{
cout<<"CONGRATULATIONS! Your answer is correct "<<endl;
}
else
{
cout<<"The correct answer is "<<num1+num2<<endl;
}
system("pause");
return 0;
}
9. Software Sales
A software company sells a package that retails for $99. Quantity discounts are given
according to the following table.
Write a program that asks for the number of units purchased and computes the total cost
of the purchase. Input Validation: Make sure the number of units is greater than 0.

# include <iostream>
using namespace std;
int main()
{
const float price=99;
float units,total_cost,discount;
cout<<"Enter the units you buy"<<endl;
cin>>units;
if(units<0)
{
cout<<"Please input positive integer"<<endl;
}
if(units>=10&&units<20)
{
discount=price*0.2;
total_cost=price-discount;
cout<<"The total price is "<<total_cost<<endl;
}
else if(units>=20&&units<50)
{
discount=price*0.3;
total_cost=price-discount;
cout<<"The total price is "<<total_cost<<endl;
}
else if(units>=50&&units<100)
{
discount=price*0.4;
total_cost=price-discount;
cout<<"The total price is "<<total_cost<<endl;
}
else if(units>=100)
{
discount=price*0.5;
total_cost=price-discount;
cout<<"The total price is "<<total_cost<<endl;
}
system("pause");
return 0;
}

10. Bank Charges


A bank charges $10 per month plus the following check fees for a commercial checking account:
$.10 each for fewer than 20 checks
$.08 each for 20–39 checks
$.06 each for 40–59 checks
$.04 each for 60 or more checks
Write a program that asks for the number of checks written during the past month, then computes and
displays the bank’s fees for the month. Input Validation: Do not accept a negative value for the number
of checks written.

# include <iostream>
using namespace std;
int main()
{
int checks,total_charges;
const int tax=10;
cout<<"Enter the number of checks you written this month"<<endl;
cin>>checks;
if(checks<0)
{
cout<<"Please input positive integer"<<endl;
}
else if(checks<20)
{
total_charges=tax+(checks*10);
cout<<"The total charges of the bank is "<<total_charges<<endl;
}
else if(checks>=20&&checks<40)
{
total_charges=tax+(checks*8);
cout<<"The total charges of the bank is "<<total_charges<<endl;
}
else if(checks>=40&&checks<60)
{
total_charges=tax+(checks*6);
cout<<"The total charges of the bank is "<<total_charges<<endl;
}
else if(checks>=60)
{
total_charges=tax+(checks*4);
cout<<"The total charges of the bank is "<<total_charges<<endl;
}
system("pause");
return 0;
}

11. Geometry Calculator


Write a program that displays the following menu:
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
If the user enters 1, the program should ask for the radius of the circle and then
display its area. Use 3.14159 for π. If the user enters 2, the program should ask for the
length and width of the rectangle, and then display the rectangle’s area. If the user
enters 3, the program should ask for the length of the triangle’s base and its height,
and then display its area. If the user enters 4, the program should end. Input
Validation: Display an error message if the user enters a number outside the range of 1
through 4 when selecting an item from the menu. Do not accept negative values for the
circle’s radius, the rectangle’s length or width, or the triangle’s base or height.

# include <iostream>
using namespace std;
int main()
{
int length,width,radius,base,height,value;
const float pi=3.14;
float area;
cout<<"Geometric Areas\n"<<endl;
cout<<"Press 1 to calculate the area of the circle"<<endl;
cout<<"Press 2 to calculate the area of the rectangle"<<endl;
cout<<"Press 3 to calculate the area of the triangle"<<endl;
cout<<"press 4 to quit"<<endl;
cout<<"Enter 1-4 to get the desired area"<<endl;
cin>>value;
if(value<1||value>4)
{
cout<<"YOU cannot go out of the given range "<<endl;
}
else if(value==1)
{
cout<<"To get the area of the circle\n";
cout<<"Enter radius of the circle"<<endl;
cin>>radius;
if(radius<0)
{
cout<<"Please enter positive integer"<<endl;
}
else
{
area=pi*radius*radius;
cout<<"The area of the circle is "<<area<<endl;
}
}
if(value==2)
{
cout<<"To get the area of the rectangle\n";
cout<<"Enter length"<<endl;
cin>>length;
cout<<"Enter width"<<endl;
cin>>width;
if(length<0||width<0)
{
cout<<"Please enter positive integer"<<endl;
}
else
{
area=length*width;
cout<<"The area of the rectangle is "<<area<<endl;
}
}
if(value==3)
{
cout<<"To get the area of the triangle\n";
cout<<"Enter base"<<endl;
cin>>base;
cout<<"Enter height"<<endl;
cin>>height;
if(base<0||height<0)
{
cout<<"Please enter positive integer"<<endl;
}
else
{area=0.5*base*height;
cout<<"The area of the triangle is "<<area<<endl;
}
}
if(value==4)
{
cout<<"QUIT"<<endl;
}
system("pause");
return 0;
}

By switch
# include <iostream>
using namespace std;
int main()
{
int length,width,radius,base,height,value;
const float pi=3.14;
float area;
cout<<"Geometric Areas\n"<<endl;
cout<<"Press 1 to calculate the area of the circle"<<endl;
cout<<"Press 2 to calculate the area of the rectangle"<<endl;
cout<<"Press 3 to calculate the area of the triangle"<<endl;
cout<<"press 4 to quit"<<endl;
cout<<"Enter 1-4 to get the desired area"<<endl;
cin>>value;
if(value>=1&&value<=4)
{
switch(value)
{
case 1:
{
cout<<"To get the area of the circle\n";
cout<<"Enter radius of the circle"<<endl;
cin>>radius;
if(radius<0)
{
cout<<"Please enter positive integer"<<endl;
}
else
{
area=pi*radius*radius;
cout<<"The area of the circle is "<<area<<endl;
}
break;
}
case 2:
{
cout<<"To get the area of the rectangle\n";
cout<<"Enter length"<<endl;
cin>>length;
cout<<"Enter width"<<endl;
cin>>width;
if(length<0||width<0)
{
cout<<"Please enter positive integer"<<endl;
}
else
{
area=length*width;
cout<<"The area of the rectangle is "<<area<<endl;
}
break;
}
case 3:
{
cout<<"To get the area of the triangle\n";
cout<<"Enter base"<<endl;
cin>>base;
cout<<"Enter height"<<endl;
cin>>height;
if(base<0||height<0)
{
cout<<"Please enter positive integer"<<endl;
}
else
{area=0.5*base*height;
cout<<"The area of the triangle is "<<area<<endl;
}
break;
}
case 4:
{
cout<<"QUIT"<<endl;
}
break;
}
}
else
{
cout<<"Enter the number from given range"<<endl;
}
system("pause");
return 0;
}

22. Using Files—Internet Service Provider Part 1


An Internet service provider has three different subscription packages for its
customers: Package A: For $9.95 per month 10 hours of access are provided.
Additional hours are $2.00 per hour. Package B: For $14.95 per month 20 hours of
access are provided. Additional hours are $1.00 per hour. Package C: For $19.95
per month unlimited access is provided. Write a program that calculates a
customer’s monthly bill. It should input customer name, which package the
customer has purchased, and how many hours were used. It should then create a
bill that includes the input information and the total amount due. The bill should be
written to a file. Input Validation: Be sure the user only selects package A, B, or C.
Also, the number of hours used in a month cannot exceed 744.

# include <iostream>
# include <string>
using namespace std;
int main()
{
string package,name;
float time,price;
cout<<"Please Enter you name"<<endl;
cin>>name;
cout<<"Package A: For $9.95 per month 10 hours of access are provided. Additional
hours are $2.00 per hour."<<endl;
cout<<"Package B: For $14.95 per month 20 hours of access are provided. Additional
hours are $1.00 per hour."<<endl;
cout<<"Package C: For $19.95 per month unlimited access is provided."<<endl;
cout<<"choose package"<<endl;
cin>>package;

if (package=="A"||package=="B"||package=="C")
{
cout<<"Enter the number of hours"<<endl;
cin>>time;
if(time<=744)
{
if(package=="A")
{
if(time>10)
{
price=9.95+(time-10)*2;
}
else
{
price=9.95;
}
}
else if(package=="B")
{
if(time>20)
{
price=14.95+(time-20)*1;
}
else
{
price=14.95;
}
}
else if(package=="C")
{
price=19.95;
}
cout<<"Your internet charges are "<<price<<endl;
}
else
{
cout<<"invalid time"<<endl;
}
}
else
{
cout<<"invalid package"<<endl;
}

system("pause");
return 0;
}

24-internet provider 3

# include <iostream>
# include <string>
using namespace std;
int main()
{
string name,month;
char package;
float hours,price;
cout<<"Please Enter you name"<<endl;
cin>>name;
cout<<"Package A: For $9.95 per month 10 hours of access are provided. Additional
hours are $2.00 per hour."<<endl;
cout<<"Package B: For $14.95 per month 20 hours of access are provided. Additional
hours are $1.00 per hour."<<endl;
cout<<"Package C: For $19.95 per month unlimited access is provided."<<endl;
cout<<"choose package"<<endl;
cin>>package;

switch(package)
{
case 'A':
case 'a':
{
cout<<"Enter your month name"<<endl;
cin>>month;
cout<<"Enter your number of hours "<<endl;
cin>>hours;
if(month=="feb")
{
if(hours<=672&&hours>10)
{
price=9.95+(hours-10)*2;
}
else if(hours<=672&&hours<=10)
{
price=9.95;
}
else
{
cout<<"Hours of this month cannot be greater than 672"<<endl;
}
}
else if(month=="apr"||month=="jun"||month=="sep"||month=="nov")
{
if(hours<=720&&hours>10)
{
price=9.95+(hours-10)*2;
}
else if(hours<=720&&hours<=10)
{
price=9.95;
}
else
{
cout<<"Hours of this month cannot be greater than 720"<<endl;
}
}
else
if(month=="jan"||month=="mar"||month=="may"||month=="jul"||month=="aug"||month=="oct"||mo
nth=="dec")
{
if(hours<=744&&hours>10)
{
price=9.95+(hours-10)*2;
}
else if(hours<=744&&hours<=10)
{
price=9.95;
}
else
{
cout<<"Hours of this month cannot be greater than 744"<<endl;
}
}
break;
}
case 'B':
case 'b':
{
cout<<"Enter your month name"<<endl;
cin>>month;
cout<<"Enter your number of hours "<<endl;
cin>>hours;
if(month=="feb")
{
if(hours<=672&&hours>20)
{
price=14.95+(hours-20)*1;
}
else if(hours<=672&&hours<=20)
{
price=14.95;
}
else
{
cout<<"Hours of this month cannot be greater than 672"<<endl;
}
}
else if(month=="apr"||month=="jun"||month=="sep"||month=="nov")
{
if(hours<=720&&hours>20)
{
price=9.95+(hours-20)*1;
}
else if(hours<=672&&hours<=20)
{
price=14.95;
}
else
{
cout<<"Hours of this month cannot be greater than 720"<<endl;
}
}
else
if(month=="jan"||month=="mar"||month=="may"||month=="jul"||month=="aug"||month=="oct"||mo
nth=="dec")
{
if(hours<=744&&hours>20)
{
price=9.95+(hours-20)*1;
}
else if(hours<=672&&hours<=20)
{
price=14.95;
}
else
{
cout<<"Hours of this month cannot be greater than 744"<<endl;
}
}
break;
}
case 'C':
case 'c':
{
cout<<"You have provided unlimited access for the whole month"<<endl;
price=19.95;
break;
}
}
cout<<"Your monthly charges of internet are "<<price<<"$"<<endl;
system("pause");
return 0;
}
Question 1:
Write a program to input an integer value. Test the integer value if the value is
divisible
by 2 then print the message “Divisible by 2” otherwise “Not divisible by 2” by using
switch
statement.

# include <iostream>
using namespace std;
int main()
{
int value,result;
cout<<"enter the value"<<endl;
cin>>value;
result=value%2;
switch(result)
{
case(0):cout<<"divisible by 2"<<endl;
break;
case(1):cout<<"not divisible"<<endl;
break;
}
system("pause");
return 0;
}

Question 2:
Write a program that inputs a character ch. Create 4 switch conditions. If the user
enter
+ as value of ch add two variables, If the user enter - as value of ch subtract two
variables,
If the user enter * as value of ch multiply two variables and If the user enter / as value
of
ch divide two variables

# include <iostream>
# include <cstdlib>
using namespace std;
int main()
{
int x,y;
x=rand();
y=rand();
cout<<x<<endl;
cout<<y<<endl;
char ch;
cout<<"Enter the character "<<endl;
cin>>ch;
switch(ch)
{

case '+':cout<<"sum of two numbers "<<x+y<<endl;


break;
case '-':cout<<"subtraction of two numbers "<<x-y<<endl;
break;
case'*':cout<<"multipication of two numbers "<<x*y<<endl;
break;
case'/':cout<<"division of two numbers "<<x/y<<endl;
break;
default:cout<<"Please enter an operator"<<endl;
}

system("pause");
return 0;
}

Question 3:
Write a program in C++ to input a single digit from 0 to 9 and print the input value in
words. For example if the input value is 0, then print “zero”. Use switch statement.

# include <iostream>
using namespace std;
int main()
{
int input;
cout<<"Enter the value from 0 to 9"<<endl;
cin>>input;
switch(input)
{
case 0:cout<<"You enter zero"<<endl;
break;
case 1:cout<<"You enter one"<<endl;
break;
case 2:cout<<"You enter two"<<endl;
break;
case 3:cout<<"You enter three"<<endl;
break;
case 4:cout<<"You enter four"<<endl;
break;
case 5:cout<<"You enter five"<<endl;
break;
case 6:cout<<"You enter six"<<endl;
break;
case 7:cout<<"You enter seven"<<endl;
break;
case 8:cout<<"You enter eight"<<endl;
break;
case 9:cout<<"You enter nine"<<endl;
break;
default:cout<<"Please enter the number from given range"<<endl;
}
system("pause");
return 0;
}

Question 4:
Write a program that displays the day of the week depending on the value of an integer.
Integer will represent the number of the day e.g. Monday is equal to the first day of the
week and so on,

# include <iostream>
using namespace std;
int main()
{
int input;
cout<<"Enter the value from 1 to 7"<<endl;
cin>>input;
switch(input)
{
case 1:cout<<"MONDAY"<<endl;
break;
case 2:cout<<"TUESDAY"<<endl;
break;
case 3:cout<<"WEDNESDAY"<<endl;
break;
case 4:cout<<"THURSDAY"<<endl;
break;
case 5:cout<<"FRIDAY"<<endl;
break;
case 6:cout<<"SATURDAY"<<endl;
break;
case 7:cout<<"SUNDAY"<<endl;
break;
default:cout<<"Please enter the number from given range"<<endl;
}
system("pause");
return 0;
}

2. Roman Numeral Converter


Write a program that asks the user to enter a number within the range of 1 through 10.
Use a switch statement to display the Roman numeral version of that number. Input
Validation: Do not accept a number less than 1 or greater than 10.

# include <iostream>
using namespace std;
int main()
{
int input;
cout<<"Enter the value from 1 to 10"<<endl;
cin>>input;
switch(input)
{
case 1:cout<<"i"<<endl;
break;
case 2:cout<<"ii"<<endl;
break;
case 3:cout<<"iii"<<endl;
break;
case 4:cout<<"iv"<<endl;
break;
case 5:cout<<"v"<<endl;
break;
case 6:cout<<"vi"<<endl;
break;
case 7:cout<<"vii"<<endl;
break;
case 8:cout<<"viii"<<endl;
break;
case 9:cout<<"ix"<<endl;
break;
case 10:cout<<"x"<<endl;
break;
default:
cout<<"Please enter the number from given range"<<endl;
}
system("pause");
return 0;
}

1. Minimum/Maximum
Write a program that asks the user to enter two numbers. The program
should use the conditional operator to determine which number is the
smaller and which is the larger.

# include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two number "<<endl;
cin>>a>>b;
if(a>b)
{
cout<<a<<" is greater "<<endl;
cout<<b<<" is smaller"<<endl;
}
else
{
cout<<b<<" is greater "<<endl;
cout<<a<<" is smaller "<<endl;
}
system("pause");
return 0;
}

S-ar putea să vă placă și