Sum of Digits in anInteger

 Sum of Digits in anInteger


Problem Description

Write a Java program and compute the sum of the digits of an integer.

Note:

Use Long Data Type
Logic Test Case 1

Input (stdin)
2512

Expected Output

10
Logic Test Case 2

Input (stdin)
4596



CODE AREA

import java.io.*;

import java.util.*;

public class TestClass {

public static void main(String[] args) { 

Scanner g=new Scanner(System.in);

       int n,s=0;

       n=g.nextInt();

       while(n!=0)

       { 

         s+=n%10;

       n=n/10;

         

       }

       System.out.println(s);

}

}

Post a comment

0 Comments