Problem
You're given an integer N. Write a program to calculate the sum of all the digits of N.
Input Format
The first line contains an integer T, the total number of testcases. Then follow T lines, each line contains an integer N.
Output Format
For each test case, calculate the sum of digits of N, and display it in a new line.
Constraints
Sample 1:
Input
Output
3 12345 31203 2123
15 9 8
ANSWER:-
#include<iostream>
using namespace std;
int main(){
int test;
cin>>test;
while(test -- ){
int n,rem, sum = 0;
cin>>n;
while(n>0){
rem = n % 10;
sum += rem;
n /= 10;
}
cout<<sum<<endl;
}
return 0;
}
Comments
Post a Comment