accept a number and check if the number is a special two digit number or not a number is said to be special if the sum of the digit when added to the product of its digit the result is equal to the original two digit number example is equal to n is equal to 15 9 sum is 5 + 914 product is 5 *9=45 total 14 + 45=59 Java program
Answers
Correct Question:
- WAP in Java to accept a number and check whether it is a Special two-digit number or not.
What is Special Number?
- A number is said to be a special number when the sum of the digit added ri the product of the digit is equal to the original number.
- Example: 59, 5+9=14 and 5×9=45, So, 14+45 = 59.
//Here you go!
import java.util.Scanner;
class Special_Num{
public static void main (String ar[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number : ");
int n=sc.nextInt();
int s=0,p=1;
if(n>9&&n<=99){
for(int N1=n;N1!=0;N1/=10){
s+=N1%10;
p*=N1%10;
}
if((s+p)==n)
System.out.println("Number is a Special two-digit number!");
else
System.out.println("Number is not a Special Two-digit number.");
}
else {
System.out.println("Number is not a two-digit number");
}
}
}
Explanation:-
- As mentioned in the question that we have to check for 2-digit Special number, so before proceeding with the main logic of the program we must put a condition that a number must be two-digit number.
- Now we can proceed with main logic where we are extracting each digit and adding that to variable s (initialised as 0) and multiplying that same digit with variable p (initialised as 1).
- Finally, we will add s and p to check if it is orignal number? Yes? then a special number otherwise not.
- Hurray! You made it!
Note:-
- You may use while loop or do-while loop instead of for loop.
- Do not copy the exact cöde given above as there are some empty characters used for showing indentation.