Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

Sum of Digits : FLOW006 | CodeChef Solution



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

  •  T  1000
  •  N  1000000
  • 1 \leq T \leq 1000

Sample 1:

Input
Output
3 
12345
31203
2123
15
9
8

Solution

#include <iostream>
using namespace std;

// Solution from : Code Radius [ https://radiuscode.blogspot.com/ ]

int main()
{
int t;
cin>>t;

while(t--)
{
int n;
cin>>n;

int sum=0;

while(n>0)
{
sum+=(n%10);
n/=10;
}

cout<<sum<<"\n";

}
return 0;

}

Please First Try to Solve Problem by Yourself.

Post a Comment

© Code Radius 2. All rights reserved. Distributed by Techy Darshan Distributed by Pro Templates