List of java numbers programs with easy explanation

In this tutorial ,we will learn about some important programs related to numbers.It is basic programs which is asked by the interviewer for both the fresher and experienced candidates. 

1.Write a program of palindrome number in java

public class Palindrome
{
Public static void main(String[ ] args)
{
int number=141,rev=0,rem,temp;
temp=number;

while(number>0)
{
rem=number%10;
rev=rev*10+rem;
number=number/10;
}
if(rev==temp)
{
System.out.println(“Numer is a palindrome”);
}
else
{
System.out.println(“Number is not a palindrome”)
}
}
}

Output

Number is a palindrome

2.Write a program of sum of natural numbers in java

The positive integer numbers 1,2,3,and 4…….. are known as natural numbers.

public class NaturalNumber
{
public static void main(String[ ] args)
{
int num=5,count=1,sum=0;
while(count<=num)
{
sum=sum+count
Count++;
}
System.out.println(“sum of 5 numbers=”+sum);
}
}
}

Output:

sum of 5 numbers=15

3.Write a program of Armstrong number in java

Example

153=1^3  + 5^3 + 3^3

If  we take three digits then sum of cube of every individual digit is equal of that numbers then we can say given number is Armstrong number.

public class ArmstrongNumber
{
Public static void main(String[ ] args)
{
int sum=0,rem,temp,number=153;
temp=number;
while(number>0)
{
rem=number%10;
sum=sum+rem*rem*rem;
number=number/10;
}
if(sum==temp)
{
System.out.println(“Number is Armstrong”);
}
else
{
System.out.println(“Number is not Armstrong”);
}
}
} 

Output:

Number is Armstrong

4.Write a program to check whether given number is positive or negative in java

public class PositiveOrNegative
{
public static void main(String[ ] args)
{
Int number=10;

if(number>0)
{
System.out.println(“Number is positive”);
}
else if(number<0)
{
System.out.println(“Number is negative”);
}
else
{
System.out.println(“Number is neither positive or negative”);
}
}
}

Output:

Number is positive

5.Write a program to generate Random number in java

public class RandomNumber
{
public static void main(String[ ] args)
{
int number;
Random random=new Random( );
System.out.println(“Random numbers……”);

for(number=1;number<=4;number++)
{
System.out.println(random.nextint(200));
}
}
}

Output

Random numbers…..

200

445

555

243

6. Write a program  sum of digits of a given number in java

public class SumOfDigit
{
public static void main(String[ ] args)
{
Int sum=0,rem,number=1234;

while(number>0)
{
rem=number%10;
sum=sum+rem;
number=number/10;
}
System.out.println(“sum of digits=”+sum);
}
}
}

Output:

sum of digits=10

7.Write a program to Reverse the number in java

Public class ReverseNumber
{
public static void main(String[ ] args)
{
int number=1234,rev=0,rem;
while(number>0)
{
rem=number%10;
rev=rev*10+rem;
}
System.out.println(“Reverse number is=” +rev);
}
}

Output:

Reverse number is=4321

8.Write  a program to check whether given number is prime or not in java

public class PrimeNumber
{
Public static void main(String[ ] arg)
{
int number=31,i=2;
Boolean flag=false;

while(i<=number/2)
{
if(number%i==0)
{
flag=true;
Break;
}
I++;
if(!flag)
{
System.out.println(“Number is a prime”);
}
else
{
System.out.println(“Number is not prime”);
}
}
}

Output

Number is prime

Hope,I explained all the programs step by step.it will help you to crack interview for both fresher and experienced candidate.If you have any query the comment me.

Thanks and Regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *