Sum of Digits Test Codechef Solution

Hello Programmers In this post, you will know how to solve the Sum of Digits Test Codechef Solution.

Ezoicreport this adSum of Digits Test Codechef Solution
Sum of Digits Test Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Ezoicreport this adProblem

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

Sum of Digits CodeChef Solutions in Python

n = int(input())
for _ in range(n):
    num = input()
    sum = 0
    for i in num:
        sum += int(i)
    print(sum)

Sum of Digits CodeChef Solutions in CPP

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin>>n;
    while(n--)
    {
     int num,m,sum=0;
     cin>>num;
     while(num>0)
     {
      m=num%10;
      sum+=m;
      num/=10;
     }
     cout<<sum<<"\n";
    }
    return 0;
}

Sum of Digits CodeChef Solutions in JAVA

import java.io.*;
public class Main {
    private static void digitSum() throws IOException
    {
        int digit,sum;
        final BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
        final int t = Integer.parseInt(br.readLine());
        for(int i=0 ; i<t ;i++)
        {
            sum = 0;
            final int n = Integer.parseInt(br.readLine());
            int temp = n;
            for( ;temp>0 ; temp=temp/10)
            {
                digit = temp%10;
                sum+=digit;
            }
            System.out.println(sum);
        }
    }
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        digitSum();
    }
}

Disclaimer: The above Problem (Sum of Digits) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Enormous Input Test Codechef Solution

Sharing Is Caring

Leave a Comment