Solved Problems PDF
Solved Problems PDF
EXERCISE 4
1. When do you use the keyword return while defining a function? When do you not
use the keyword return when defining a function?
ANSWER:
I use the keyword return while defining the function to return the value of the called
function to the caller function and I did not use it when there is no need of the value
to be returned to the caller function.
2. Write a program in C++ that prints out the larger of two numbers entered from the
keyboard. Use a function to do the actual comparison of the two numbers. Pass the
two numbers to the function as arguments, and have the function return the answer.
PROGRAM
#include<iostream.h>
float compare(float num1,float num2);
main()
{
float a,b,large;
cout<<"Enter value of a"<<endl;
cin>>a;
cout<<"Enter value of b"<<endl;
cin>>b;
large=compare(a,b);
}
float compare(float num1,float num2)
{
if(num1>num2)
cout<<"a is large";
else if(num2>num1)
cout<<"b is large";
else
cout<<"a and b are equal";
}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 1
SAMPLE OUTPUT
3. Write a program in C++ that asks the user to input the length of two sides of the
right-angled triangle. Make use of the built-in functions pow() and sqrt() to
compute the length of hypotenuse using Pythagoras Theorem.
PROGRAM
#include<iostream.h>
#include<math.h>
main()
{
float base,height,hypo;
cout<<"Enter the base"<<endl;
cin>>base;
cout<<"Enter the height"<<endl;
cin>>height;
cout<<"The hypotenus value is "<<sqrt(pow(base,2)+pow(height,2));
}
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 2
4. Write a C++ program that asks for a distance in meters and convert it to feet. Let
your program use a function other than main to perform the conversion.
PROGRAM
#include<iostream.h>
float convert(float m);
main()
{
float meter,conv;
cout<<"Enter meter value"<<endl;
cin>>meter;
conv=convert(meter);
cout<<"The feet value is "<<conv;
}
float convert(float m)
{
float ans;
ans=m*3.28;
return ans;
}
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 3
5. Write a C++ program that calls a function which returns a cube of a given number.
PROGRAM
#include<iostream.h>
#include<math.h>
float cube(float a);
main()
{
float b,ans;
cout<<"Enter a value"<<endl;
cin>>b;
ans=cube(b);
cout<<"The cube of a number is "<<ans;
}
float cube(float a)
{
float answer;
answer=pow(a,3);
return answer;
}
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 4
6. Write a C++ program to accept a decimal number and pass this number to a
function that convert it to binary number and return it to the caller for the result
display.
PROGRAM
#include<iostream.h>
int dec(int d);
int main()
{
int decimalNumber,bin;
cout<<"Enter any decimal number:"<<endl;
cin>>decimalNumber;
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 5
REVIEW EXERCISES
1. Write a program in C++ that asks the user to input the length of two sides of a right
angled triangle. Make use of built in functions pow() and sqrt() functions to compute
the length of the Hypotenuse using Pythagoras theorem.
PROGRAM
#include<iostream.h>
#include<math.h>
main()
{
float base,height,hypo;
cout<<"Enter the base"<<endl;
cin>>base;
cout<<"Enter the height"<<endl;
cin>>height;
cout<<"The hypotenuse value is "<<sqrt(pow(base,2)+pow(height,2));
}
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 6
2. Using switch-case statements write a program in C++ that Add, Subtract, Multiply
and Divide two numbers. Let the program display the menu of options, prompt the
user to enter numbers and, perform computations and print the result in each case.
PROGRAM
#include<iostream.h>
main()
{
int a,b,answer,choice;
cout<<"Enter value of a \n";
cin>>a;
cout<<"Enter value of b \n";
cin>>b;
cout<<"Enter your choice \n";
cout<<"1:Add,2:Sub,3:Mult,4:Div \n";
cin>>choice;
switch(choice)
{
case 1:
answer=a+b;
cout<<"The sum is "<<answer;
break;
case 2:
answer=a-b;
cout<<"The defference is "<<answer;
break;
case 3:
answer=a*b;
cout<<"The product is "<<answer;
break;
case 4:
if(b>0)
{
answer=a/b;
cout<<"The devision is "<<answer;
}
else
cout<<"can not divide by zero";
break;
default:
cout<<"Unrecognized input";
break;
}
}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 7
SAMPLE OUTPUT
3. Using switch–case statements write a C++ mprogram that compute the area of a
circle, the area of a rectangle and the area of a triangle. Let the program prompt the
user to enter dimensions, perform calculations and print the result in each case. For
simplicity, assume C++ is not case sensitive.
PROGRAM
#include<iostream.h>
const float pie=3.14;
main()
{
float radius,base,height,length,width,b,area;
int choice;
cout<<"1:Area of the circle \n";
cout<<"2:Area of the triangle \n";
cout<<"3:Area of the rectangle \n";
cout<<"Enter your choice \n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the radius"<<endl;
cin>>radius;
area=pie*radius*radius;
cout<<"The area of the circle "<<area;
break;
case 2:
cout<<"Enter the base"<<endl;
cin>>base;
cout<<"Enter the height"<<endl;
cin>>height;
area=0.5*base*height;
cout<<"The area of the triangle is "<<area;
break;
case 3:
cout<<"Enter the length"<<endl;
cin>>length;
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 8
cout<<"Enter the width"<<endl;
cin>>width;
area=length*width;
cout<<"The area of the rectangle is "<<area;
break;
default:
cout<<"Unrecognized input";
break;
}
}
SAMPLE OUTPUT
4. Using if conditional statement and only one function main(), write a C++ program to
convert temperature from degrees Centigrade to degrees Fahrenheit and vice versa.
Provide the menu from which a user can make a choice.
(Hint: Use the mathematical model C = 5/9*(F – 32), F=9/5*C+32.
PROGRAM
#include<iostream.h>
main()
{
float cent,fah,temp;
int choice;
cout<<" MENU "<<endl;
cout<<"1: CONVERT TO CENTIGRADE"<<endl;
cout<<"2: CONVERT TO FAHRENHEIT"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
cout<<"Enter the temperature value"<<endl;
cin>>temp;
if(choice==1)
{
cent=5.0/9.0*(temp-32);
cout<<"The centigrade value is "<<cent;
}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 9
else if(choice==2)
{
fah=9.0/5.0*temp+32;
cout<<"The Fahrenheit value is "<<fah;
}
else
cout<<"Wrong choice";
}
SAMPLE OUTPUT
5. Write a C++ program that uses a function called cylinder_vol() to compute the
volume of a cylinder. Let the main() program prompt the user to enter radius and
height of a cylinder, pass these values to cylinder_vol() which perform the
computation and return the result to main for display.
(Hint: Use the mathematical model v = r2h)
PROGRAM
#include<iostream.h>
float cylinder_vol(float r,float h);
const float pie=3.14;
main()
{
float volume,radius,height;
cout<<"Enter the radius"<<endl;
cin>>radius;
cout<<"Enter the height"<<endl;
cin>>height;
volume=cylinder_vol(radius,height);
cout<<"The volume of Cylinder is "<<volume;
}
float cylinder_vol(float r,float h)
{
float vol;
vol=pie*r*r*h;
return vol;}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 10
SAMPLE OUTPUT
6. Using the “switch” statement write a C++ program that recognizes the numbers 2, 4,
6, 8 and 10. Let the program display the word TWO when the number two is entered,
FOUR, when the number 4 is entered and so on. Let also the program display the
statement UNRECOGNIZED INPUT!, when the number entered is not among the
numbers mentioned above.
PROGRAM
#include<iostream.h>
main()
{
int choice;
cout<<"Enter the number you wish to be displayed i.e 2,4,6,8 or 10"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 2:
cout<<"TWO";
break;
case 4:
cout<<"FOUR";
break;
case 6:
cout<<"SIX";
break;
case 8:
cout<<"EIGHT";
break;
case 10:
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 11
cout<<"TEN";
break;
default:
cout<<"UNRECOGNIZED INPUT!";
break;
}
}
SAMPLE OUTPUT
7. Write a C++ program that prompt the user for an integer value. Next using a for loop
make it count from this value to zero displaying each number on its own line. When it
reaches zero have it sound a bell.
Hint: For a bell to sound use the character combination (“\a”).
PROGRAM
#include<iostream.h>
main()
{
int a;
cout<<"Enter the value of a"<<endl;
cin>>a;
for(a=a;a>=0;a--)
cout<<a<<endl;
cout<<"\a";
}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 12
SAMPLE OUTPUT
8. Using switch–case statements write a program that Add, Subtract, Multiply and
divide two numbers. Let the program prompt the user to make a choice of what
operation to be made and print the result in each case. For simplicity, assume C++ is
not case sensitive. Note that C++ is case sensitive.
PROGRAM
#include<iostream.h>
main()
{
int a,b,answer,choice;
cout<<"Enter value of a \n";
cin>>a;
cout<<"Enter value of b \n";
cin>>b;
cout<<"Enter your choice \n";
cout<<"1:Add,2:Sub,3:Mult,4:Div,5:Modulus \n";
cin>>choice;
switch(choice)
{
case 1:
answer=a+b;
cout<<"The sum is "<<answer;
break;
case 2:
answer=a-b;
cout<<"The defference is "<<answer;
break;
case 3:
answer=a*b;
cout<<"The product is "<<answer;
break;
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 13
case 4:
if(b>0)
{
answer=a/b;
cout<<"The devision is "<<answer;
}
else
cout<<"can not divide by zero";
break;
case 5:
answer=a%b;
cout<<answer;
break;
default:
cout<<"Unrecognized input";
break;
}
}
SAMPLE OUTPUT
9. Using switch–case statements write a program that compute the area of a circle, the
area of a rectangle and the area of a triangle. Let the program prompt the user to
enter dimensions, perform calculations and print the result in each case. Note that
C++ is case sensitive.
PROGRAM
#include<iostream.h>
const float pie=3.14;
main()
{
float radius,base,height,length,width,b,area;
int choice;
cout<<"1:Area of the circle \n";
cout<<"2:Area of the triangle \n";
cout<<"3:Area of the rectangle \n";
cout<<"Enter your choice \n";
cin>>choice;
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 14
switch(choice)
{
case 1:
cout<<"Enter the radius"<<endl;
cin>>radius;
area=pie*radius*radius;
cout<<"The area of the circle "<<area;
break;
case 2:
cout<<"Enter the base"<<endl;
cin>>base;
cout<<"Enter the height"<<endl;
cin>>height;
area=0.5*base*height;
cout<<"The area of the triangle is "<<area;
break;
case 3:
cout<<"Enter the length"<<endl;
cin>>length;
cout<<"Enter the width"<<endl;
cin>>width;
area=length*width;
cout<<"The area of the rectangle is "<<area;
break;
default:
cout<<"Unrecognized input";
break;
}
}
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 15
10. Write a C++ program to read two “float” values representing the radius and height of
a cylinder. Let your program define two functions cylinder_area() and
cylinder_vol(), which receive radius and height dimensions from main(), calculate
the area and volume of a cylinder and then return the results to main() for display.
Hint: Use the following mathematical models: A = 2πr2 + 2πrh, V = πr2h.
PROGRAM
#include<iostream.h>
float cylinder_vol(float r,float h);
float cylinder_area(float rd,float ht);
const float pie=3.14;
main()
{
float area,volume,radius,height;
cout<<"Enter the radius"<<endl;
cin>>radius;
cout<<"Enter the height"<<endl;
cin>>height;
volume=cylinder_vol(radius,height);
volume=cylinder_area(radius,height);
cout<<"The area of Cylinder is "<<area<<endl;
cout<<"The volume of Cylinder is "<<volume;
}
float cylinder_vol(float r,float h)
{
float vol;
vol=pie*r*r*h;
return vol;
}
float cylinder_area(float rd,float ht)
{
float a;
a=2*pie*rd*rd + 2*pie*rd*ht;
return a;
}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 16
SAMPLE OUTPUT
11. Write a C++ program to read a “float” number representing temperatures. Let your
program define two functions convert_Fahr() and convert_Cels(), whereby
convert_Fahr() converts degree Fahrenheit to degree Celsius and convert_Cels()
convert degree Celsius to Fahrenheit. Your main program should print the float
equivalent temperature results such us:
“100.0 DEGREES CELSIUS EQUALS 212.0 DEGREE FAHRENHEIT”.
After the user has entered a number, let him have a choice of what conversion he
want to perform.
PROGRAM
#include<iostream.h>
float convert_fuhr(float far);
float convert_cels(float cel);
main()
{
float temp,tempvalue;
int choice;
cout<<"Enter the temperature value for conversion"<<endl;
cin>>temp;
else if(choice==2)
{
tempvalue=convert_cels(temp);
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 17
cout<<temp<<" DEGREE CELSIUS EQUALS "<<tempvalue <<" DEGREE
FAHRENHEIT.";
}
else
cout<<"WRONG CHOICE";
}
float convert_fuhr(float fr)
{
float value;
value=5.0/9.0*(fr-32);
return value;
}
float convert_cels(float cel)
{
float value;
value=9.0/5.0*cel+32;
return value;
}
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 18
12. Using if or if ... else conditional statement, write a C++ program to convert
temperature from degrees Centigrade to degrees Fahrenheit or vise versa. Declare a
function other than main() that receives temperature value perform the conversion
and return the converted value to main() for display. Provide the menu from which a
user can make a choice of what a conversion to perform. (Hint: Use the
mathematical model C = (5/9)(F – 32)).
PROGRAM
#include<iostream.h>
float convert_fuhr(float far);
float convert_cels(float cel);
main()
{
float temp,tempvalue;
int choice;
cout<<"Enter the temperature value for conversion"<<endl;
cin>>temp;
else if(choice==2)
{
tempvalue=convert_cels(temp);
cout<<"The fahrenheit value is "<<tempvalue;
}
else
cout<<"WRONG CHOICE";
}
float convert_fuhr(float fr)
{
float value;
value=5.0/9.0*(fr-32);
return value;
}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 19
float convert_cels(float cel)
{
float value;
value=9.0/5.0*cel+32;
return value;
}
SAMPLE OUTPUT
13. Write a C++ program that reads two “floating” point numbers representing the radius
and height of a cylinder. Declare two functions that calculate the area and volume of a
cylinder and let the program print out the area and volume for the given dimensions.
Your output should take the form.
The area of a cylinder of radius …cm and height …cm is … square cm.
The volume of a cylinder of radius …cm and height …cm is … cubic cm.
Also let your program print the error message:
“ERROR!! Negative values not permitted!!”
In case the user enters negative values for radius or height.
PROGRAM
#include<iostream.h>
const float pai=3.14;
float Area(float rad,float Ht);
float Volume(float rd,float ht);
main()
{
float radius,height,area,volume;
cout<<"Enter the radius value"<<endl;
cin>>radius;
cout<<"Enter the height value"<<endl;
cin>>height;
if(radius<0||height<0)
cout<<"ERROR!! Negative values not permitted!!";
else
{
area=Area(radius,height);
volume=Volume(radius,height);
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 20
cout<<"The area of a cylinder of radius "<<radius<<" cm and height of "<< height<<"
cm is " <<area <<" square cm"<<endl;
cout<<"The volume of a cylinder of radius "<<radius<<" cm and height of
"<<height<<" cm is " <<volume<<" cubic cm";
}
}
SAMPLE OUTPUT
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 21
14. Determine the output that will be generated by the following C++ program.
#include <iostream.h>
main()
{
int x = 10, y = 20, z, m, k;
char c, ch;
m = x;
z = 'x';
k = 'y';
c = 'A';
ch = 65;
cout << " The value of x is " << x << endl;
cout << " The value of m is " << m << endl;
cout << " The value of y is " << y << endl;
cout << " The value of z is " << z << endl;
cout << " The value of k is " << k << endl;
cout << " The value of c is " << c << endl;
cout << " The value of ch is " << ch << endl
}
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 22
15. Using the standard library functions sqrt() and pow(), write a C++ expression
corresponding to the following mathematical expressions.
Hint: sqrt(x) returns a non-negative square root of x, and pow(x, y) returns xy.
ANSWER:
a). <=> z = (3*pow(x,4)+5*pow(x,2)) / (3*x-y)
PREPARED BY- MAZOLEKA GODFREY +255 769 024 207; JUNE 09, 2015 Page 23