Code:
1. Assume 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
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int i, j, entred_int;
cout<<"Enter a number: "; cin >>entred_int;
for(i=0; i<20; i++)
{for(j=0; j<10; j++)
cout<<setw(7)<<(entred_int*(10*i+j+1));
cout<<endl;}
}
Code:
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,
2 to convert Celsius to Fahrenheit: 1
Enter temperature in Fahrenheit: 70
In Celsius that’s 21.111111
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int choise; float temp;
cout<<"Type 1 to convert Fahrenheit to Celsius,\n 2 to convert Celsius to Fahrenheit: ";
cin >>choise;
switch(choise){ //replacable by if ... else .
case 2:
cout<<"Enter temperature in Celsius: "; cin >>temp;
cout<<"In Fahrenheit that's "<<(9*temp/5)+32;
break;
case 1:
cout<<"Enter temperature in Fahrenheit: "; cin >>temp;
cout<<"In Celsius that's " <<((temp-32)*5)/9;
break;
default :
cout<<"Invalid choise try again !";}
}
Code:
3. Operators such as >>, which read input from the keyboard, must be able to convert a series of
digits into a number. Write a program that does the same thing. It should allow the user to type up
to six digits, and then display the resulting number as a type long integer. The digits should be read
individually, as characters, using getche(). Constructing the number involves multiplying the existing
value by 10 and then adding the new digit. (Hint: Subtract 48 or ‘0’ to go from ASCII to a
numerical digit.)
Here’s some sample interaction:
Enter a number: 123456
Number is: 123456
#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
int i; long _output; char char_input;
cout<<"Enter a number (Maximum six characters): ";
_output = 0; i=0;
//ASCII('\r') == 13
while((char_input=getche()) != 13 && i<6){
_output = 10*_output + (char_input - 48);
i++;}
if(i == 6) cout<<"\b ";
cout<<"\nNumber is: "<<_output;
}
Code:
4. Create the equivalent of a four-function calculator. The program should request the user to
enter a number, an 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 if 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.333333
Do another (y/n)? y
Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n)? n
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
void calcul(void); //calculations function.
void asking(void); //asking to continue function.
intt main()
{
do{
calcul();
asking();
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
void calcul(void)
{
float num[2]; char operation;
cout<<"\nEnter first number, operator, second number: ";
cin >>num[0]>>operation>>num[1];
switch(operation){
case '+':
cout<<"Answer = "<<num[0] + num[1]<<endl;
break;
case '-':
cout<<"Answer = "<<num[0] - num[1]<<endl;
break;
case '*':
cout<<"Answer = "<<num[0] * num[1]<<endl;
break;
case '/':
if(num[1] != 0) cout<<"Answer = "<<num[0] / num[1]<<endl;
else {cout<<"Math error !"<<endl; calcul();}
break;
default:
cout<<"Unknow operator please try again !"<<endl;
calcul();}
}
void asking()
{
cout<<"\nDo another (y/n)? ";
switch(toupper(getche())){
case 'Y':
calcul();
asking();
break;
case 'N':
exit(1);
break;
default:
asking();}
}
Code:
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.
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
for(int i=0; i<20; i++)
{
for(int X=0 ; X<20 ; X++) cout<<" "; //This line optional to make the pyramid on the midlle.
for(int j=20-i; j>0 ; j--) cout<<" ";
for(int k=0 ; k<2*i+1; k++) cout<<"X";
cout<<endl;}
}
6. Modify the FACTOR program in this chapter so that it repeatedly asks for a number and
calculates its factorial, until the user enters 0, at which point it terminates. You can enclose the
relevant statements in FACTOR in a while loop or a do loop to achieve this effect.*/
#include<iostream>
using namespace std;
#include<conio.h>
// The FACTOR program:
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!#include <iostream>
!using namespace std;
!
!int main()
! {
! unsigned int numb;
! unsigned long fact=1; //long for larger numbers
!
! cout << “Enter a number: ”;
! cin >> numb; //get number
!
! for(int j=numb; j>0; j--) //multiply 1 by
! fact *= j; //numb, numb-1, ..., 2, 1
! cout << “Factorial is ” << fact << endl;
! return 0;
! }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void main(void)
{
cout<<"### Programmed By Amahdy(MrJava) ,right restricted.~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"-------------------------------------------------------------------------------\n"<<endl;
unsigned int numb, i;
unsigned long fact;
cout<<"Enter a number: ";
cin >>numb;
do{
fact=1; i=0;
for(i=numb; i>0; i--) fact *= i;
cout<<"\n\nFactorial is "<<fact<<endl;
cout<<" !Enter 0 to exit or any other number to calculate it's factorial: ";
cin >>numb;
}while(numb!=0);
}
Code:
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 the 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.
At the end of the first year you have 3000 + (3000 * 0.055), which is 3165. At the end of the
second year you have 3165 + (3165 * 0.055), which is 3339.08. Do this as many times as there
are years. A for loop makes the calculation easy.
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int num_year; float init_amount, intrst_rate;
do{
cout<<"Enter initial amount : ";
cin >>init_amount;
cout<<"Enter number of years : ";
cin >>num_year ;
cout<<"Enter interest rate (percent per year): ";
cin >>intrst_rate;
for(int i=0; i<num_year; i++) init_amount += init_amount*intrst_rate/100;
cout<<"At the end of "<<num_year
<<" years, you will have "<<init_amount
<<" dollars.";
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
Code:
8. Write a program that repeatedly asks the user to enter two money amounts expressed in old-
style British currency: pounds, shillings, and pence. (See Exercises 10 and 12 in Chapter 2, “++
Programming Basics.”) 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 if 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.*/
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int m[2][3]; char sep; //m (money), sep (char_separator).
do{
cout<<"Enter first amount : \x9c";
cin >>m[0][0]>>sep>>m[0][1]>>sep>>m[0][2];
cout<<"Enter second amount: \x9c";
cin >>m[1][0]>>sep>>m[1][1]>>sep>>m[1][2];
m[0][0] += m[1][0]; m[0][1] += m[1][1]; m[0][2] += m[1][2];
if(m[0][2]>11){m[0][1] += static_cast<int>(m[0][2]/12); m[0][2] %= 12;}
if(m[0][1]>19){m[0][0] += static_cast<int>(m[0][1]/20); m[0][1] %= 20;}
cout<<"Total is : \x9c"<<m[0][0]<<sep<<m[0][1]<<sep<<m[0][2];
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
Code:
9. Suppose you give a dinner party for six guests, but your table seats only four. In how many ways
can four of the six guests arrange themselves at the table? Any of the six guests can sit in the first
chair. Any of the remaining five can sit in the second chair. Any of the remaining four can sit in the
third chair, and any of the remaining three can sit in the fourth chair. (The last two will have to
stand.) So the number of possible arrangements of six guests in four chairs is 6*5*4*3, which is
360. Write a program that calculates the number of possible arrangements for any number of guests
and any number of chairs. (Assume there will never be fewer guests than chairs.) Don’t let this get
too complicated. A simple for loop should do it.
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int ch_num, gs_num, result;
do{
cout<<"Enter number of guests : \xdb "; cin >>gs_num;
cout<<"Enter number of chairs : \xdb "; cin >>ch_num;
result=1; for(int i=0; i<ch_num; i++) result*=(gs_num-i);
cout<<"The number of possible arrangements is : \xdb "<<result;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
Code:
10. Write another version of the program from Exercise 7 so that, instead of finding the final
amount of your investment, you tell the program the final amount and it figures out how many years it
will take, at a fixed rate of interest compounded yearly, to reach this amount. What sort of loop is
appropriate for this problem? (Don’t worry about fractional years; use an integer value for the year.)
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int i; float init_amount, intrst_rate, finl_amount;
do{
cout<<"Enter initial amount : "; cin >>init_amount;
cout<<"Enter interest rate (percent per year): "; cin >>intrst_rate;
cout<<"Enter final amount : "; cin >>finl_amount;
i=0;
while(finl_amount>=init_amount) {finl_amount -= finl_amount*intrst_rate/100; i++;}
cout<<"Number of years is : "<<i;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
Code:
11. Create a three-function calculator for old-style English currency, where money amounts are
specified in pounds, shillings, and pence. (See Exercises 10 and 12 in Chapter 2.) The calculator
should allow the user to add or subtract two money amounts, or to multiply a money amount by a
floating-point number. (It doesn’t make sense to multiply two money amounts; there is no such thing
as square money. We’ll ignore division. Use the general style of the ordinary four-function
calculator in Exercise 4 in this chapter.)
#include<iostream.h>
#include<conio.h>
using namespace std;
void chk_overs(void);
int m[3][3]; char c[3];
int main()
{
do{
cout<<"Enter the currency opperation: ";
cin >>m[0][0]>>c[0]>>m[0][1]>>c[1]>>m[0][2]>>c[2];
switch(c[2]){
case '+':
cin >>m[1][0]>>c[0]>>m[1][1]>>c[1]>>m[1][2];
m[0][0] += m[1][0]; m[0][1] += m[1][1]; m[0][2] += m[1][2];
chk_overs();
cout<<"Answer = "<<m[0][0]<<c[0]<<m[0][1]<<c[1]<<m[0][2];
break;
case '-':
cin >>m[1][0]>>c[0]>>m[1][1]>>c[1]>>m[1][2];
m[0][0] -= m[1][0]; m[0][1] -= m[1][1]; m[0][2] -= m[1][2];
chk_overs();
cout<<"Answer = "<<m[0][0]<<c[0]<<m[0][1]<<c[1]<<m[0][2];
break;
case '*':
cin >>m[1][0];
m[0][0] *= m[1][0]; m[0][1] *= m[1][0]; m[0][2] *= m[1][0];
chk_overs();
cout<<"Answer = "<<m[0][0]<<c[0]<<m[0][1]<<c[1]<<m[0][2];
break;
default :
cout<<"Syntex or operation error, check your inputs again.";}
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
void chk_overs(void)
{
if(m[0][2]>11){m[0][1] += static_cast<int>(m[0][2]/12); m[0][2] %= 12;}
if(m[0][1]>19){m[0][0] += static_cast<int>(m[0][1]/20); m[0][1] %= 20;}
}
Code:
12. Create a four-function calculator for fractions. (See Exercise 9 in Chapter 2, and Exercise 4 in
this chapter.) Here are the formulas for the four arithmetic operations applied to fractions:
Addition: a/b + c/d = (a*d + b*c) / (b*d)
Subtraction: a/b - c/d = (a*d - b*c) / (b*d)
Multiplication: a/b * c/d = (a*c) / (b*d)
Division: a/b / c/d = (a*d) / (b*c)
The user should type the first fraction, an operator, and a second fraction. The program should then
display the result and ask if the user wants to continue.
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int first[2], last[2];
char op[2];
do{
cout<<"Enter your task : ";
cin >>first[0]>>op[0]>>last[0]>>op[1]
>>first[1]>>op[0]>>last[1];
if(!last[0] || !last[1]) {cout<<"Illeagle fraction !"<<endl; op[1] = false;}
switch(op[1]) {
case '+':
cout<<"Answer = "<<(first[0]*last[1] + last[0]*first[1])<<op[0]<<(last[0]*last[1]);
break;
case '-':
cout<<"Answer = "<<(first[0]*last[1] - last[0]*first[1])<<op[0]<<(last[0]*last[1]);
break;
case '*':
cout<<"Answer = "<<first[0]*first[1]<<op[0]<<last[0]*last[1];
break;
case '/':
if(first[1] != 0) cout<<"Answer = "<<first[0]*last[1]<<op[0]<<first[1]*last[0];
else cout<<"Math error !"<<endl;
break;
default:
cout<<"Unknow operator please try again !"<<endl;}
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}
0 comentários:
Post a Comment