C program for average of n numbers using arrays




















These numbers are stored in the num[] array. Once the for loop is completed, the average is calculated and printed on the screen. Course Index Explore Programiz. Popular Tutorials Data Types in C. C for Loop. Arrays in C Programming. Pointers in C. Find roots of a quadratic equation. Print Pyramids and Patterns. Check prime number. Print the Fibonacci series. Reference Materials string. SUM We use for loop and iterate N times to fetch all the array elements.

Inside for loop we add the value of individual array element a[i] to previous value of variable sum. You could even calculate sum inside the for loop which is used to accept input from the user.

Whenever user inputs a number it is immediately added to the previous value of variable sum. We calculate sum inside first for loop itself which is used to accept input by user. This method is faster and cheaper on resource usage.

Make use of macros to assign size of array. Make sure to declare variable avg to calculate average as float or double. If the user enters a value of n above or below , a while loop is executed which asks the user to enter a value of n until it is between 1 and Then, we use a for loop to input the numbers from the user and store them in the num array.

Course Index Explore Programiz. Popular Examples Create a simple calculator. Check prime number. Print the Fibonacci sequence.

Check if a number is palindrome or not. Program to multiply matrix. Reference Materials iostream.



0コメント

  • 1000 / 1000