Category Archives: Java Programs

Java Programs

Java Program to Print Prime Numbers

In this tutorial, we will learn to create a Java program that will print Prime numbers between given number 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
  • Class and Object.
  • Basic Java programming.
  • If-else Statements.
  • Loops in Java
  • While and for Loop.

What Is Prime number?

Any positive Number which is bigger than 1 and it can only  divisible by itself and by 1 and it has maximum of factors is two is called as Prime number.Rather we can say that a prime number is a number which only divided by him or 1.

Java Program to Print Prime Numbers.

In our program we will find that Prime number between the number provided by user using while loop.Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and find is prime number between given number.

Output

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

  • upper= it will hold upper range
  • lower= it will hold lower range.
  • i = for iteration.
  • count = will give us number is prime or not.

And  in the next statement values will be  assigned to variables “upper” and “lower”. And at start value of count set to false default.

After getting the values in  a program we start the loop within the range from lower to upper and find prime number between them.

whenever value of count became “true” means the value of a number is completely dived by itself,then we print the number that divide itself and not by others is a prime number .Then the next number in the loop is checked, till all numbers are checked within the range of lower an upper as  we taken.

So here the result shows prime numbers within  the  range lies are.

Java Program to Swapping Two Numbers without Using a Temporary Variable

Java Program to Swapping Two Numbers without Using a Temporary Variable

In this tutorial, we will learn to create a Java program that will be Swapping Two Numbers without using a Temporary Variable in Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • Arithmetic Operations in Java Programming.

Algorithm:-

Swapping Two Numbers without using a Temporary Variable.

In today’s tutorial, we will learn to create a java program that will swap two numbers. First, we will take the numbers in input from the user. Then we will pass that number to the arithmetic equations to swap the numbers without the third variable. At last, we will print the swapped numbers to the user using the print function.

With the help of this program, we can Swap Two Numbers without using a Temporary Variable.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • x = it will hold the integer value.
  • y = it will hold the integer value.

Taking input numbers from the user.

Swapping the numbers without using the third variable in the program.

Printing the numbers after swapping.

Java Program to Swapping Two Numbers Using a Temporary Variable

In this tutorial, we will learn to create a Java program that will Swap Two Numbers Using a Temporary Variable  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
  • Class and Object.
  • Basic Java programming.

What is meant by Swapping two numbers?

Swapping two numbers means exchanging the values of  the Variables with each other. Example:=> variable a and b has  values a= 5 and b=5 before swapping and after swapping there values became a=6 and b=5.

Java Program to Swapping Two Numbers Using a Temporary Variable

In our program we will swap two numbers value using third temporary variable . We would first declared and initialized the required variables. Next, we would prompt user to input the two values . Later we will Swap their value using temporary Variable.

Output

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

  • a= it will hold First value.
  • b= it will hold second value.

And  in the next statement user will be prompted to enter two values and  which will be assigned to variable ‘a‘ and ‘b‘ respectively.

After that with the help of  third(Temporary)  c variable  we will swap the actual values of a and b .

As you can see in above image first the value of ‘a’ is passes to variable ‘c’ (c=a) and in the next statement value of b passed to a (a=b),and at the end value of c passed to b (b=c).So in that manner using third variable we swapped value of two with third Temporary variable.

And Finally we will print the swapped values of Variables using Temporary(Third) variable.

Java Program to Calculate Compound Interest

Java Program to Calculate Compound Interest

In this tutorial, we will learn to create a Java program that will calculate compound interest using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • Conditional Statements in Java programming.
  • Arithmetic operations in Java Programming.

What Is Compound Interest?

Compound Interest is an interest that obtains by adding the interest of each year to the principal sum and then calculating the interest on the updated amount.

CI is interest earned from the original principal +  accumulated interest.

Formula for Compound Interest is.


Here,
is principle amount.
is the rate in Percent %.
is time in year.

Program Description:-

In this program, First, we will take the rate, time, and the principal amount from the user. Then we will calculate the compound interest of that amount for the time using the mathematical expression. At last, We will print the compound interest from the program.

Below is an example program.

Java Program to Calculate Compound Interest.

Output

In the above program, we have first initialized the required variable.

  • p = it will hold the double principal value from the user.
  • r = it will hold the double rate value from the user.
  • t = it will hold the double-time value from the user.
  • amount  = it will hold the ci value.
  • ci  = it will hold the double value.

Calculating the Compound interest of the program.

Printing the output to the user.

Java Program to Calculate Simple Interest

Java Program to Calculate Simple Interest

In this tutorial, we will learn to create a Java Program to calculate simple interest using java programming.

Prerequisites

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

  • 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 Simple Interest?

Simple Interest is a method used in Banking, Business, and in economic sectors to calculate the interest charges on loans. Formula to calculate Simple interest

Formula:=>

  1. Simple Interest = (P × R × T)/100
  2. Where P = Principal Amount, R = Rate per Annum, T = Time (years)

where:

P is the Principal amount.
R is the rate per annum.
T is time in years.

Algorithm

  • Define variables as  Principal(P), interest(i), and Time of loans in the year(t).
  • Use in the formula.
  • Print the Simple Interest.

Java Program to calculate simple interest

In this program, we will find simple interest of given principal rate and time using java programming. We would first declared and initialized the required variables. Next, we will find simple interest. Lets have a look at the code.

Output

Simple Interest.

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

  • p= it will hold value of principal.
  • r= it will hold value of interest.
  • t =it will hold value of time in year.
  • si= it will hold value of simple interest.

After declaring variables we initiate values in  variables.

