Skip to main content

Program in Java: To accept a number. Display the sum and product of the digits.

 Program in Java: To accept a number. Display the sum and product of the digits.


/**
Write a program accept a number.
Find the sum and product of the digits of the number. 
*/
import java.util.Scanner;
class sumproduct
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number:");
        //to accept a number along with declaring sum and product variables
        int n=sc.nextInt(),sum=0,pro=1;
        while(n>0) //or while(n!=0)
        {
            int d=n%10;
            sum=sum+d;
            pro=pro*d;
            n=n/10;
        }
        System.out.println("Sum of the digits:"+sum);
        System.out.println("Product of the digits:"+pro);
    }
}

Popular posts from this blog