Category Archives: Java Basic Programs

Java Basic Programs

Java Program to Multiply two Numbers

Java Program to Multiply Two Numbers

In this tutorial, we will learn to create a Java program that will Multiply 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 operations in java programming.

Program to Multiply Two Numbers:-

In today’s tutorial, we will create a program that will multiply the two numbers using the concepts of java programming. First, we will take the two numbers in input from the user. Then we will multiply the two numbers from the code. At last, we will print the output of the program.

With the help of this program, we can take input and Multiply Two Numbers.

Algorithm to multiply the numbers:-

Program to Multiply Two Numbers:-

Output:-

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

  • no1 = it will hold the input number value from the user.
  • no2 = it will hold the input number value from the user.
  • Multiply = it will hold the sum value of the numbers.

Taking input numbers from the user.

Calculating the multiply of two numbers.

Printing the output.

 

Java Program to add two complex numbers

Java Program to add two complex numbers

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

What is a Complex Number?

A number is said to be a complex number if it is in the form of x+yi, where x and are real numbers, and i is an indeterminate satisfying the condition i2 = −1.

For example, 2 + 3i

Algorithm:-

Program to add two Complex Numbers:-

In this program, we will first declare the values of the variables. Then we will add two numbers using the functions in java. At last, we will print the output of the program to the user using a print function.

Program:-

Output:-

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

  • temp = it will hold the complex value.
  • real = it will hold the float value.
  • img = it will hold the integer value.

Taking the input integer number and exponent from the user.

Calculating the complex numbers.
Printing the output numbers.

Java Program to add two binary numbers

In this tutorial, we will learn to create a Java Program to Add two Binary 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 Function

Binary Number System.

A Number system in which  numbers are represented by using only two digits (0 and 1) with a base 2 is called a binary number system.

Binary addition.

For Binary addition you must remember the following  rules
The four rules of binary addition are:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 =10.

Just look at the table how we add values in Binary are shown.

       x  

             y

                                 x+y

           0                     0                                                           0
           0                     1                                                          1
          1                     0                                                          1
          1                     1                                                          0      (where 1 is carried over)

Java Program to add two binary numbers

In this program we will learn to add two binary numbers.First of all we declare required variable and also initiates required values in variable. Next we take value from user at run time and then after we will add two binary given by user.

Output

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

  • no1 = it will hold first given binary number.
  • no2= it will hold second Binary number.
  • sum= it will hold sum of numbers.
  • rem= ot will hold reminder of values.

First of all we declared and initialized set of variables required in the program. Then we will take a value from user at run time as shown in image below.

After taking all the values for user we will perform Binary Addition using the following formula.

With in the while loop first of all we check the given number are greater than zero and after that with all the four rule we add two given number by user as shown below.

User enter the two binary numbers that we add bit by bit using while loop and storing the result in sum.

And finally we will print the result.

Java Program to Calculate Future Investment Value

In this tutorial, we will learn to create a Java Program to Calculate Future Investment value 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.

Investment

investing means buying an asset, or money put in a bank to get an interest in it in the future in other words investment is a process of allocating money in expectation of a benefit/return in the Future.

Java Program to Calculate Future Investment Value

In our program, we will learn to calculate the Future investment value of a given Money. Firstly we declare the required variable and also initiate the required values in the variable. Next, we take value from the user at run time, and then after we will Calculate Future Investment value.

Output

Investment.

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

  • amount= it will hold the entered value of amount
  • rate= it will hold the entered value of rate
  • year= it will give the value of the year.
  • Investment= it will give the value of the accumulated  Price.

First of all, we declared and initialized the set of variables required in the program. Then we will take a value from the user at run time as shown in the image below.

After taking all the values for user we will calculate the future investment using the following formula.

And finally with the help of above formula we will print the required estimation value of a invested amount.

Java Program to Find Ncr and Npr

In this tutorial, we will learn to create a Java Program to Find Ncr and Npr 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.

Permutation:

Permutation is the process of arranging the elements in sequence of all the members of given set into a particular sequence or order.

Example: Set A of elements (1 & 2) can be arranged into 2 ways such as in increasing {1,2,} and in decreasing order {2,1}.

Combination:

Combination is the process of arranging elements in a set as many combination as possible of any particular set.

Example: We have set of 3 elements from set A with members (1, 2 & 3) we can create 3 possibilities or combination they are {1,2}, {2,3} and {1,3}.

