Algorithm


problem Link : https://www.codechef.com/problems/FLOW006 

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

  • 1≤�≤1000
  • 1≤�≤1000000

Sample 1:

Input
 
Output
 
3 
12345
31203
2123
15
9
8

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include<stdio.h>
int main()
{
    int n,r,num;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&num);
        int sum=0;
        while(num!=0)
        {
            r=num%10;
            sum=sum+r;
            num=num/10;
        }
        printf("%d\n",sum);
    }
} 
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
12345
31203
2123

Output

x
+
cmd
15
9
8
Advertisements

Demonstration


CodeChef solution FLOW006  - Sum of Digits  CodeChef solution in C,C++

Previous
CodeChef solution BROKENPHONE - Broken Phone Codechef solution in C,C++
Next
CodeChef solution FLOW004 - ,First and Last Digit CodeChef solution in C,C++