Answers for "java code for average of 2 numbers"

1

Java program to calculate average of 3 numbers

// Java program to calculate average of 3 numbers
import java.util.Scanner;
public class AverageNumberDemo 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first number: ");
      double number1 = sc.nextDouble();
      System.out.print("Please enter second number: ");
      double number2 = sc.nextDouble();
      System.out.print("Please enter third number: ");
      double number3 = sc.nextDouble();
      System.out.print("The average of 3 numbers is: " + calculateAverage(number1, number2, number3));
      sc.close();
   }
   public static double calculateAverage(double num1, double num2, double num3)
   {
      return (num1 + num2 + num3) / 3;
   }
}
Posted by: Guest on February-20-2021
1

Java program to find average of n numbers

// Java program to find average of n numbers
import java.util.Scanner;
public class FindAverageOfNNumbers
{
   public static void main(String[] args) 
   {
      int num, count = 1;   
      float a, average, total = 0;   
      Scanner sc = new Scanner(System.in);     
      System.out.println("Please enter n numbers: ");  
      num = sc.nextInt();  
      while(count <= num)
      {   
         System.out.println(count + " number: ");  
         a = sc.nextInt();
         total += a;
         ++count;   
      }
      average = total / num;   
      System.out.println("Average is: " + average);
      sc.close();
   }
}
Posted by: Guest on February-20-2021

Code answers related to "java code for average of 2 numbers"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language