Write a Java method to compute the average of three numbers


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1.  
  2. import java.util.Scanner;
  3. public class Javaexercise {
  4.  
  5. public static void main(String[] args)
  6. {
  7. Scanner in = new Scanner(System.in);
  8. System.out.print("Input the first number: ");
  9. double num1 = in.nextDouble();
  10. System.out.print("Input the second number: ");
  11. double num2 = in.nextDouble();
  12. System.out.print("Input the third number: ");
  13. double num3 = in.nextDouble();
  14. System.out.print("The average value is " + average(num1, num2, num3)+"\n" );
  15. }
  16.  
  17. public static double average(double num1, double num2, double num3)
  18. {
  19. return (num1 + num2 + num3) / 3;
  20. }
  21. }

Result

java techstudy.org

     





Related Topics




Popular Posts