Category Archives: Java Number Programs

Java Number Programs

Program to check Harshad Number In Java

In this tutorial, we will learn to create a Java Program to check Harshad  Numbers in Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Harshad number

Harshad number is a number that if it is divisible by the sum of its digit.

For example:=> 40, Sum of digit is (4 +0 = 4). So 40 is divisible by 4. So, 40 is a Harshad number.

Program to check Harshad Number in Java

In this program we would find given number is a Harshad  number or not .first of we would take a value from user and find the Harshad  number.let’s  have a look at the code.

Output

Harshad Number.

Not a Harshad Number.

In the above program, we have first declared and initialized a set variables required in the program.

  • no and number= it will hold entered number.
  • remainder = it will hold remainder of values.
  • sum = it will hold sum of digit

After that we take a number from user and find given number is a Harshad number or not

Algorithm

  1. First of all we take and initialize variable number.
  2. Duplicate the number in variable no.
  3. check Condition while number is greater than 0, then find remainder of number by dividing number with 10.

  1. Add remainder to a variable sum=>(sum = sum + remainder).
  2. Divide number by 10.
  3. Repeat the steps 3 to 5,

If no divide sum completely, then number is a Harshad number. Else, given number is not a Harshad number.

Program to check Pronic Number in Java

In this tutorial, we will learn to create a Java Program to check Pronic Numbers in Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Pronic or Heteromecic  number?

A number is called a Pronic or Heterimecic if product or two consecutive number giver the number itself.

Example :

1) 42 => 6 * 7 => gives 42 6 and 7 are  consecutive  number.

2) 50 => 10 * 5 =: gives 50 but 5 and 10 are not consecutive  number.

Program to check Pronic Number in Java

In this our program we will find given number is a Pronic number or not in using java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the a number.Later we  find number is Pronic number or not let’s have a look at the code.

Output

Pronic Number.

Not a Pronic Number.

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number.
  • result= it will hold result.
  • i= i for iteration .

After declaring variables in the next statement user will be prompted to enter a value and  which will be assigned to variable ‘number’.

And after that we calculate the product of two consecutive number with in the number gives the number itself then the number is a pronic.

So the given number called Pronic if “sum of its two consecutive number gives the number” Called Pronic

If not then the number is not a Pronic.

Program to check Disarium Number In Java

In this tutorial, we will learn to create a Java Program to check Disarium Numbers in Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.
  • Inbuilt functions.

What is In  Disarium Numbers?

A number is called Disarium number if sum of its power at the positions from left to right is equal to the number.

Example:
11 + 32 + 53 = 1 + 9 + 125 = 135

Program to check Disarium Number In Java

In this program we will find given number is a Disurium number or not in using java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the a number.Later we  find number is Sisarium number or not let’s have a look at the code.

Output

Disarium Number

Not a Disarium Number

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number
  • temp= it will hold temporary origianal number
  • sum= it will hold sum of values
  • l= it will hold length of a number

After declaring variables in the next statement user will be prompted to enter a value and  which will be assigned to variable ‘number’.

And after that we calculate the value Sum of its digits powered with their respective position is equal to the original number or not using Math.pow(a,l). inbuilt function

So the given number called Disarium if “sum of its digits powered with their respective position are equal” with the number.

Number is Disarium if condition True.

If not it is not a Disarium Number.

Program to check Strong Number In Java

In this tutorial, we will learn to create a Java Program to check Strong Numbers in Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Strong Number

A strong number is a number which sum of the factorial of the digits is equal to the number itself.
Example:-
145 = 1! + 4! + 5! = 1 + 24 + 120 = 145
So we can say that the 145 is a strong number.

Program to check Strong Number In Java

In our program we will find given number is a strong number or not in java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the age value .Later we calculate the age is valid for vote or not let’s have a look at the code.

Output

Strong number.

Not a strong number.

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number
  • shrt= it will hold value person short by how much.

After declaring variables in the next statement user will be prompted to enter a value and  which will be assigned to variable ‘number’.

And after that we calculate the value given number is strong or not by passing the number to user define function Strong(number) .

