2
import java.io.*;
class specialPrint
{
static int fact(int a)
{
int f=1;
for(int i=1;i<=a;i++)
f*=i;
return f;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int d,i,k;
int s1=0;
int s;
System.out.println("Enter the lower range:-");
int m=Integer.parseInt(br.readLine());
System.out.println("Enter the upper range:-");
int n=Integer.parseInt(br.readLine());
for(i=m;i<=n;i++)
{
k=i;
while(k!=0)
{
d=k%10;
s=fact(d);
s1=s1+s;
k=k/10;
}
if(s1==i)
System.out.println(i);
}
}
}
class specialPrint
{
static int fact(int a)
{
int f=1;
for(int i=1;i<=a;i++)
f*=i;
return f;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int d,i,k;
int s1=0;
int s;
System.out.println("Enter the lower range:-");
int m=Integer.parseInt(br.readLine());
System.out.println("Enter the upper range:-");
int n=Integer.parseInt(br.readLine());
for(i=m;i<=n;i++)
{
k=i;
while(k!=0)
{
d=k%10;
s=fact(d);
s1=s1+s;
k=k/10;
}
if(s1==i)
System.out.println(i);
}
}
}
Abhishek01
Jun 12, '14
1 answers
1
Hi Abhishek,
You just missed one single statement. Rest of your code is correct. You are adding the sum in the variable s1. Now this s1 should be re-initialized every time you enter the while loop. So, just before the while loop, add s1 = 0, and it'll work fine.
Also, these numbers are called factorions. Check this wiki page for reference: http://en.wikipedia.org/wiki/Factorion
khanmansoor
Jun 24, '14
Log in to your account to answer this questions.