BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl
Home / Java Examples / Java Program to Find average of 3 numbers

Java Program to Find average of 3 numbers

By Chaitanya Singh

In this tutorial we will write a java program to find the average of three numbers.

Java Program to find the average of 3 numbers

In this example, we are taking input from the user and calculating the average of entered numbers using “/” operator. The reason why we are using double as data type because a user can enter any data type number such as int, float, long & double, since double can hold the values of all these data types, it is important to declare variables as double data type.

However if you are not taking input from user then you can declare the data type based on the number value, for example if you want to find out the average of three integer numbers then you are declare num1, num2 & num3 as int but you have to use double as the return type of the avr method because the average of 3 int numbers can be a decimal value.

import java.util.Scanner;
public class JavaExample {

 public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        double num1 = scan.nextDouble();
        System.out.print("Enter the second number: ");
        double num2 = scan.nextDouble();
        System.out.print("Enter the third number: ");
        double num3 = scan.nextDouble();
        scan.close();
        System.out.print("The average of entered numbers is:" + avr(num1, num2, num3) );
    }

  public static double avr(double a, double b, double c)
    {
        return (a + b + c) / 3;
    }
}

Output: Following is the screenshot of the output of above program:
Java Program to Find average of 3 numbers

Related Java Examples

  1. Java program to calculate average using array
  2. Java program to calculate simple interest
  3. Java program to calculate compound interest
  4. Java program to calculate and display student grades

Posted Under: Java Examples

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap