how to learn Java Program of special no
Answers
Answered by
1
HEYY THERE!
First of all, what is a Special number?
A number is special when the factorials of the digits of the number is equal to the number itself.Eg = 145, where 1! + 4! + 5! = 1 + 24 + 120 = 145
Here's your program code:
import java.util.*;
public class SpecialNumber
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int n, r, s = 0;
System.out.println("Enter the number: ");
n = sc.nextInt();
int p = n;
SpecialNumber obj = new SpecialNumber();
while (n > 0)
{
r = n % 10;
s = s + obj.fact(r);
n = n / 10;
}
if (s == p)
{
System.out.println("It is a special number");
}
else
{
System.out.println("It is not a special number");
}
public int fact (int n)
{
int i = 2, fn = 1;
for (i = 2; i <= n; i++)
{
fn = fn * i;
}
return fn;
}
}
I have used a method/function to do the program.
I have used Scanner class.
THANKS!
First of all, what is a Special number?
A number is special when the factorials of the digits of the number is equal to the number itself.Eg = 145, where 1! + 4! + 5! = 1 + 24 + 120 = 145
Here's your program code:
import java.util.*;
public class SpecialNumber
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int n, r, s = 0;
System.out.println("Enter the number: ");
n = sc.nextInt();
int p = n;
SpecialNumber obj = new SpecialNumber();
while (n > 0)
{
r = n % 10;
s = s + obj.fact(r);
n = n / 10;
}
if (s == p)
{
System.out.println("It is a special number");
}
else
{
System.out.println("It is not a special number");
}
public int fact (int n)
{
int i = 2, fn = 1;
for (i = 2; i <= n; i++)
{
fn = fn * i;
}
return fn;
}
}
I have used a method/function to do the program.
I have used Scanner class.
THANKS!
RiyaWani:
OK thanks
Similar questions