- Write a program in order to play the game “Game of Guessing”.
- Your program should generate a random number and the user should try to figure out that number.
- According to the users guess, the program should also direct the user, such as; if the user guesses a number less than the computers number the program should warn the user with a message as; “Too low, a little bit higher please”.
- And when the user finds the number the program should print the number of tries that the user did to find the number.
Sample Run: (Let’s say the computer generates a number: 55)
I have a number, try to guess it: 43
Too low, a little bit higher please: 70
Too high, a little bit lower please: 60
Too high, a little bit lower please: 50
Too low, a little bit higher please: 55
You found the number! Congratulations!
Number of tries = 5
Hints: To generate a random number include the libraries;
#include<stdlib.h>
#include<time.h>
And add the following code to your main program;
srand(time(0));
number = rand()%XXX
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
int guess, number, counter;
srand(time(0));
number=1+rand()%99;
// printf("number=%d\n",number);
// number=55;
printf("I have a number, try to guess it: ");
scanf(" %d",&guess);
counter=1;
while(guess != number)
{
if (guess < number)
printf("Too low, a little bit higher please: ");
else if (guess > number)
printf("Too high, a little bit lower please: ");
scanf(" %d",&guess);
++counter;
}
printf("You found the number! Congratulations!\n");
printf("Number of tries = %d\n",counter);
return 0;
}
- One large chemical company pays its salespeople on a commission basis. The salespeople receive $200 per week
- plus 8 percent (if he/she sold less then $2500 worth of chemicals in a week)
- or 12 percent (if he/she sold more then $2500 worth of chemicals in a week) of their gross sales for that week.
- For example, a salesperson who sells $5000 worth of chemicals in a week receives $200 plus 12 percent of $5000, or a total of $800.
Develop a complete C program that uses any of the repetitive structures to input each salesperson’s gross sales for last week and calculate and display that salesperson’s earnings.
Sample Run:
Enter sales in dollars (-1 to end): 5000
Salary is: $800.00
#include<stdio.h>
int main(void)
{
float sales, earning, base=200.0;
printf("Enter sales in dollars (-1 to end) : ");
scanf(" %f",&sales);
// printf("sales = %f \n",sales);
while(sales != -1)
{
if (sales <= 2500)
earning=base+sales*8/100;
else if (sales > 2500)
earning=base+sales*12/100;
printf("Salary is $%.2f \n",earning);
printf("Enter sales in dollars (-1 to end) : ");
scanf(" %f",&sales);
// printf("sales = %f \n",sales);
}
return 0;
}
- What will be the output of the following program fragment?
for( sum=0, i=2; i <= 8; i +=2)
{
j=i;
while(j < 4)
{
k=j;
do
{
sum++;
k+=2;
}while(k <= 3);
j++;
}
printf(" %d %d %d %d \n", sum, i, j, k);
}
#include<stdio.h>
int main()
{
int sum,i,j,k;
for( sum=0, i=2; i <= 8; i +=2)
{
j=i;
while(j < 4)
{
k=j;
do
{
sum++;
k+=2;
}while(k <= 3);
j++;
}
printf(" %d %d %d %d \n", sum, i, j, k);
}
return 0;
}
2 2 4 5
2 4 4 5
2 6 6 5
2 8 8 5
- A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following
five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a complete C program that reads in a five-digit integer and determines whether it is a palindrome.
Hints:
- Use the division and modulus operators to seperate the number into its individual digits.
- Store each digit in its own variable.
#include<stdio.h>
int main(void)
{
int i,number, onlar=10,a,b,c,d,e;
printf("Enter a five-digit integer : ");
scanf(" %d",&number);
// printf("number = %d \n",number);
a=number%onlar;
number=number/onlar;
b=number%onlar;
number=number/onlar;
c=number%onlar;
number=number/onlar;
d=number%onlar;
number=number/onlar;
e=number%onlar;
printf("a=%d b=%d c=%d d=%d e=%d \n",a,b,c,d,e);
if(a==e && b==d)
printf("Entered number %d%d%d%d%d is a palindrome\n",e,d,c,b,a);
else
printf("Entered number %d%d%d%d%d is NOT a palindrome\n",e,d,c,b,a);
return 0;
}