within the function here first of all we are going to find factors of given number and also the factorial of each factor of a number.

after that we going add the values and check if given number and sum of factor of given area same as shown in image

If both are same number is strong

If not Not a strong number

Program to check Perfect Number In Java

In this tutorial, we will learn to create a Java Program to check Perfect Numbers in Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Perfect Number.

A number is called Perfect number if Sum of factors (not include the number itself) is equal to the number is called a perfect number.

For example:=>
Number = 6.
Divisors of 6 are 1, 2 and 3.
Sum = 1+2+3 = 6 = 6
⇒ So 6 is a perfect number.

Program to check Perfect Number in Java

In this program we would find given number is a Perfect number or not .first of we would take a value from user and find the Perfect number.let have a look at the code.

Output

Perfect  Number.

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number.
  • sum=   it will hold sum of factors.

After that we take a number from user and find given number is a Perfect  number or not

after taking value from user check condition for number is perfect or not.

where we first check that number must be grater than one,after that we find the factor of a number and add them and check if sum of factor equals to number then it is a perfect number.

if not not a perfect number.

Program to check Palindrome Number In Java

In this tutorial, we will learn to create a Java Program to Check Palindrome Number In Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Palindrome Number.

A number is called a  Palindrome Number  if number  is same after reverse the number it self.

121 <=> 121

323 <=> 323

656 <=>656.

Algorithm  to find Palindrome Number

  1. Get the number from user to check  condition for palindrome
  2. pass  the number in temporary variable.
  3. Reverse the number.
  4. Check  Temporary number with reversed number.
  5. If both numbers are same number is “Palindrome number”.
  6. Else not palindrome.

Program to check Palindrome Number In Java

In this program we would find given number is a Palindrome number or not .first of we would take a value from user and find the Palindrome number.let have a look at the code.

Output

Palindrome number

Not a palindrome  Number.

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number.
  • sum=   it will hold sum of factors.

After that we take a number from user and find given number is a Palindrome number or not.

Now we will run the loop until (number>0)

  • Here we used a while loop to reverse number and store the reversed number in sum.
  • Then we will check entered number is equal to the reverse number.

  • If Number is equal to reverse number (number==sum) then number is palindrome.

If not number is not a Palindrome.

Program to check Magic Number In Java

In this tutorial, we will learn to create a Java Program to check Magic Numbers in Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Magic Number.

A number is called a Magic number  if sum of its digits repeated calculated till a single digit , If single digit end at 1 then the number is a magic number.

For example:-> 199 is a magic number because 1+9+9 = 19 = 1+9 = 10 = 1+0 = 1.

Program to check Magic Number In Java

In this program we would find given number is a Magic number or not .first of we would take a value from user and find the Magic number.let have a look at the code

Output

Magic Number.

Not a Magic Number.

In the above program, we have first declared and initialized a set variables required in the program.

  • no,number= it will hold entered number.
  • Sum=   it will hold result of calculation.

After that we take a number from user and find given number is a Magic number or not.

Now we will run the loop until(no>0 || sum>9)

  • Here we will loop till a single digit is obtained by recursively adding the sum of its digits.

  • Then we will check last digit of their sum is equals to one (sum==1) if it is one number is a Magic number.

  • if not it is not a Magic Number.

Program to check Twin prime In Java

In this tutorial, we will learn to create a Java Program to check Twin prime In Java using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Twin Prime Number.

A Number is called a Prime if it is Divisible by itself and Twin Prime number having a prime gap of two(2) is referred to as a twin prime number.

For Example:->

  1. 29 and 31 are Twin Prime Number.(31-29=2).
  2. 3 and 5 are Twin Prime Number.(5-3 =2).
  3. 71 and 73 are Twin Prime Number.(73-71 =2).

Program to check Twin prime In Java

In this program we would find given numbers are Twin prime number or not .first of we would take a value from user and find the Twin prime .let have a look at the code

Output

Twin Prime number

Not a Twin Prime number.

In the above program, we have first declared and initialized a set variables required in the program.

  • number = it will hold entered numbers
  • num,num1,no1 and no2=   it will hold entered numbers.

