Sum of Digits Codechef Solution
Sum of Digits
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, the total number of testcases. Then follow T lines, each line contains an integer N.
Output
For each test case, calculate the sum of digits of N, and display it in a new line.
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 1000000
Example
Input 3 12345 31203 2123 Output 15 9 8
Program Code#include <iostream>
using namespace std;
int main()
{ int n;
cin>>n;
int digits;
for(int i=0;i<n;i++)
{
cin>>digits;
int rem,sum;
sum=0;
while(digits>0)
{
rem=digits%10;
sum=sum+rem;
digits=digits/10;
}
cout<<sum<<endl;
}
return 0;
}
Comments
Post a Comment