Chapter (3)
no.1
Assume that you want to generate a table of multiples of any given number. Write a program that allows the user to enter the number and then generates
the table, formatting it into 10 columns and 20 lines. Interaction with the program should look like this ( only the first three lines are shown ):
Enter a number: 7 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210
Answer
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
clrscr();
int x,i,fact;
cout<<“Enter a number: “; cin>>x;
fact=1;
for(i=1;i<=30;i++)
{
fact= x*i;
cout<<setw(5)<<fact;
if(i%10==0)
cout<<endl;
}
getch();
return 0;
}
no.2
Write a temperature-conversion program that gives the user the option of converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the conversion. Use floating-point numbers. Interaction with the program might look like this:
Type 1 to convert Fahrenheit to Celsius
Type 2 to convert Celsius to Fahrenheit : 1
Enter temperature in Fahrenheit : 70
In Celsius that’s 21.111111
Answer
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int response;
float ftemp,ctemp;
cout<<“Type 1 to convert Fahrenheit to Celsius”;
cout<<“\nType 2 to convert Celsius to Fahrenheit”;
cin>>response;
if(response==1)
{
cout<<“Enter temperature in Fahrenheit: “;
cin>>ftemp;
ctemp=(ftemp-32)*5/9;
cout<<“In Celcius that’s “<<ctemp<<endl;
}
if(response==2)
{
cout<<“Enter temperature in Celsius”;
cin>>ctemp;
ftemp=ctemp*9/5+32;
cout<<“In Fahrenheit that’s “<<ftemp<<endl;
}
getch();
return 0;
}
no.4
Create the equivalent of a four-function calculator. The program should ask the user to enter a number, and operator, and another number. (Use floating point). It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result.
When it finishes the calculation, the program should ask whether the user wants to do another calculation. The response can be ‘y’ or ‘n’. Some sample interaction with the program might look like this:
Enter first number, operator, second number : 10/3
Answer = 3.33333
Do another (y/n) ? y
Enter first number, operator, second number : 12 + 100
Answer = 112
Do another (y/n) ? n
Answer
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
char resq,oper;
int num1,num2;
float ans;
do{
cout<<“Enter first number,operator and second number: “; cin>>num1>>oper>>num2;
switch(oper)
{
case’+’:ans=num1+num2;break;
case’-‘:ans=num1-num2;break;
case’*‘:ans=num1num2;break;
case’/’:ans=num1/num2;break;
}
cout<<“Answer is “<<ans<<endl;
cout<<“Do another (y/n)”;
cin>>resq;
}
while(resq!=’n’);
getch();
return 0;
}
no.5
Use for loops to construct a program that displays a pyramid of Xs on the screen. The pyramid should look like this:
X
XXX
XXXXX
XXXXXXX
XXXXXXXXX
Except that it should be 20 lines high, instead of the 5 lines shown here. One way to do this is to nest two inner loops, one to print spaces and one to print Xs, inside an outer loop that steps down the screen from line to line.
Answer
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char star=’‘;
for(int i=1;i<=5;i++)
{for(int j=i;j<5;j++)
cout<<” “;
for(int k=1;k<=(i2)-1;k++)
cout<<star;
cout<<endl;
}
getch();
return 0;
}
no.7
Write a program that calculates how much money you’ll end up with if you invest an amount of money at a fixed interest rate, compounded yearly. Have the user furnish the initial amount, the number of years and yearly interest rate in percent. Some interaction with the program might look like this:
Enter initial amount: 3000
Enter number of years: 10
Enter interest rate (percent per year) : 5.5
At the end of 10 years, you will have 5124.43 dollars.
Answer
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
int i;
float amount,years,rate,interest;
cout<<“Enter initial number: “; cin>>amount;
cout<<“Enter number of years: “; cin>>years;
cout<<“Enter interest rate: “; cin>>rate;
for(i=1;i<=years;i++){
interest=(amount*rate)/100;
amount+=interest;
}
cout<<“At the end of 10 years, you will have ” <<amount;
getch();
return 0;
}
no.8
Write a program that repeatedly asks the user to enter two money amounts expressed in old-style British currency: pounds, shillings, and pence. The program should then add the two amounts and display the answer , again in pounds, shillings and pence. Use a do loop that asks the user whether the program should be terminated. Typical interaction might be
Enter first amount: £ 5.10.6
Enter second amount: £ 3.2.6
Total is £ 8.13.0
Do you wish to continue ( y/n ) ?
To add the two amounts, you’ll need to carry 1 shilling when the pence value is greater than 11 and carry 1 pound when there are more than 19 shillings.
Answer
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
int p1 ,p2, sh1 ,sh2, pen1, pen2, p, sh, pen, sh3, p3;
char dummy,ps=’\x9c’;
cout<<“Enter first amount: “<<‘\t'<<ps;
cin>>p1>>dummy>>sh1>>dummy>>pen1;
cout<<“Enter second amount: “<<‘\t'<<ps;
cin>>p2>>dummy>>sh2>>dummy>>pen2;
pen=pen1+pen2;
if(pen>=12)
{ sh3=pen/12;
pen=pen%12;
}
else
sh3=0;
sh=sh1+sh2+sh3;
if(sh>=20)
{ p3=sh/20;
sh%=20;
}
else
p3=0;
p=p1+p2+p3;
cout<<“The answer is: “<<ps<<p<<“.”<<sh<<“.”<<pen;
getch();
return 0;
}