Write a program to calculate the sum of digits of a given integer no.

Write a program to calculate the sum of digits of a given integer no.




import java.util.Scanner;

class SumOfDigits

{

public static void main(String[] args) 

     {

       int n,d,sum=0;

       Scanner scr=new Scanner(System.in);

       System.out.println("Enter any three digit number ");

       n=scr.nextInt();

       while(n>0)

       {

        d=n%10;

        sum=sum+d;

        n=n/10;

       }

       System.out.println("Sum of digits = " +sum);

    }

}

Output




Post a Comment

0 Comments