Patrick wants to add credit points to his credit card. Since each card game in Disneyland can be played only once, he cannot play the card game – III again to add credit points. So he decided to play the card game – VI which also helps in adding credit points. In this game, he has to pick one card which is shuffled on the table. Patrick must handover the card picked by him to Disneyland card game manager. The sum of digits of the card number he picked is credited as points to Patrick.
The players can pick any card with any number of digits. So it is difficult for the Disneyland managers to sum up all digits in the card number. They approach you and ask for your help.
Can you please help them with the program to find the sum of all digits in the card using for loop?
Input Format:
Input consists of a single integer.
Output Format:
Output consists of a single line. Refer sample output for details.
[All text in bold corresponds to the input and the rest corresponds to output]
Sample Input and Output 1:
Enter the card picked up by Patrick:
3459
The credit points is 21
Sample Input and Output 2:
Enter the card picked up by Patrick:
120
The credit points is 3
Program Code:
#include<stdio.h>
int main()
{
long num, digit, sum = 0;
printf("Enter the card picked up by Patrick:\n");
scanf("%ld", &num);
for (;num > 0;)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("The credit points is %ld\n", sum);
return 0;
}
Post A Comment:
0 comments: