Category Archives: Java Programs

Java Programs

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

Java Program to Find Sum of odd digits in a number

In this tutorial, we will learn to create a Java Program to Find sum of odd digits 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.
  • Built in functions.

What is Even number.

Even number are that number  that can be divided by two and gives remainder equals to zero and ends with 2,4,6,8 and with 0.
Example: (No/2==0) =>  (8/2==0) so 8 is a even Number.
Some even numbers are 2, 4, 6, 8, 10.

What is Odd Number

Odd numbers are number that are not divide by 2  and does not give remainder to Zero, when divided by 2, some the odd numbers are end with 1, 3, 5, 7, 9.

Example: (No/2==0) =>  (5/2==1) so 5 is an Odd Number.

Java Program to Find Sum of odd digits in a number.

In this program we will learn to create a program that will find sum of Odd digits in 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 Odd digits in a given number.

Output

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

  • number= it will hold entered number.
  • Even = it will hold sum or even digit.
  • rev = for reversing a string.

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

Here we create a method named Oddsum() for finding all the odd number in a digit and adding them at the end return sum of all odd values and print the required output.

.

 

Java Program to Find sum of even digits in a number

In this tutorial, we will learn to create a Java Program to Find sum of even digits 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. Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Even number

Even number are that number  that can be divided by two and gives remainder equals to zero.
Example: (Number/2==0) =>  (4/2==0) so 4 is a even Number.
Some even numbers are 2, 4, 6, 8, 10,……

Odd Number

Odd numbers that are not divisible by 2. and does not give remainder = Zero, when divided by 2, some the odd numbers are end in 1, 3, 5, 7, 9, 11……

Java Program to Find sum of even digits in a number

In this program we will learn to create a program that will find sum of even digits in a 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 sum of even digits in a number.

Output

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

  • number= it will hold entered number.
  • Even = it will hold sum or even digit.
  • rev = for reversing a string.

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

Steps for adding even values.

  1. Take a number from user,.
  2. Declare a variable Even to store the sum of even value in a number.
  3. Reverse a Digit to find last digit of the number.
  4. Now check all the digit is even or not.
  5. If it even then adds it to Even variable, else go to next step
  6. Remove last digit of the number.
  7. Repeat this until the number becomes 0.
  8. And last print the sum of all even digit in a number.

Java Program to Find sum of N Natural numbers

In this tutorial, we will learn to create a Java Program to Find sum of N Natural 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
  • Class and Object.
  • Basic Java programming.
  • Loop in Java 
  • While loop For loops.

What Is Natural  Number?

Mathematically ,Natural numbers are those numbers used for counting and ordering,Simply all the counting numbers  {1, 2,3.4…} are commonly called Natural numbers.Natural numbers  include all the positive integers from 1 to infinity.

Java Program to Calculate the Sum of Natural Numbers

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 sum of all N natural number of given number.

Output

In our program we iterate from 1 to the given (15) and adds all numbers to the variable Sum
sum=sum+i. and after calculating value we will print sum or all natural 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.

In the above program, we have first declared and initialized a set variables required in the program. In our  program first we will take a value from user.After that we will start loop from 1 to given number.

Within the while loop, we add all values in variable Sum .After calculating values  of all natural number between the given numbers natural number.

and finally we print sum of the all “N” Natural number between 1 to given number.

Java Program to Find GCD of N Numbers

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

What Is GCD of HCF?

HCF stands for Highest Common Factor or (GCD) Greatest common divisor .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 GCD=> “is the largest positive integer that divides each of the integers“.

Java Program to Find GCD of N Numbers

In this program we will find GCD of N given number using For loop. We would first declared and initialized the required variables. Next, we would prompt user to input size of an array Later we will find the GCD or  HCF of given numbers.

Output

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

  • number =It will hold size of an array elements
  • arr[] =It will hold N number given by user.
  • i = For iteration.

Let’s see the steps of finding gcd.

* Read the data from user using scanner class method and store those entered values in to the variables arr[].

  • Now pass the arr[] and result variable to the static method gcd(result,arr[i]) to find GCD.
  • Where within the function gcd() we will find the greatest common divisor of given values and return the GCD.

Create Electric Bill Calculator Program In java

In this tutorial, we will learn to Create a program of Electric Bill Calculator Program 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.
  • nested if-else statements.
  • Loop’s in Java.

How to calculate Electricity bill.

Here in this tutorial you will learn to calculate Electricity bill by simply writing  Java program . Create Electric Bill Calculator Program In java

Before, writing a program.