Java Program to Find Ncr and Npr

In this program we will learn to create a program that will find Ncr and Npr of a given  numbers. Firstly we declare required variable and also initiates required values in variable. Next we take value from user at run time and then after we will finf Ncr and Npr of a given numbers.

Output

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

  • N= it will hold entered value of n
  • R = it will hold entered value of r
  • per = it will give value of permutation.
  • com= it will give value of Combination.

First of all we declared and initialized set of required variables in the program.Then take a value from user at run time.Afterword we pass the given values to function named cp.permutation(n,r) it calls the permut() method and permut() method calls the fact() method and prints all the permutations of numbers.

cp.combinat(n,r) it will calls the combinat() method and combinat() method calls the fact()

method and prints all the Combinations.this process will keep on repeating until the condition if(n>=r) became false.

And at last we will print all the value or Permutation and Combination.

Java Program to Calculate Average Marks

In this tutorial, we will learn to create a Java Program to Calculate Average marks  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.

Average

Average can be find by adding the numbers(let say we have 5 number then average is “1+2+3+4+5/5”),and then dividing the total by the total number size (5).

In mathematics, an average is called a “mean” = (sum of values)/number of items.

Java Program to Calculate Average Marks

In our program we will create a program that will Calculate Average of Marks. Firstly we declare required variable and also initiates required values in variable. Next we take values from user at run time and then after we will find the average of Marks.

Output

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

  • mark= it will hold entered numbers.
  • sum = it will hold sum of values.
  • avg = it will hold average of numbers.

After that we will takes values from user in variable mark[].

And after we will add values taken from user sum=sum+mark[i] and find the average of marks in avg (sum/avg) variable as shown in image below.

And finally we print the result i.e sum of all values and average of given values. The above Program is compile and executed, it will produce the following output:

Java Program to Calculate CGPA

In this tutorial, we will learn to create a Java Program to Calculate CGPA 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.

CGPA(Cumulative Grade Point Average)

Cumulative Grade Point Average (CGPA) is a tool used to evaluate your academic performance.
For conversion of CGPA to percentage, the CGPA must be multiplied with 9.5.

Example. if your score in Cgpa is 9.7 ,then in “%”, it will be 9.7×9.5=92.1%.

Java Program to Calculate CGPA

In this program we will learn to create a program that will Calculate CGPA and Percentage from CGPA. Firstly we declare required variable and also initiates required values in variable. Next we take value from user at run time and then after we will find CGPA and Percentage from CGPA .Lets have a look at the program.

Output

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

  • number= it will hold size of number.
  • marks= it will hold given marks.
  • sum= it wil hold sum of values.
  • cgpa= it will give value result in cgpa.

First of all we declared and initialized set of variables required in the program. Then we will take a value and pass this value to function Calculate_CGPA(marks,number).

With in the Function we will Calculate both the answer in CGPA and also in percentage.

And return the result back to main function where we print the value in CGPA and in Percentage.

To get value form CGPA just simply multiply with 9.5 as shown in above picture.

Java Program to Calculate Batting Average

In this tutorial, we will learn to create a Java Program to Calculate Batting Average 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. Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Batting Average.

By taking three values ” runsmatches, and number of not-out” represent number of runs scored and innings played by the batsman and number of times remained Not Out. respectively.For calculating Batting Average

“Average= Run score/number of dismissals.”

where : number of dismissals = number of innings -number of innings remained not out.

Java Program to Calculate Batting Average

In this program we will to create a program to calculate Bating Average using java program. We would first declared and initialized the required variables. Next, we would prompt user to input the values.Later we find Batting average. 

Output

Batting Average.

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

  • match = it will hold entered match
  • run   =  it will hold entered runs
  • ings  = it will hold entered innings.
  • notout= it will hold total number of not-out.

And in the next statement user will be prompted to enter  different values  and  which will be assigned to variables “match,run , notout , ings” respectevely.

Matches:

Innings:

Not-out

Runs:

After taking all the values,Now we will calculate Bating average.
First we calculate the number of dismissals,that’s equal to matches – notout.
Batting Average, equal to runs/ (matches – notout).

At last we will Print the calculated Bating average.

Java Program to Calculate Average Of N Numbers

In this tutorial, we will learn to create a Java Program to Calculate Average Of N 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. Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Average of  numbers.

Average is the sum of all data values Dived by the Number of data values.

Average=sum of n natural number divided by N or  “[n(n+1)/2]”.

