Search This Blog
Subtract the Product and Sum of Digits of an Integer using Java (LeetCode: 1281)
By
Code and Concepts
DSA
·
java
·
leetcode
·
Problem Solving
👆 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:
- The first line of the code imports the Scanner class from the java.util package, which is used to read input from the user.
- The "Main" class is defined, which contains the main() method that serves as the entry point for the program.
- 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.
- The user is prompted to enter a number using the System.out.println() method, which prints a message to the console.
- The nextInt() method of the Scanner class is called to read an integer input from the user and store it in the num variable.
- 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.
- A while loop is used to iterate over each digit of the input number. The loop continues until the num variable becomes 0.
- Inside the loop, the modulus operator (%) is used to obtain the rightmost digit of the num variable, which is stored in the singleDigit variable.
- The sum variable is incremented by the value of singleDigit, and the prod variable is multiplied by the value of singleDigit.
- The num variable is divided by 10 using the /= operator, which removes the rightmost digit from the num variable.
- 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.
🔥Checkout my Socials:
🔥GitHub Repository:
- 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
Admin LinkedIn
Useful for You
Search from google
Popular uploads
-
Subtract the Product and Sum of Digits of an Integer LeetCode Problem no. 1281 🔥 Checkout live: 👆 Replit Link: click here 👉 LeetCode p...
-
Data Structures and Algorithms - B Tech - Mumbai University Trees - BTech (Handwritten Study/Revision Notes) 🔥 This PDF File Include: Tr...
-
Data Structures and Algorithms - B Tech - Mumbai University Graphs - BTech (Handwritten Study/Revision Notes) 🔥 This PDF File Include: Gra...
No comments:
Post a Comment