write a program in Java to assign four integers and print their average
Answers
Answer:
import java.util.Scanner;
public class Exercise12 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input first number: ");
int num1 = in.nextInt();
System.out.print("Input second number: ");
int num2 = in.nextInt();
System.out.print("Input third number: ");
int num3 = in.nextInt();
System.out.print("Input fourth number: ");
int num4 = in.nextInt();
System.out.print("Enter fifth number: ");
int num5 = in.nextInt();
System.out.println("Average of five numbers is: " +
(num1 + num2 + num3 + num4 + num5) / 5);
}
}
Copy
Sample Output:
Input first number: 10
Input second number: 20
Input third number: 30
Input fourth number: 40
Enter fifth number: 50
Average of five numbers is: 30
Flowchart:
Flowchart: Java exercises: Calculate and print the average of five numbers
Sample Solution:
Java Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double num = 0;
double x = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Input the number(n) you want to calculate the average: ");
int n = sc.nextInt();
while (x <= n) {
System.out.println("Input number " + "("+ (int) x +")" + ":");
num += sc.nextInt();
x += 1;
}
double avgn = (num / n);
System.out.println("Average:" + avgn);
}
}
Copy
Sample Output:
Input the number(n) you want to calculate the average:
4
Input number (1):
2
Input number (2):
4
Input number (3):
4
Input number (4):
2
Average:3.0
Flowchart:
Flowchart: Java exercises: Calculate and print the average of five numbers
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to print the area and perimeter of a circle.
Next: Write a Java program to print the area and perimeter of a rectangle.
What is the difficulty level of this exercise?
EASY MEDIUM HARD
Based on 477 votes, average difficulty level of this exercise is Easy .
New Content published on w3resource :
Python Numpy exercises
Python GeoPy Package exercises
Python Pandas exercises
Python nltk exercises
Python BeautifulSoup exercises
Form Template
Composer - PHP Package Manager
PHPUnit - PHP Testing
Laravel - PHP Framework
Angular - JavaScript Framework
React - JavaScript Library
Vue - JavaScript Framework
Jest - JavaScript Testing Framework
by TaboolaSponsored LinksYou May Like
The cost of hearing aids in Thane might surprise you!
hear.com
13 Card Game.Get upto Rs 2000 Free bonus. Play rummy now!
Rummy Circle
Make Your Kid a Coding Champion. Get Free Class.
CampK12
Homes that care for you, in Kanjurmarg W, from ₹1.10 Cr*
Tulip at Runwal Forests
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
©w3resource.com 2011-2020
PrivacyAboutContactFeedbackAdvertise
JAVA PROGRAM
import static java.lang.Float.sum;
import java.util.Scanner;
public class Average {
public static void main(String[] args)
{
int n, count = 1;
float xF, averageF, sumF = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
n = sc.nextInt();
while (count <= n)
{
System.out.println("Enter the "+count+" number?");
xF = sc.nextInt();
sumF += xF;
++count;
}
averageF = sumF/n;
System.out.println("The Average is"+averageF);
}
}
#SPJ2