first we know about the tariff rates of per unit according to MPEB .Here we are taking some random charges  user can alter according to their Electric Broad.

  • 1 to 100 units –rs 10/unit.
  • 100 to 200 units –rs 15/unit.
  • 200 to 300 units – rs 20/unit.
  • above 300 units – rs 25/unit.

If consumer use electricity between “0-100” unit then he/she have to pay 10 rs per unit, if we consume electricity between 100-200 unit then for the first 100 he/she have to pay 10 per unit (i.e. 100*10), and for remaining pay 15 per unit. Similarly, if consume >200 units then he/she have to pay 10 per unit for the first 100 units 15 per unit for the next 100 units, and 20 per unit for the remaining units. and so on..

Java program to calculate Electricity bill

In this program we will to create a java program to generate electric Bill . We would first declared and initialized the required variables. Next, we would prompt user to input the units. Later we will Calculate Bill.

Output

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

  • unit= it will hold entered units.

After that we pass the given unit by user to a user define function “Bill(unit)”

where we calculate Bill according to provided units using nested if-else statement and after calculate bill reruns the bill to main function.

here we will print the charge payable by user for consuming that number of units

 

Create BMI (Body Mass Index) Calculator In Java

In this tutorial, we will learn to create a Create BMI (Body Mass Index) Calculator 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. Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Body Mass Index (BMI)

It is measured on the basis of “health,height and weight”. So it can be calculated by taking the weight in kg and by dividing the square of your height in meters.

Formula of Calculating BMI.

BMI = (Weight in Kg) / (Height in Meters * Height in Meters).

Example
weight = 70 kg, height = 1.2 m
BMI = 70 / (1.65*1.65) = BMI= 25.7 kg/m2

  • Healthy BMI range is :=> 18.5 kg/m2 – 25 kg/m2
  • Healthy weight for the height ranges :=> 50.4 kgs – 68.1 kgs
  • You need to Lose 1.9 kgs to reach a BMI of 25 kg/m2.
     BMI Range                          Category
     BMI   < 18.5                             Thinness
     BMI 18.5 – 25                             Normal
     BMI   25 – 30                             Overweight
     BMI> 30                             Obese

Create BMI (Body Mass Index) Calculator In Java

In this program we will find BMI(Body Mass Index). We would first declared and initialized the required variables. Next, we would prompt user to input height and weight.Later we will find the BMI. Let’s have a look at the program.

Output

BMI

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

  • weight =It will hold value of kg.
  • meter =It will hold value in meter.
  • BMI =  For calculating BMI.

After that taking value of weight in kg and height in meter we pass these values to function bmicalculator (weight,height).

After taking values from user bmicalculator () method takes two arguments and calculate bmi as shown in image below.

Here BMI calculator finds that whether the person is underweight, normal, overweight, or obese and the print result.

Create Simple Calculator Program In Java

In this tutorial, we will learn to create a Java program to make a simple Calculator using Switch case 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.
  • Switch case.
  • Nested if-else Statements.
  • Java Scanner class.

What is calculator?

Calculator is a Device that is used to performing mathematical operation like arithmetic operations on numbers.With the Calculators we can perform operation like Addition, Subtraction,Multiplication, and Division… and function like that.

Create Simple Calculator Program In Java

In this our program we will create a small calculator using switch case . We would first declared and initialized the required variables. Next, we would prompt user to input the two values . Later in program we will ask operator and perform that particular operation in our program.

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.
  • opt=it will hold operator value.

After declaring variables  in the next statement user will be prompted to enter two values and  which will be assigned to variable ‘a‘ and ‘b‘ respectively as shown in image below.

Afterword we will ask user to input symbol to perform following task like( +,-,*,/) to perform following task.

After taking values opt we will pass the value of options to switch case to Calculate result

The above  statements compute the addition of two numbers and print the output.

and finally we will print the result.

Java Program to Print 1 to 100 Without Loop

Java Program to Print 1 to 100 without using Loop

In this tutorial, we will learn to create a Java program that will Print 1 to 100 without using Loop 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.
  • Conditional Statements in Java programming.

Algorithm:-

As we all know, the series printing works are done using the loops, but today we will print a series from 1 to 100 without using Loop. In this program, we will first define the variables for the program. Then we will pass that variable to a user-defined function to print the series. The function first prints a number and increases the value by 1.

With Java programming, we can make many arithmetic series with just a little code.

With the help of this program, we can Print 1 to 100 without using Loop.

Program:-

Output:-

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

  • number = it will hold the integer value for the program.

Sending the data to the user-defined function for the generation of series.

Body of the user-defined function to print the series.

Program code to print the list up to the given number 100.