now take all the values of Principal rate and time.

after getting all the values we will calculate simple interest using the below  formula.

Now print the simple intereset

Java Program to Perform Addition, Subtraction, Multiplication and Division

Java Program to Perform Addition, Subtraction, Multiplication, and Division

In this tutorial, we will learn to create a Java program that will Addition, Subtraction, Multiplication, and Division using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • Arithmetic operations in Java Programming.

Program to perform the arithmetic operations:-

In this program, we will first take two numbers in input from the user. Then will use the arithmetic operators to perform the operations on that numbers. At last, we will print the output to the user. Using the print function.

The operators are as follows:-

  1. Addition Operator. “+”
  2. Subtraction Operator. “-“
  3. Multiplication Operator. “*”
  4. Divide Operator. “/”

Algorithm:-

Program to Add Subtract Multiply Divide:-

Output:-

In the above program, we have first initialized the required variable.

  • m = it will hold the integer value.
  • n  = it will hold the integer value.

Input number from the user.

Program Logic Code to perform the calculation.

Printing the addition of the program.

Java Program to Generate Random Numbers

 

In this tutorial, we will learn to create a Java Program to Generate Random Numbers  using java programming.

Prerequisites

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

  • 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 random number in java?

In Java we use Math.random() function to generate a pseudorandom number, Number created with a formula that simulates randomness.

Java Program to Generate Random Numbers

In this program we will find random number using random() method and a for loop. We would first declared and initialized the required variables. Next, we will find find random number using random() method .Lets have a look at the code.

Output

Using the Math.random() Method

Output

We can use either methods to print random numbers

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

  • range= it will hold entered max range
  • r=  it will hold random numbers
  • i = for iteration

After declaring variables we initiate values.

How to generate random numbers in Java

  1. first of all Import the class java.util.Random.
  2. create instance of class Random, i.e., Random r = new Random()
  3. generates random numbers in the range 0 to upperbound-1

and within the loop we will print 10 random number within the range given by user.

 Note :=> Every time we get a different output when we execute the program.

Java Program to Count number of Digits In Number

Java Program to Count Number of Digits In Number

In this tutorial, we will learn to create a Java program that will count the number of digits in number in Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • While Loop in Java programming.

Count the number of digits in the number.

In today’s tutorial, we will learn to create a java program that will count the number of digits of the number. First, we will take the number in input from the user. Then we will pass that number to the while loop to count digits. At last, we will print the number of digits in the number.

With the help of this program, we can count the number of digits in numbers.

Program to count the number of digits in numbers:-

Output:-

In the above program, we have first initialized the required variable.

  • flag  = it will hold the integer value.
  • digits = it will hold the integer value for numbers.
  • no = it will hold the integer value of number input.

Taking input number from the user.

Counting the digits of the number.

Printing the numbers of digits from the program.

Java Program to Calculate Sum of Digits

Java Program to Calculate Sum of Digits

In this tutorial, we will learn to create a Java program that will Print the Sum of digits in a given Number using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • While loop in Java programming.
  • Conditional Statements in Java programming.
  • Arithmetic operations in Java Programming.

Program to Print Sum of digits in a given Number

In Java programming, we will make a program in today’s tutorial. We will take integer input from the user and find the Sum of digits in a given number with the help of a program. At last, we will print the sum of all the digits in that number.

With the help of this program, we can Print the Sum of digits in a given number.

Algorithm:-

Program to Print Sum of digits in a given number:-

Output:-

In the above program, we have first initialized the required variable.

  • no = it will hold the integer for the input
  • digit = it will hold the digits of the number
  • sum = it will hold the sum of the digits

Input number from the user.

Program Logic Code.

Printing output for sum the digits.

Java Program to Check Whether a Number is Palindrome or Not

Java Program to Check Whether a Number is Palindrome or Not

In this tutorial, we will learn to create a Java Program that will Check Whether a Number is Palindrome or Not 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.

Java Program to Find Greatest Number

Java Program to Find The Greatest Number

In this tutorial, we will learn to create a Java program that will Find The Greatest Number using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • Conditional statement in Java programming.

Program to Find the Greatest Number

In Java programming, it is possible to take multiple integer inputs from the user and Find the Greatest Number with the help of a very short program. The Java language has many types of header libraries which has supported function in them, with the help of these files the programming is easy. But today we will Find the Greatest Number from the taken numbers from the user using Java programming.

Algorithm:-

With the help of this program, we can Find the Greatest Number.

Program to Find the Greatest Number:-

Output:-

In the above program, we have first initialized the required variable.

  • x= it will hold the first integer value.
  • y = it will hold the second integer value.
  • z = it will hold the third integer value.

Input numbers for the program.

Program Logic Code.

Printing output for the greatest number.

Java Program to Generate Fibonacci Series

Java Program to Generate Fibonacci Series

In this tutorial, we will learn to create a Java program that will Generate Fibonacci Series using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • Conditional Statements in Java programming.
  • Arithmetic operations in Java Programming.

Program to generate the Fibonacci series:-

In this program, we will, first, declare the variables for the program. Then we will assign the values to the variables. Now we will use the user-defined function to generate the Fibonacci series to the endpoint using the arithmetic expression. At last, we will print the output Fibonacci series to the user.

With the help of this program, we can Generate Fibonacci Series.

Algorithm:-

Program to Generate Fibonacci Series:-

Output:-

In the above program, we have first initialized the required variable.

  • flag = it will hold the integer value for the length of the series.

Input message for the user for the integer value.

Program Logic Code.

Printing output Fibonacci Series.