Calculate sum and average of an array element.

Calculate sum and average of an array element.

/*
Author : Sabya Roy
Problem : Calculate sum and average of an array element
*/

#include<stdio.h>
#include<conio.h>
main ()
{
            printf(“________Calculate sum and average________”);
            int n, i;                                                                         //Taking counter variable
            float num[100], sum=0.0, avg;                              //taking variable for sum and average
            printf(“\n\nEnter the numbers of element : “);  //Show a dialog
            scanf(“%d”, &n);                                                        //input numbers of element
            if (n>100 || n<=0)//a condition
            {
                        printf(“You must enter in range of (1 to 100).\n”);   //Show a dialog
                        printf(“Enter the numbers of element again : “);
…………………..scanf(“%d”, &n);                                                             //input numbers of element
            }
            for (i=0; i<n; i++)
            {
                        printf(“\n%d.Enter number : “, i+1);
                        scanf(“%f”, &num[i]);
                        sum=sum+num[i];
            }
            avg=sum/n;
            printf(“\nSummation : %0.2f”, sum);                                      //Show answer of Summation
            printf(“\nAverage : %0.2f”, avg);                                               //Show answer of Average
            getch();
}

———————————————————————————————————————————————————-




Output :

________Calculate sum and average________

Enter the numbers of element : 3

1. Enter Number : 56
2. Enter Number : 67
3. Enter Number : 23
Summation : 146.00
Average : 48.67

———————————————————————————————————————————————————-

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *