1. Java program to check whether a number is prime or not
class prime
{
public static void main(String...s)
{
int n,i;
n=Integer.parseInt(s[0]);
for(i=2;i<n;++i)
{
if(n%i==0)
break;
}
if(i==n)
System.out.println("\nNumber is prime");
else
System.out.println("\nNumber is not prime");
}
}
class sum
{
public static void main(String...s)
{
int n,i,sum=0;
n=Integer.parseInt(s[0]);
while(n!=0)
{
i=n%10;
sum+=i;
n/=10;
}
System.out.println(sum);
}
}
3. Java program to find whether a number is armstrong or not
class Armstrong
{
public static void main(String...s)
{
int m,n,i,j=0;
n=Integer.parseInt(s[0]);
m=n;
while(n!=0)
{
i=n%10;
j+=(i*i*i);
n/=10;
}
if(j==m)
System.out.println("\nNumber is armstrong");
else
System.out.println("\nNumber is not armstrong");
}
}
4. Java program to display multiplication table of any number
class Table
{
public static void main(String...s)
{
int n,i;
n=Integer.parseInt(s[0]);
for(i=1;i<=10;++i)
System.out.println(n+" * "+i+" = "+(n*i));
}
}
5. Java program to check whether a given number is palindrome or not
class Palindrome
{
public static void main(String...s)
{
int n,m,rev=0,i;
n=Integer.parseInt(s[0]);
m=n;
while(n!=0)
{
i=n%10;
rev=(rev*10)+i;
n/=10;
}
if(m==rev)
System.out.println("\nNumber is
palindrome");
else
System.out.println("\nNumber is not
palindrome");
}
}
6. Java program to find factorial of given number
{
public static void main(String...s)
{
int n,fac=1,i;
n=Integer.parseInt(s[0]);
for(i=1;i<=n;i++)
fac*=i;
System.out.println("\nFactorial="+fac);
}
}
7. Java program to reverse a given number
class Reverse
{
public static void main(String...s)
{
int n,rev=0,i;
n=Integer.parseInt(s[0]);
while(n!=0)
{
i=n%10;
rev=(rev*10)+i;
n/=10;
}
System.out.println("\nReverse="+rev);
}
}
8. Java program to print fibonacci series
class Fibonacci
{
public static void main(String...s)
{
int first=0,second=1,third,n,i;
n=Integer.parseInt(s[0]);
System.out.print("\n"+first+" "+second);
for(i=2;i<n;i++)
{
third=first+second;
first=second;
second=third;
System.out.print(" "+third);
}
System.out.println();
}
}
0 comments :
Please Enter best of your Comments