Category Archives: Java Basic Programs

Java Basic Programs

Create Simple Mortgage Calculator In Java

In this tutorial, we will learn to create a Java Program to Create Simple Mortgage Calculator 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.

Mortgage Payment

Know as the amount of money that you need to return to the person,where you have your property mortgaged.

Formula to calculate Mortgage is:

“M = P [{r(1+r)^n}/{(1+r)^n – 1}]”

where
M = Monthly payment
P = principal
r = rate
n = number of payments

Create Simple Mortgage Calculator In Java

In this our program we will create a simple Mortgage Calculator. We would first declared and initialized the required variables. Next, we would prompt user to input value.Later in the program we will find Mortgage.

Output

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 rate
  • t= it will hold time in a year.
  • principal= calculate payment of month.

In the next statement user will be prompted to enter the values of p,r and and later we will fine mortgage.

Here we will to use following formula “principal= (p * r) / (1 – Math.pow(1 + r, -t))” and Math.pow() function which is define in Java standard library.it will take two parameter as a arguments p,r and t values supply to it.

After calculating Mortgage value will be stored in  variable principal.

Finally we will print the Mortgage of given values.

Java Program to Find Distance Between 2 Points

In this tutorial, we will learn to create a Java Program to Find Distance between 2 point 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.

Distance Between two Points

Using Pythagorean theorem to find the distance between two points (x1, y1) and (x2, y2), all we need are the coordinates in pairs and apply the formula.

Distance Formula:

Distance:- √((x2 – x1)2 + (y2 -y1)2).

Assume :=> We have two points A and B, with coordinates (x1, y1) and (x2, y2) respectively. So their distance  can be represented as AB and it can be calculated as.

The first point (A):- (x1, y1)
Second point (B):- (x2, y2)

Java Program to Find Distance Between 2 Points

In this our program we will create a program to find Distance Between two points. We would first declared and initialized the required variables. Next, we would prompt user to input value.Later in the program we will find Distance between points.

Output

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

  • x1 = it will hold value of first coordinates
  • y1 = it will hold value of first coordinates
  • x2 = it will hold value of second coordinates
  • y2 = it will hold value of second coordinates
  • x= it will represent distance.
  • y= it will represent distance.
  • distance= it will display final result.

the Formula to find the distance between two points is:

To calculate the distance between two points “(x1,y1) and (x2,y2) or √ ( x )2+( y )2 . All you need is use the coordinates formula shown below.

Here we will to use following formula “ Math.sqrt(x*x+y*y))” and Math.sqrt() function which is define in Java standard library.it will take two parameter as a arguments x and y values supply to it.And in return gives the required output.

As shown in image above.

Java Program to swap two Numbers

Java Program to Swap two Numbers

In this tutorial, we will learn to create a Java program that will Swap Two Numbers 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.

Program to Swap the Numbers:-

As we all know, Java is a very powerful language. With the help of the Java programming language, we can make many programs. We can perform many input-output operations using Java programming. In today’s tutorial, we take the input numbers from the user. Then we will swap the Numbers with the help of operators in the Java programming language.

With the help of this program, we can swap the Numbers.

Algorithm:-

Program to Swap to Numbers using a third variable:-

Output:-

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

  • first= it will hold the integer value.
  • second = it will hold the integer value.
  • flag = it will hold the integer value.

Taking the input number 1 and 2 from the user.

Swapping the numbers.
Printing the output numbers.

Java Program to Calculate Average of 3 Numbers

Java Program to Calculate Average of 3 Numbers

In this tutorial, we will learn to create a Java program that will Calculate the Average of 3 Numbers 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 operator in Java programming.

What is the average of the numbers?

The average of numbers in mathematics means that all the numbers are added and then the sum of all the numbers is divided by the total number of objects.

It means if there are 4 numbers as follows {2,4,6,8} then the average will be:-

average = 2+4+6+8/4

average = 5.

Algorithm:-

Program to Calculate the average of a Number

In this program, we will take three numbers in input from the user. Then we will add that number and store it in a variable as a sum. After that, we will divide the sum of those numbers by 3 to find the average. At last, we will print the average of 3 numbers.

With the help of this program, we can find the average of three numbers.

Program code:-

Output:-

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

  • no1 = it will hold the integer value.
  • no2  = it will hold the integer value.
  • no3  = it will hold the integer value.
  • sum = it will hold the integer value.
  • avg = it will hold the float value.

Taking input numbers from the user to for the average of two numbers.

Calculating the average of those numbers.

Printing output average for that given number.

Java Program to Calculate Average of Two Numbers

Java Program to Calculate Average of Two Numbers

In this tutorial, we will learn to create a Java program that will Calculate the Average of Two Numbers 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 operator in Java programming.

What is the average of the numbers?

The average of numbers in mathematics means that all the numbers are added and then the sum of all the numbers is divided by the total number of objects.

It means if there are 4 numbers as follows {2,4,6,8} then the average will be:-

average = 2+4+6+8/4

average = 5.

Algorithm:-

Program to Calculate the average of a Number

In this program, we will take two numbers in input from the user. Then we will add that number and store it in a variable. After that, we will divide the sum of those numbers by 2 to find the average. At last, we will print the average of two numbers.

With the help of this program, we can find the average of two numbers.

Program code:-

Output:-

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

  • no1 = it will hold the integer value.
  • no2  = it will hold the integer value.
  • sum = it will hold the integer value.
  • avg = it will hold the float value.

Taking input numbers from the user to for the average of two numbers.

Calculating the average of those numbers.

Printing output average for that given number.