You are here: Home / Topics / Java program to add all the digits of a given positive number

Java program to add all the digits of a given positive number

Filed under: Java on 2024-03-05 21:32:46

import java.util.Scanner;
public class Javaexcercise 
{
   public static void main(String args[])
   {
       int m, n, sum = 0;
       Scanner s = new Scanner(System.in);
       System.out.print("Enter the number:");
       m = s.nextInt();
       while(m > 0)
       {
           n = m % 10;
           sum = sum + n;
           m = m / 10;
       }
       System.out.println("Sum of Digits:"+sum);
   }
}

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.