Java Program to Find the Average and Sum of Numbers Until 0 is Entered

Add numbers until 0

Java programs to find the sum and average of the numbers until 0 is entered, are given here. Suppose the input numbers are 5, 10, 9, 0, then the sum = (5 + 10 + 9) = 24 and average = (5 + 10 + 9) / 3 = 8.







1. Algorithm to find the sum and average of the numbers until 0 is entered


1. Set sum = 0, count = 0.

2. Take a number n as input.

3. Perform sum = sum + n.

4. Check if n = 0

5. If step 4 is true declare sum and as the sum and (sum / count) as the average of the numbers entered.

6. If step 4 is false, perform count = count + 1 and go to step 2




2. Pseudocode to find the sum and average of the numbers until 0 is entered


1. Procedure sumAverage():

2. sum := 0

3. count := 0

4. n := Input()

5. Repeat until n = 0

6. sum := sum + n

7. count := count + 1

8. n := Input()

9. avg := (sum / count)

10. Return sum, avg

11. End Procedure





3. Time complexity to find the sum and average of the numbers until 0 is entered


Time Complexity: O(n)

Here n is the number of terms.





4. Program to find the sum and average of the numbers until 0 is entered




4.1. Java Program to find the sum and average of the numbers until 0 is entered using iteration

Code has been copied
/************************************
	  alphabetacoder.com
Java program to find average and sum
of numbers until 0 is entered
*************************************/

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        // declare object of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int n, count, sum;

        // initialize
        sum = 0;
        count = -1;

        // keep taking input 
        // until 0 is entered
        do {
            // take input
            System.out.print("Enter a number: ");
            n = sc.nextInt();

            // add to sum
            sum = sum + n;

            // increment the value of number count
            count++;

        } while (n != 0);

        // display the sum and average
        System.out.println("\nTotal number entered: " + count);
        System.out.println("Sum: " + sum);
        System.out.println("Average: " + (double) sum / count);
    }
}

Output


Enter a number: 5

Enter a number: 9

Enter a number: 8

Enter a number: 4

Enter a number: 0


Total number entered: 4

Sum: 26

Average: 6.5




4.2. Java Program to find the sum and average of the numbers until 0 is entered using recursion

Code has been copied
/******************************************
	     alphabetacoder.com
Java program to find average and sum of
numbers until 0 is entered using recursion
*******************************************/

import java.util.Scanner;

class Main {
    // recursive function to find average and sum of
    // numbers until 0 is entered
    static void findSumAverage(int sum, int count) {
        // declare object of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int n;
        // take input
        System.out.print("Enter a number: ");
        n = sc.nextInt();

        // keep taking input 
        // until 0 is entered
        if (n == 0) {
            System.out.println("\nTotal number entered: " + count);
            System.out.println("Sum: " + sum);
            System.out.println("Average: " + (double) sum / count);
        } else {
            // add input to sum and increment count
            sum += n;
            count++;
            findSumAverage(sum, count); // recursive call
        }
    }

    public static void main(String args[]) {
        // declare and initialize variables
        int sum = 0, count = 0;

        // call function to display result
        findSumAverage(sum, count);
    }
}

Output


Enter a number: 9

Enter a number: 12

Enter a number: 7

Enter a number: 9

Enter a number: 10

Enter a number: 3

Enter a number: 0


Total number entered: 6

Sum: 50

Average: 8.333333333333334