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

Add numbers until 0

C# 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. C# Program to find the sum and average of the numbers until 0 is entered using iteration

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

using System;

namespace AverageSum {
    class Program {
        static void Main() {
            // declare variables
            int n, count, sum;

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

            // keep taking input 
            // until 0 is entered
            do {
                // take input
                Console.Write("Enter a number: ");
                n = Convert.ToInt32(Console.ReadLine());

                // add to sum
                sum = sum + n;

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

            } while (n != 0);

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

Output


Enter a number: 9

Enter a number: 4

Enter a number: 7

Enter a number: 2

Enter a number: 3

Enter a number: 2

Enter a number: 0


Total number entered: 6

Sum: 27

Average: 4.5




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

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

using System;

namespace AverageSum {
    class Program {
        // recursive function to find average and sum
        // of numbers until 0 is entered
        static void findSumAverage(int sum, int count) {
            // take input
            int n;
            Console.Write("Enter a number: ");
            n = Convert.ToInt32(Console.ReadLine());

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

        static void Main() {
            // declare and initialize variables
            int sum = 0, count = 0;

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

Output


Enter a number: 8

Enter a number: 9

Enter a number: 4

Enter a number: 5

Enter a number: 0


Total number entered: 4

Sum: 26

Average: 6.5