Sum of Digits
All submissions for this problem are available.
You're given an integer N. Write a program to calculate the sum of all the digits of N.
Input
The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N.
Output
Calculate the sum of digits of N.
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 1000000
Example
Input 3 12345 31203 2123 Output 15 9 8
solutions
- #include<stdio.h>
- int main()
- { int t,i,d;
- long n,s;
- scanf("%d",&t);
- for(i=0;i<t;i++)
- { s=0;
- scanf("%ld",&n);
- while(n!=0){
- d=n%10;
- s=s+d;
- n=n/10;
- }
- printf("%ld\n",s);
- }
- }
No comments:
Post a Comment