Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

java program to find the sum of digits of a given number taking input from command line

0 votes
asked Oct 25, 2021 by SWATI SAXENA (120 points)

2 Answers

+1 vote
answered Oct 30, 2021 by Peter Minarik (55,760 points)
Please, share your attempt to solve the problem and we'll be happy to review your code.
0 votes
answered Oct 30, 2021 by Mammadova Aysel (140 points)
import java.util.Scanner;

class NewClass {
    public static int sumofDigits(int n){
        int sum=0;
        while(n!=0){
            int remainder = n % 10;
            sum += remainder ;
            n=n/10;
        }
        return sum;
    }
    public static void main(String args[] ) throws Exception {
        
       
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println("sum of digits : "+sumofDigits(n));
     
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...