Java Program to Calculate Average Of N Numbers

In this program we will learn to create a program to calculate Average of N number. 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 then after we will find the Average of N natural number.

Output

In our program with the help of for loops we loop from 1 to the given number (5) and add all numbers to the variable Sum.
sum=sum+i. and after we calculate average of values “average=sum/number“.

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

  • number=to take value from user.
  • i =For iteration.
  • sum = for counting sum of numbers.

Here we first declared and initialized set of variables required in the program. Then we will take a value from user.After that we will start loop from 1 to given number to add values in array.

after taking these values we will add these values in loop.

Within the loop, we add all values in variable Sum .After calculating sum of values of N natural number between the given numbers natural number we find average of n values and print the result in output.

Java Program to Generate Fibonacci Series using Recursion

Java Program to Generate Fibonacci Series using Recursion

In this tutorial, we will learn to create a Java program that will Print the Fibonacci Series Program 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.
  • For loop in Java Programming.
  • Functions in Java Programming.

What is Fibonacci Series?

The Fibonacci series is a series that is strongly related to Binet’s formula (golden ratio). In the Fibonacci series, the series starts from 0, 1 and then the third value is generated by adding those numbers.

Algorithm:-

Programs to Generate Fibonacci Series:-

In this program, we take the number of elements for the series from the user. Then we will generate the Fibonacci series using the while loop. At last, we will print the series using the print function. The code for the program is given below.

Program:-

Program to Generate Fibonacci Series.

 

Output:-

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

  • i = it will hold the integer value for the loop.

Generating the Fibonacci series using the loops.

Program Code for Printing the series.

Java Program to Find Sum of Digits Until Single Digit

In this tutorial, we will learn to create a Java Program to Find sum of digits Until Single Digit in a 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 function in Java.
  • Class and Object in Java.
  • Basic Java programming. 
  • If-else statements in Java.
  • For loop in Java.
  • Inbuilt functions and user define functions.

Sum of Digits of a number until single Digit.

Here we will find the sum of digits until the number becomes single digit.
Example:-Suppose we have taken a number = 346.
The sum of digits of 346 = 3+4+6 = 13.
Now the number 13 is of two digits number so we need to do again we will find the sum of digits of the number,
The sum of digits of 13 = 1+3 = 4
Finally 4 is single-digit so sum of the number until single digit of 346 is 4.

Java Program to Find Sum of Digits Until Single Digit

In this program we will learn to create a program that will find sum of digits until single digit of a given number. Firstly we declare required variable and also initiates required values in variable. Next we take value from user at run time and then after we will find the sum of all digit until single digit sum.

Output

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

  • number= it will hold entered number.
  • firstdigit= it will hold single digit sum.
  • sum= it will hold sum of digit.
  • enddigit= it will hold find last digit.
  • total=it will add the  values.

Here we first declared and initialized set of variables required in the program. Then we will take a value from user at run time.After that pass the given number to function named Sumation(number).where we going to calculate sum of all digit of a given number.

within the function we pass total to function Sum()  where we calculate sum of digit to unit place.

this function will return the digit sum to main function where we print the result.

 

Java Program to Find Sum of first & last digit of a number

In this tutorial, we will learn to create a Java Program to Find Sum of first & last digit of a 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 function in Java.
  • Class and Object in Java.
  • Basic Java programming. 
  • If-else statements in Java.
  • For loop in Java.

Java Program to Find Sum of first & last digit of a number.

In this program we will find sum of first and last Digit of a Number .We would first declared and initialized the required variables. Next, we would prompt user to input a digit. Later we will find sum of first and last value of a digit.

Output

In our program we take a digit from user and add first and last digit of a number and return the sum of first and last digit.

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

  • number=to take value from user.
  • first=it will hold digits first number.
  • last= it will hold digit last number.
  • sum = for counting sum of first and last.
  • count = for counting digit number.

In the above program, we have first declared and initialized a set variables required in the program. after that we will take a value from user and find sum of its first and last digits.

How to find the sum of first and last digit of a number in java,

        1. First of all take a number from user.
        2. Declaring variable and initialize them.
        3. First find the last digit simply we use %(modulus) operator.
        4. The expression is “number %10” gives the last digit of the number.
        5. For finding the first digit of a number.
        6. Find the total number of digits in the given number.
      1. Divide the number by 10^(total_number_of_digits-1), it will give the first digit of the number.Add first and the last digit to the sum variable