0% found this document useful (1 vote)
2K views

Programming Exercise Solutions

This document contains 11 programming exercises that demonstrate the use of conditional statements like if-else, switch case in C++. Each exercise presents a short code snippet that uses conditional logic to output different results based on the input values or conditions. The exercises cover common programming scenarios like determining if a character is lowercase, calculating salary based on employee type, finding common divisors, calculating area of shapes, mapping input to company names, movie categories etc.

Uploaded by

Sarah Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
2K views

Programming Exercise Solutions

This document contains 11 programming exercises that demonstrate the use of conditional statements like if-else, switch case in C++. Each exercise presents a short code snippet that uses conditional logic to output different results based on the input values or conditions. The exercises cover common programming scenarios like determining if a character is lowercase, calculating salary based on employee type, finding common divisors, calculating area of shapes, mapping input to company names, movie categories etc.

Uploaded by

Sarah Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Exercise Solutions

Chapter 5
1.
#include <iostream.h>
#include <conio.h>
void main()
{

char letter;
cout<< “Enter a character : ”;
cin>>letter;
//determine letter is lowercase or not
if (letter>= ‘a’ && letter<= ‘z’)
cout<< “Entered character is a lowercase letter \n”;
else
cout<< “Entered character is not a lowercase letter \n”;
getch();
}

2.
#include <iostream.h>
#include <conio.h>
void main()
{
char status;
int senior=400, junior=275;
cout<< “ ‘J’ --> junior person’s salary \n ”;
cout<< “ Enter status > ”;
cin>>status;
if (status== ‘S’ || status== ‘s’)
cout<< “Senior person salary is Rs.”<<senior;
else if (status== ‘J’ || status== ‘j’)
cout<< “Junior person salay is Rs.”<<junior;
else
cout<< “You should select senior or junior, try again!”

getch();
}

3.
#include <iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter the value of a , b and c:";
cin>>a>>b>>c;
if(a==0)
{
cout<<"divisor can not be 0"<<endl;
}
else
{
Chapter 5  Conditional Structures (Programming Exercise Solutions) 103

if(b%a==0&&c%a==0)
cout<<a<<" is a common divisor of "<<b<<" and "<<c<<endl;
else
cout<<a<<" is not a common divisor of "<<b<<" and "<<c<<endl;
}
getch();
}

4.
#include <iostream.h>
#include <conio.h>
void main()
{
char op;
int side, base, height;
float area;
clrscr();
cout<<"Enter choice (S for Square, T for Triangle:)";
cin>>op;
if(op == 'S')
{
cout<<"Enter side: ";
cin>>side;
area = side * side;
cout<<"Area ="<< area;
}
else if(op == 'T')
{
cout<<"Enter base:";
cin>>base);
cout<<"Enter height:";
cin>>height;
area = base * height * 0.5;
cout<<"Area = "<< area;
}
getch();
}

5.
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
float temp, temp2;
char choice;
clrscr();
cout<<"Enter temperature: ";
cin>>temp;
cout<<"Enter choice: ";
cin>>choice;
if(choice == ‘f’)
{
temp2 = 5.0 / 9.0 * (temp – 32);
cout<<”Temperature in Celcius is “<<temp2<<endl;
104 IT Series  Object-Oriented Programming using C++

}
else if(choice == ‘c’)
{
temp2 = 9.0 / 5.0 * temp + 32;
cout<<"Temperature in Fahrenheit is "<<temp2;
}
else
cout<<"Invalid choice.";
getch();
}

6.
#include <iostream.h>
#include<conio.h>
void main()
{
int code;
clrscr();
cout<<"============== \n";
cout<<"Code Numbers\n";
cout<<"============== \n";
cout<<"1 for Western Digital \n";
cout<<"2 for 3M Corporation \n";
cout<<"3 for Maxil Corporation \n";
cout<<"4 for Sony Corporation \n";
cout<<"5 for Verbatim Corporation \n";
cout<<"\nEnter any code: ";
cin>>code;
switch(code)
{
case 1:
cout<<" Western Digital ";
break;
case 2:
cout<<"3M Corporation ";
break;
case 3:
cout<<"Maxil Corporation ";
break;
case 4:
cout<<"Sony Corporation ";
break;
case 5:
cout<<"Verbatim Corporation ";
break;
}
getch();
}

7.
#include <iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
Chapter 5  Conditional Structures (Programming Exercise Solutions) 105

cout<<"================= \n";
cout<<"Movies Categories \n";
cout<<"================= \n";
cout<<"A for Adventure \n";
cout<<"C for Comedy \n";
cout<<"F for Family \n";
cout<<"H for Horror \n";
cout<<"S for Science Fiction \n";
cout<<"\nEnter code of movie category: ";
cin>>ch;
switch(ch)
{
case 'A':
cout<<"Your selection is Adventure Movies";
break;
case 'C':
cout<<"Your selection is Comedy Movies";
break;
case 'F':
cout<<"Your selection is Family Movies";
break;
case 'H':
cout<<"Your selection is Horror Movies";
break;
case 'S':
cout<<"Your selection is Science Fiction Movies";
break;
default:
cout<<"Invalid category";
}
getch();
}

8.
#include <iostream.h>
#include<conio.h>
void main()
{
float value;
char con;
clrscr();
cout<<"Enter conversion type: \n";
cout<<"C for Centimeters \n";
cout<<"L for Liters \n";
cout<<"K for Kilometers \n";
cout<<"G for Kilograms \n";
cin>>con;
cout<<"Enter a value: ";
cin>>value;
switch(con)
{
case 'C':
case 'c':
cout<<"Value"<<value * 2.54;
break;
case 'L':
case 'l':
106 IT Series  Object-Oriented Programming using C++

cout<<"Value: "<< value * 3.785;


break;
case 'K':
case 'k':
cout<<"Value: "<< value * 1.609;
break;
case 'G':
case 'g':
cout<<"Value: "<<value * .4536;
break;
default:
cout<<"Invalid conversion type!";
}
getch();
}

9.

void main()
{
int year;
clrscr();
cout<<"Enter a year:";
cin>>year);
if(year%4==0)
cout<<"Leap year.";
else if(year%100==0 && year%400==0)
cout<<"Leap year.";
else
cout<<"Not a leap year.";
getch();
}

10.

#include <iostream.h>
#include <conio.h>
void main()
{
int t;
clrscr();
cout<<"Enter temperature: ";
cin>>t;
if(t > 35)
cout<<"Hot day.";
else if(t >= 25)
cout<<"Pleasant day.";
else
cout<<"Cool day.";
getch();
}
Chapter 5  Conditional Structures (Programming Exercise Solutions) 107

11.
#include <iostream.h>
#include <conio.h>
void main()
{
int m;
char g;
float a;
clrscr();
cout<<"Enter marks: ";
cin>>m;
a = m * 100.0 / 1100.0;
if(a >= 80)
cout<<"A+";
else if(a >= 70)
cout<<"A";
else if(a >= 60)
cout<<"B";
else if(a >= 50)
cout("C");
else if(a >= 40)
cout<<"D";
else if(a >= 33)
cout<<"E";
else
coutM<<"F";
getch();
}

You might also like