Search This Blog

Subtract the Product and Sum of Digits of an Integer using Java (LeetCode: 1281)


Subtract the Product and Sum of Digits of an Integer

LeetCode Problem no. 1281


🔥Checkout live:

👆 Replit Link: click here
👉 LeetCode problem: click here

Code: 

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the number: ");
    int num = sc.nextInt();
    int sum = 0, prod = 1;

    while (num != 0) {
      int singleDigit = num % 10;
      sum += singleDigit;
      prod *= singleDigit;
      num /= 10;
    }
    System.out.println("The sum is: " + sum);
    System.out.println("The product is: " + prod);
    int ans = prod - sum;
    System.out.println("The answer is: " + prod + "-" + sum + "=" + ans);
  }
}

Code Explanation: 

This Java code is an implementation of a program that reads an integer input from the user and performs some arithmetic operations on it. Here is a step-by-step explanation of the code:
  1. The first line of the code imports the Scanner class from the java.util package, which is used to read input from the user.
  2. The "Main" class is defined, which contains the main() method that serves as the entry point for the program.
  3. Inside the main() method, a new Scanner object is created to read input from the user. The System.in parameter passed to the Scanner constructor indicates that input should be read from the standard input stream, which is typically the keyboard.
  4. The user is prompted to enter a number using the System.out.println() method, which prints a message to the console.
  5. The nextInt() method of the Scanner class is called to read an integer input from the user and store it in the num variable.
  6. Two new variables sum and prod are initialized to 0 and 1, respectively. These variables will be used to store the sum and product of the digits of the input number.
  7. A while loop is used to iterate over each digit of the input number. The loop continues until the num variable becomes 0.
  8. Inside the loop, the modulus operator (%) is used to obtain the rightmost digit of the num variable, which is stored in the singleDigit variable.
  9. The sum variable is incremented by the value of singleDigit, and the prod variable is multiplied by the value of singleDigit.
  10. The num variable is divided by 10 using the /= operator, which removes the rightmost digit from the num variable.
  11. After the loop completes, the sum, prod, and ans variables are printed to the console using the System.out.println() method. The ans variable is calculated by subtracting the value of sum from prod.
Overall, this program reads an integer input from the user, calculates the sum and product of its digits, and then prints the sum, product, and the difference between the product and the sum to the console. 

🔥Checkout my Socials:

  1. GitHub
  2. LinkedIn
  3. Replit
🔥GitHub Repository:
  1. DSA using JAVA practice: A comprehensive repository containing my lecture notes (by Kunal Kushwaha) and programs for Data Structures and Algorithms, challenging assignments, and curated LeetCode questions to enhance my coding skills

No comments:

Post a Comment

Admin LinkedIn

Useful for You

Search from google

Popular uploads