After that we take a number from user and find given number is a Twin Prime number or not.

After taking two prime number we pas this number to Prime() function where we calculate is there difference or prime number is 2 if yes.

Function returns true

else return False.

Java Program to Find HCF and LCM of Two Numbers

In this tutorial, we will learn to create a Java Program to Find HCF and LCM of Two Numbers using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is LCM Number.

LCM is a method to finding the smallest possible multiplication of two or more numbers. LCM of a numbers is divisible by both or more numbers. For example, LCM of 5 ,4 and 20 is 20.

What is HCF Number.

GCD(Greatest Common Divisor) or HCF (Highest Common Factor) . When  we finding the factor of two numbers, Highest Common Factor of  numbers is the Largest factor that divides these two numbers. The greatest factor found from the common factors is called the HCF. It is also called the Greatest common factor.

Java Program to Find HCF and LCM of Two Numbers.

In this program we would find HCF and LCM of given numbers  .first of we would take a value sfrom user and find then we will find HCF and LCM of given numbers.

Output

Hcf and Lcm

In the above program, we have first declared and initialized a set variables required in the program.

  • no1,no2= it will hold entered numbers.
  • t1and t2= it will hold temp values.
  • lcm = it will hold result of lcm
  • Hcf = it will hold result of hcf.

After that we take a numbers from user and find HCF and LCM of given numbers.

after that we calculate hcf and lcm of given number as shown in image below

And finally we will print both hcd and lcm of given numbers.

Java Program to Find average of 3 numbers

In this tutorial, we will learn to create a Java Program to Find average of 3 numbers using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is average of numbers.

Average of numbers is by adding up all the numbers and dividing by size of numbers are in the set.

Java Program to Find average of 3 numbers

In this program we would find average of given number.first of we would take a values from user and find the average of  numbers.let have a look at the code.

Output

Average of Three Numbers.

In the above program, we have first declared and initialized a set variables required in the program.

  • number,number1 and number2 = it will hold entered numbers.

After that we take a numbers from user and find average of numbers.

after getting values from users we pass the number to function avg().

In this Function we take three parameter as a arguments and add all three values and divide the sum by 3 to get the desire output and return the average to main function and where we will print the result of program.

 

Above image is the output of above program.

Java Program to display odd numbers from 1 to n or 1 to 100

In this tutorial, we will learn to create a Java Program to display odd numbers from 1 to n or 1 to 100 using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Odd Number.

A number is  called odd that number cannot be divided exactly into pairs or we can say odd numbers, when divided by 2, gives remainder 1. (5%2==1).

Java Program to display odd numbers from 1 to n or 1 to 100

In this program we would print all odd number between “1 to n or 1 to 100” .first of we would take a value from user and find the Odd number between given numbers .let have a look at the code.

Output

Odd number between 1-100.

Odd number between 1-N. Value of n is 50.

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number.
  • i=   i for iteration.

After that we take a number from user and find odd number between the given number.

Here we can use  for loops to display odd numbers:

  • Using  for Loop
  • Using if-else Statement.

In order to check  odd number, we divided the number from 1 to N by 2 if it leaves a remainder of 1,then the number is odd

and the print the values.. As shown in image above.

Java Program to display even numbers from 1 to n or 1 to 100

In this tutorial, we will learn to create a Java Program to display even numbers from 1 to n or 1 to 100 using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is even Number.

A number is called a even that completely divided into pairs or we can say even numbers, when divided by 2, gives remainder (0)Zero. . (4%2==0).

Java Program to display even numbers from 1 to n or 1 to 100

In this program we would print all even number between “1 to n or 1 to 100” .first of we would take a value from user and find the even number between given numbers .let have a look at the code.

Output

even number between 1-100.

even number between 1-N. Value of n is 50.

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold entered number.
  • i=   i for iteration.

After that we take a number from user and find even number between the given number.

Here we can use  for loops to display even numbers:

  • Using  for Loop
  • Using if-else Statement.

In order to check  even number, we divided the number from 1 to N by 2 if it leaves a remainder of 0 zero,then the number is even.

and the print the values.. As shown in image above.