Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

Statistics - Practice Exercises Java


Lesson 2:

Flow Control


Exercise 2.29:

Statistics


Objetive:

Write a program to compute several basic statistical operations: it will accept numbers from the user and show their sum, average, minimum and maximum, as in the following example:

Number? 5
Total=5 Amount=1 Average=5 Maximum=5 Minimum=5
Number? 2
Total=7 Amount=2 Average=3 Maximum=5 Minimum=2
Number? 0
Bye!

(As seen in this example, the program will finish when the user enters 0)


Source Code:


import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int num;

		int total = 0, amount = 0;
		int maximum, minimum;

		System.out.print("number? ");
		num = Integer.parseInt(new Scanner(System.in).nextLine());
		maximum = num;
		minimum = num;

		while (num != 0)
		{
			amount++;
			total += num;

			if (num > maximum)
			{
				maximum = num;
			}

			if (num < minimum)
			{
				minimum = num;
			}

			System.out.printf("Total=%1$s Amount=%2$s Average=%3$s maximum=%4$s minimum=%5$s" + "\r\n", total, amount, total / amount, maximum, minimum);

			System.out.print("number? ");
			num = Integer.parseInt(new Scanner(System.in).nextLine());
		}
		System.out.println("Bye!");
	}
}
Exercisey 2.29

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.