This blog is under construction

Sunday, 23 June 2013

C program to find the Average of N numbers

Write a C program to find the average of N numbers.


  #include <stdio.h>
  #include <stdlib.h>
  int main() {
        int n, *entry, sum = 0, i;
        float average;

        /* get the number of entries from user */
        printf("Enter your no of entries:");
        scanf("%d", &n);

        entry = (int *) malloc(sizeof (int) * n);

        /* get the entries from the user */
        printf("Enter the value for your entries:\n");
        for (i = 0; i < n; i++)
                scanf("%d", &entry[i]);

        /* calculate the total */
        for (i = 0; i < n; i++)
                sum = sum + entry[i];

        /* find the average */
        average = (float) sum/n;

        /* print the output */
        printf("Average: %.2f\n", average);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your no of entries:5
  Enter the value for your entries:
  100
  78
  89
  54
  99
  Average: 84.00



No comments:

Post a Comment