C C++ program to find sum and average of array elements

C C++ program to find sum and average of array elements

C Language में Structure क्या होता है और इसका प्रयोग क्यो किया जाता है जानने के लिए देखें—Structures in C

C Language में Array और इसके विभिन्न प्रकारो के बारे में और अधिक जानने के लिए देखें— Arrays in C

C Language में Character Array या String किसे कहते है जानने के लिए देखें हमारा यह पोस्ट— Strings in C

C program to find sum and average of array elements

#include<stdio.h>
int main()
{
    int num[5],i,sum=0;
    float avg;
    printf("Enter five numbers: ");
    for(i=0;i<5;i++)
    {
        scanf("%d",&num[i]);
    }
    for(i=0;i<5;i++)
    {
        printf("Number = %d\n",num[i]);
        sum=sum+num[i];
    }
    avg=sum/5.0;
    printf("Sum = %d\n",sum);
    printf("Average = %f",avg);
    return 0;
}

C++ program to find sum and average of array elements

#include<iostream>
using namespace std;
int main()
{
    int num[5],i,sum=0;
    float avg;
    cout<<"Enter five numbers: ";
    for(i=0;i<5;i++)
    {
        cin>>num[i];
    }
    for(i=0;i<5;i++)
    {
        cout<<"Number = "<<num[i]<<endl;
        sum=sum+num[i];
    }
    avg=sum/5.0;
    cout<<"Sum = "<<sum<<endl;
    cout<<"Average = "<<avg;
    return 0;
}

Output of C C++ program to find sum and average of array elements

C C++ program to find sum and average of array elements

Important array programs in C C++

  1. Declaration and initialization of array
  2. Input-output in array
  3. Largest Element in Array
  4. Smallest element in Array
  5. Reverse Elements of Array
  6. Merging two arrays
  7. Searching in array (Linear search)
  8. Sorting in array (Selection sort)
  9. Pointer to array
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer