304 North Cardinal St.
Dorchester Center, MA 02124

# Sum of Digits CodeChef Solution

## Problem – Sum of Digits CodeChef Solution

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.

•  T  1000
•  N  1000000

### Example

``````Input: 3
12345
31203
2123
Output: 15
9
8``````

### Sum of Digits CodeChef Solution in Pyth 3

``````T = int(input())
res = []
for i in range(T):
inpLine = input()
r = 0
for j in inpLine:
r += int(j)
res.append(r)

print(*res, sep="\n")``````

### Sum of Digits CodeChef Solution in C++17

``````#include<bits/stdc++.h>
#define pf printf
#define sf scanf
#define db double
#define z long long int
using namespace std ;
#define MAXN 100001
#define xx 12345678901
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
z i,j;
int main()
{

fast;
z t;
cin>>t;
while(t--)
{
z ans=0;
string s;
cin>>s;
for(i=0;i<s.length();i++)
{
ans+=(s[i]-'0');
}
cout << ans <<endl;
}
return 0;
}``````

### Sum of Digits CodeChef Solution in Java

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

``````
##### Sum of Digits CodeChef Solution Review:

In our experience, we suggest you solve this Sum of Digits CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Sum of Digits CodeChef Solution

Find on CodeChef

##### Conclusion:

I hope this Sum of Digits CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions