Factorial
of a given Number.
Factorial
of a number is the product of all number from 1 to 'n'.
Ex.
Factorial of 5!=5*4*3*2*1 = 120.
import
java.io.*;
class Factorial
{
public static void main(String
args[])throws IOException
{
BufferedReader Br=new
BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("\tEnter a No : ");
n=Integer.parseInt(Br.readLine());
Factorial Ft=new
Factorial();
long F=Ft.Fact(n);
System.out.println("\n\tFactorial of '"+n+"' = "+F);
}
long Fact(int n)
{
long F=1;
for(;n>1;n--)
{
F*=n;
}
return F;
}
}
Output
Enter a No : 5
Factorial of '5' = 120
Perfect
No is a number where sum of all factor of a number equals to the number itself.
Ex.
6 = (1+2+3+6)/2 = 6 or 1+2+3 = 6
import
java.io.*;
class Perfect
{
public static void main(String
args[])throws IOException
{
BufferedReader Br=new
BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("\tEnter a No : ");
n=Integer.parseInt(Br.readLine());
Perfect P = new Perfect();
if(P.Sum_Factor(n)==n)
{
System.out.println("\n\t '"+n+"' is a Perfect No");
}
else
{
System.out.println("\n\t '"+n+"' is not a Perfect
No");
}
}
int Sum_Factor(int n)
{
int s=0;
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
{
s+=i;
}
}
return s;
}
}
Output
Enter a No : 6
'6' is a Perfect No
Enter a No : 12
'12' is not a Perfect No
Prime
No is the numbers that divided by 1 and itself or prime numbers only can have
two factors.
Ex.
5 is only divisible by 1 and 5.
import
java.io.*;
class Prime
{
public static void main(String
args[])throws IOException
{
BufferedReader Br=new
BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("\tEnter a No : ");
n=Integer.parseInt(Br.readLine());
Prime Pr=new Prime();
if(Pr.isPrime(n)==0)
{
System.out.println("\n\t"+n+" is not Prime");
}
else
{
System.out.println("\n\t"+n+" is Prime");
}
}
int isPrime(int n)
{
int i;;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
}
Output
Enter a No : 23
23 is Prime
Enter a No : 54
54 is not Prime
No comments:
Post a Comment