take array input from user and calc the avr in c

To calculate the average of an array in C, you can follow these steps:

  1. Declare an array: Start by declaring an array of a specific size. For example, if you want to calculate the average of 5 numbers, you can declare an array of size 5.

  2. Take input from the user: Use a loop to take input from the user for each element of the array. You can prompt the user to enter a number and store it in the corresponding array element.

  3. Calculate the sum: Initialize a variable to store the sum of all the elements in the array. Use a loop to iterate through each element of the array and add it to the sum variable.

  4. Calculate the average: Divide the sum by the number of elements in the array to calculate the average. You can store the result in a separate variable.

  5. Display the average: Print the calculated average to the console or display it in any desired format.

Here's an example code snippet that demonstrates these steps:

#include <stdio.h>

#define SIZE 5

int main() {
    int numbers[SIZE];
    int sum = 0;
    float average;

    // Take input from the user
    printf("Enter %d numbers:\n", SIZE);
    for (int i = 0; i < SIZE; i++) {
        printf("Number %d: ", i + 1);
        scanf("%d", &numbers[i]);
    }

    // Calculate the sum
    for (int i = 0; i < SIZE; i++) {
        sum += numbers[i];
    }

    // Calculate the average
    average = (float)sum / SIZE;

    // Display the average
    printf("The average is: %.2f\n", average);

    return 0;
}

In this example, we declare an array numbers of size 5 to store the user's input. We then use a loop to take input from the user and store it in the array. Next, we calculate the sum of all the elements in the array using another loop. Finally, we calculate the average by dividing the sum by the number of elements in the array and display it to the user.

I hope this helps! Let me know if you have any further questions.