codechef program #8:Sum of Digits

PROBLEM STATEMENT
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, total number of testcases. Then follow T lines, each line contains an integer N.
Output
Calculate the sum of digits of N.
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ N ≤ 100000
Example
Input 3 12345 31203 2123 Output 15 9 8
My Solution:
import java.util.*;
import java.io.*;
class Main
{
public static void main(String[] args)
{
try{
int i=0,m,n,b=0;
Scanner input = new Scanner(System.in);
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
int q=input.nextInt();
int arr1[]=new int[q];
while (i<q)
{
n=input.nextInt();
while(n!=0)
{
m=n%10;
n=n/10;
b=b+m;
}
arr1[i]=b;
b=0;
i++;
}
for (int k=0;k<arr1.length;k++){
System.out.println(arr1[k]);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
posted by lol ik
Comments
Post a Comment