Tuesday, February 3, 2015

SUM AND AVERAGE OF THE GIVEN ARRAY

AIM   
            To write a C program to find the sum and average of the given array

ALGORITHM
Step-1: Start the program
Step-2: Get the number of elements to be used in the array
Step-3: Enter the element.
Step-4: Find the summation of all the elements using for loop
Step-5: Display the result
Step-6: End the program

PROGRAM
#include<stdio.h>
main()
{
            int a[100],i,no,sum=0;
            float avg=0;
            printf(“\nEnter the number of elements”);
            scanf(“%d”,&no);
            printf(“Enter the numbers”);
            for(i=0;i<no;i++)
            {
                        scanf(“%d”,&a[i]);
                        sum=sum+a[i];
            }
            avg=(float)sum/no;
            printf(“sum=%d\naverage=%f”,sum,avg);
}


OUTPUT

            Enter the number of elements5
            Enter the numbers 1    2 3 4 5
            Sum=15
            Average=3.000000




RESULT: Thus a c program to find the sum and average from the given array is  

                   executed.

No comments:

Post a Comment