In: Computer Science
9. Generate a random number between 1 and 8, which is the computer’s number. Ask the user to guess a number between 1 and 8. Write a sentinel-controlled loop to continue to executed until the user’s guess matches the computer’s number. If the user's guess matches the random number, print a message in green on the SenseHat congratulating the user and then break out of the loop. Otherwise, print a message in red on the SenseHat to ask the user to try again.
10. Write nested for loops that print out the value of each index
for each iteration through the loop. The first loop should start at
1 and end at 3 and the second loop should start at 10 and end at
1.
11. Create a grocery store application. Prompt the user to input
the number of the number of items, name of the item, and the price
each. Use a while with sentinel control to exit the loop when the
user enters a number of items less than or equal to 0. At each
iteration of the loop, compute the cost of the items as the number
of items times the price each. Print out the name of the item and
the cost formatted with seven spaces and two decimal places of
precision at each iteration. (Hint: Use a priming read for the
number of items before you enter the while loop. Within the loop
read in the name of the item and the price each and perform the
calculation of the cost. Add a loop read that will read in the
number of items at the end of the loop code.)
12. Modify the grocery store application in problem 11 to compute
the accumulated cost of all items purchased. Prompt the user for
the sales tax rate. Compute the sales tax as the sales tax rate
times the accumulated cost. Compute the total cost as the
accumulated cost plus the sales tax. Print out the total cost
formatted with seven spaces and two decimal places of
precision.
Did not mation the programing language. so this cause all programs are written in C-language
Program:-
9)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, n,f=0;
do
{
printf("Guess a Number Between 1 to 8 : ");
scanf("%d",&c);
n = rand() % 8 + 1;
printf("Computer Guess: %d\n", n);
printf("Your Guess: %d\n", c);
if(c==n)
{
printf(" congratulating the user !!\n");
f=1;
}
else
{
printf("Guess did not match , Try again .....\n");
}
}while(f!=1);
return 0;
}
Output
10)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=10;j>=1;j--)
{
printf("index value i :%d\n",i);
printf("index value j :%d\n",j);
}
}
return 0;
}
Output
index value i :1
index value j :10
index value i :1
index value j :9
index value i :1
index value j :8
index value i :1
index value j :7
index value i :1
index value j :6
index value i :1
index value j :5
index value i :1
index value j :4
index value i :1
index value j :3
index value i :1
index value j :2
index value i :1
index value j :1
index value i :2
index value j :10
index value i :2
index value j :9
index value i :2
index value j :8
index value i :2
index value j :7
index value i :2
index value j :6
index value i :2
index value j :5
index value i :2
index value j :4
index value i :2
index value j :3
index value i :2
index value j :2
index value i :2
index value j :1
index value i :3
index value j :10
index value i :3
index value j :9
index value i :3
index value j :8
index value i :3
index value j :7
index value i :3
index value j :6
index value i :3
index value j :5
index value i :3
index value j :4
index value i :3
index value j :3
index value i :3
index value j :2
index value i :3
index value j :1
11)
#include <stdio.h>
int main()
{
int i=1,n;
float t=0, p[10];
char name[20];
printf("\n Enter the number of items: ");
scanf("%d",& n);
for(i=0;i<n;i++)
{
printf("\nEnter item name: ");
scanf("%s", name);
printf("\nEnter Price: ");
scanf("%f",&p[i]);
printf("\nYour item is %s.", name);
printf("\nprice: %.2f",p[i]);
t=t+p[i];
}
printf("\nTotal price: %.2f",t);
return 0;
}
Output
12)
#include <stdio.h>
int main()
{
int i=1,n;
float t=0, p[10],tax,c;
char name[20];
printf("\n Enter the number of items: ");
scanf("%d",& n);
printf("\n Enter the Sales Tax: ");
scanf("%f",&tax);
for(i=0;i<n;i++)
{
printf("\nEnter item name: ");
scanf("%s", name);
printf("\nEnter Price: ");
scanf("%f",&p[i]);
printf("\nYour item is %s.", name);
printf("\nprice: %.2f",p[i]);
t=t+p[i];
}
printf("\nTotal price: %.2f",t);
c= t+( t*(tax/100));
printf("\n Total acumulated Price Adding Tax: %.2f",c);
return 0;
}
Output