Hello coders, today we are going to solve Sum of Digits Test Codechef Solution. Which is a part of Codechef Solution.

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
- 1 <= T <= 1000
- 1 <= N <= 1000000
Example
Input:
3 12345 31203 2123
Output:
15 9 8
Sum of Digits Test Codechef Solution 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 Test Codechef Solution 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 Test Codechef Solution 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 Chase2learn.This tutorial is only for Educational and Learning purpose.