wap to input a number using scanner class and check whether the number is special number or not if number is special then print out msg no. is special
Answers
Answered by
3
Hi friend,
The answer is:-
import java.util.*;
public class SpecialNumber
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
boolean specialNumber = special(n);
if (specialNumber) {
System.out.println(n + " is a special number");
} else {
System.out.println(n + " is not a special number");
}
}
public static boolean special(int n) {
int temp = n;
int sum = 0;
while (temp > 0) {
int remainder = temp % 10;
temp = temp / 10;
sum = sum + factorial(remainder);
}
if (n == sum) {
return true;
} else {
return false;
}
}
public static int factorial(int n) {
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
}
return factorial;
}
}
The answer is:-
import java.util.*;
public class SpecialNumber
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = scanner.nextInt();
boolean specialNumber = special(n);
if (specialNumber) {
System.out.println(n + " is a special number");
} else {
System.out.println(n + " is not a special number");
}
}
public static boolean special(int n) {
int temp = n;
int sum = 0;
while (temp > 0) {
int remainder = temp % 10;
temp = temp / 10;
sum = sum + factorial(remainder);
}
if (n == sum) {
return true;
} else {
return false;
}
}
public static int factorial(int n) {
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
}
return factorial;
}
}
Hope it helps!
ambuj4:
Thanks
Similar questions
English,
8 months ago
Social Sciences,
8 months ago
Physics,
1 year ago
English,
1 year ago
Environmental Sciences,
1 year ago
Math,
1 year ago