To find the sum and average in giving data set ( C and C++)

Bismillahir Rahmanir Rahim
Solution in C:

#include<stdio.h>
void main()
{
 int i,n;
 float avg,sum=0.00,a[100];
 printf("How many numbers your input (maximum 100).\n");
 scanf("%d",&n);
 printf("Enter your %d numbers:\n",n);
 for(i=0;i<n;i++)
  {
   scanf("%f",&a[i]);
   sum=sum+a[i];
  }
  avg=sum/n;
  printf("The sum is =%2.2f\t",sum);
  printf("The average is= %2.2f",avg);
 }
Solution in C++:

#include<iostream.h>
void main()
{
 int n;
 float avg,sum=0.00,a[100];
 cout<<"How many numbers your input (maximum 100)."<<endl;
 cin>>n;
 cout<<"Enter your "<<n<<" numbers."<<endl;
 for(int i=0;i<n;i++)
  {
   cin>>a[i];
   sum=sum+a[i];
  }
  avg=sum/n;
  cout<<"The sum is ="<<sum<<endl;
  cout<<"The average is= "<<avg<<endl;
 }

Socializer Widget By Blogger Yard
SOCIALIZE IT →
FOLLOW US →
SHARE IT →