Coding Practice

Write a Java program to calculate product of digits of a number

Calculate product of digits of a number
Sample Output
Calculate product of digits of a number.

Enter a number: 125
Product: 10
Java-Source Code
//package loopinjava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int number, digit, product = 1;

        Scanner input = new Scanner(System.in);

        System.out.println("Calculate product of digits of a number.\n");

        System.out.print("Enter a number: ");
        number = input.nextInt();

        while (number != 0) {
            digit = (number % 10);
            product = product * digit;
            number = (number / 10);
        }
        System.out.println("Product: " + product);

    }
}
Sample Output
Calculate product of digits of a number.

Enter a number: 192
Product: 18

No comments:

Post a Comment

Change Theme
X