Sum of Digits: FLOW006

Codechef Solution: https://www.codechef.com/CUCSE2PP0002/problems/FLOW006

Problem

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

  •  T  1000
  •  N  1000000

Example

Input
3
12345
31203
2123
Output
15
9
8

Sum of Digits CodeChef Solution in C++

// THIS code starts from INT MAIN so copy paste from here only

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int sum = 0;
        while(n!=0)
        {
            int r = n%10;
            sum+= r;
            n /= 10;
        }

        cout<< sum<< endl;
    }

    return 0;
}

Correct Slippers Codechef Solution in Python

# code by PROGIES.IN
# We have populated the solutions for the 10 easiest problems for your support.
# Click on the SUBMIT button to make a submission to this problem.

#Note that it's python3 Code. Here, we are using input() instead of raw_input().
#You can check on your local machine the version of python by typing "python --version" in the terminal.

T=int(input())
for i in range(T):
    num=int(input())
    sum=0
    for j in str(num):
        sum+=int(j)
    print(sum)

Correct Slippers Codechef Solution in Java

// We have populated the solutions for the 10 easiest problems for your support.
// Click on the SUBMIT button to make a submission to this problem.

import java.util.*;
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
        for(int i=0;i< n;i++){
            int a = sc.nextInt();
            int sum = 0 ;
            while(a>0){
            int temp = a%10;
            sum +=temp;
            a/=10;
            }
            System.out.println(sum);
        }
    }
}

Sum of Digits codechef solution by

Sum of Digits codechef solution
Sum of Digits codechef solution
See also  Coldplay: SLOOP