Você está na página 1de 13

Fahd Bin Sultan University

Name : fashqa hazazzi


201215029

1. Guess game
Build a simple guess game by asking the player to enter a number between 1 and 20 to
match the correct answer. The player has 3 chances to guess. While guessing, tell the
player whether his/her input is too small or too large than the correct answer. Assume
that the correct answer is 14.
Expected Results:

Ans :
#include<iostream.h>
void main()
{
int number=14;
int x,i;
for( i=1;i<=3;i++)
{
cout<<"enter a number between 1 to 20 ";
cin>>x;
if(x==14)
{
cout<<"You Win!"<<endl;
break;}
else
if(x>14)
cout<<"Too Large"<<endl;
else
cout<<"Too Small"<<endl;
}
}

. Modify the program above to transpose the result. For example,

Ans :
#include<iostream.h>
void main()
{
for(int i=1;i<=9;i++)
cout<<2x<<i<<=<<2*i<< ;
for(int j=1;j<=9;j++)
cout<<3x<<j<<=<<3*j<< ;

3. Fibonacci numbers is a series of integers where each number is the summation of two
previous numbers. For example:
0 1 1 2 3 5 8 13 21
This series has 9 integers.
Write a program to let users input how many numbers in Fibonacci series. Then show
the series.
**Remark that the first two numbers are always 0 and 1.

Ans :
#include<iostream.h>
int fib(int);
void main()
{
int x;
cout<<"how many Fibonacci numbers ?";
cin>>x;
for(int i=1;i<=x;i++)
cout<<fib(i)<<endl;
}
int fib(int y)
{
if(y==0 || y==1)
return y;
else
fib(y-1)+fib(y-2);
}

. Digits of numbers
Write a program to ask users to input an integer. Then separate it to many digits and
display them from right to left.

ANS :
4

#include<iostream.h>
void main()
{int number;
cout<<"Enter a number : ";
cin>>number;
int x1,x2,x3,x4,x5;
x1=number%10;
x2=(number/10)%10;
x3=(number/100)%10;
x4=(number/1000)%10;
x5=(number/10000)%10;
cout<<x5<<'\t'<<x4<<'\t'<<x3<<'\t'<<x2<<'\t'<<x1<<endl;
}

5 . Complete the following program to compute factorial of non-negative input


Integers. Note that factorial of 5 = 5x4x3x2x1 or 1x2x3x4x5 and factorial of 0 = 1.

Ans :
#include<iostream.h>
int fact(unsigned int);
void main()
{
int x;
5

cout<<"please enter a value to compute its factorial";


cin>>x;
cout<<"the factorial of x is "<<fact(x)<<endl;
}
int fact(unsigned int y)
{
if(y==1)
y;
else
return y*fact(y-1);
}

6. Input : 4 output :

*
*

*
*
*

*
*
*
*

*
*
*

*
*

Ans :
#include<iostream.h>
void main()
{
for(int i=1;i<=4;i++)
{
for(int k=1;k<=4-i;k++)
cout<<" ";
for(int j=1;j<2*i-1;j++)
cout<<"*"<<'\t';
cout<<endl;

}
}

*
*

*
*
*

*
*

*
*

*
*

Ans :
#include<iostream.h>
void main()
{
for(int i=1;i<=4;i++)
{
for(int k=1;k<=4-i;k++)
cout<<" ";
for(int j=1;j<i+1;j++)
if(i!=1)
{
cout<<"*"<<'\t';
cout<<endl; }
else
cout<<" *";
}

ABCDEFEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
Ans :
#include<iostream.h>
void main()
{
int i,n=65,m=72,f=0,s=-6;
while(m-->=n)//pointer
{s+=4;
for(i=n;i<m;i++)
cout<<i;
if(f==0) ++m;
else
for(i=0;i<s;i++)
cout<<" ";
for(i=m-1;i>=n;i--)
cout<<i;
cout<<endl;
f=1;
}

Complete the following program by filling in the missing codes.


The objective of the program below is to simulate an ATM. It has main features as follows. Try
to build a function for each feature.
show welcome message
ask for password and check password (assume the correct password is 1234)
check that the amount of withdraw is greater than 0 and less than or equal to the
Maximum limit (20000)
check that the amount of withdraw is divisible by 100
calculate and show the number of each type of banknote (1000, 500 and 100) that will be
Given to the user
show goodbye message

Ans :
#include<iostream.h>
void welcome(void);
void password(int);
void withdrawl(void);
void main()
{
int x ;
cout<<welcome;
}
void welcome(void)
{
cout<<"------------ATM------------";
cout<<"** Hello, this is MFU Bank **";
}
cout<<"Enter 4-digit password:";
cin>>x;
cout<<password(x);
cout<<"GOOD BYE"<<endl;
void password(int y)
{if(y==1234)
withdrawl();
else
cout<<"wrong password\n thanks for choosing MFU bank";
}
void withdrawl(void){
int amount;
cin>>amount;
if(amount>=0 && amount<=20000)
if(amount%100==0){
x1=amount/1000 ;
x2=(amount/100)%10;
x3=(x2/5);
x4=(X2%5);
cout<<"you got " <<x1<<" thousands banknote"<<endl;
cout<<"you got"<<x3<<"five hundred banknote"<<endl;

10

cout<<"you got"<<x4<<"hundred banknote"<<endl;


}
else
cout<<"sorry the amount is not divisable by 100";
else
cout<<"under or over withdrawl limit";
}

7. Write a function named "reduce" that takes two positive integer arguments, call them "num" and
"denom", treats them as the numerator and denominator of a fraction, and reduces the fraction. That is to
say,
each of the two arguments will be modified by dividing it by the greatest common divisor of the two
integers.
The function should return the value 0 (to indicate failure to reduce) if either of the two arguments is zero or
negative, and should return the value 1 otherwise.

Ans :
#include<iostream.h>
void main()
{
int goal;
cout<<please enter an integer!;
cin>>goal;
cout<<enough(goal)<<endl;
}
int enough (int goal)
{
int n = 1, sum = 1;
while (sum < goal)
sum += ++n;
return n;}

cout << enough(9) << endl; // will print 4 because 1+2+3+4 _ 9 but 1+2+3<9
c\out << enough(21) << endl;// will print 6 'cause 1+2+ . . .+6 _ 21 but 1+2+ . . . 5<21
cout << enough(-7) << endl;// will print 1 because 1 _ 7 and 1 is the smallest
r// positive integer
cout << enough(1) << endl; // will print 1 because 1 _ 1 and 1 is the smallest
// positive integer

11

8. Write a function named "reduce" that takes two positive integer arguments, call them "num" and
"denom", treats them as the numerator and denominator of a fraction, and reduces the fraction. That is to
say,
each of the two arguments will be modified by dividing it by the greatest common divisor of the two
integers.
The function should return the value 0 (to indicate failure to reduce) if either of the two arguments is zero or
negative, and should return the value 1 otherwise.
Ans :
#include<iostream.h>
int reduce (int num, int denom)
{
if (num <= 0 || denom <= 0)
return 1;
else
{
int common = g_c_d ( num, denom);
num /= common;
denom /= common;
}
}

9. Write a function named "swap_floats" that takes two floating point arguments and interchanges the
values that are stored in those arguments. The function should return no value.

Ans :
#include<iostream.h>
void swap_floats (float a, float b)
{

12

float temp = a ;
a = b;
b = temp;
}
void main ()
{
float a,b;
cin>>a>>b;
swap_floats(a,b);
}

13

Você também pode gostar