Patrick and Johnny started to play a new game. The game contains a board which consists of concentric circles. When Patrick correctly hits the center of the concentric circles, his score is equal to the target score. The score gets reduced depending on where the players hit on the board. When the players hits outside the board, his score is 0. Johnny will not allow Patrick to have his food unless he reaches the target score.
The players may take more turns to reach the target score.
Can you write a C program to determine the number of turns a player takes to reach the target score of ‘n’ using for loop?
Input Format:
Input consists of a list of positive integers. The first integer corresponds to the target score 'n'. Assume that all the other integers input are less than or equal to n.
Output Format:
Output consists of a single line. Refer sample output for format details.
[All text in bold corresponds to the input and the rest corresponds to output.]
Sample Input and Output1:
Enter the target score:
100
Enter the scores taken by Patrick in each turn:
4
40
60
The number of turns is 3
Sample Input and Output 2:
Enter the target score:
1000
Enter the scores taken by Patrick in each turn:
1000
The number of turns is 1
Program Code:
#include<stdio.h>
int main()
{
int num, sum=0, count, n;
printf("Enter the target score:\n");
scanf("%d", &num);
printf("Enter the scores taken by Patrick in each turn:\n");
for(count=0;sum<num;count++)
{
scanf("%d", &n);
sum=sum+n;
}
printf("The number of turns is %d", count);
return 0;
}
Post A Comment:
0 comments: