Write a C++ Program to Find Sum and Average of n numbers using for loop.
Solution:
/* C++ Program to Find Sum and Average of n numbers using for loop */
#include<iostream>
using namespace std;
int main()
{
int i,n,x,sum=0;
float avg;
cout<<"How many numbers u want to enter :: ";
cin>>n;
for(i=1;i<=n;++i)
{
cout<<"
Enter number "<<i<<" :: ";
cin>>x;
sum+=x;
}
avg=(float)sum/(float)n;
cout<<"
Sum of "<<n<<" Numbers :: "<<sum;
cout<<"
Average of "<<n<<" Numbers :: "<<avg;
cout<<"
";
return 0;
}
Output:
/* C++ Program to Find Sum and Average of n numbers using for loop */
How many numbers u want to enter :: 6
Enter number 1 :: 1
Enter number 2 :: 2
Enter number 3 :: 3
Enter number 4 :: 4
Enter number 5 :: 5
Enter number 6 :: 6
Sum of 6 Numbers :: 21
Average of 6 Numbers :: 3.5
Process returned 0
| Is This Answer Correct ? | 0 Yes | 0 No |
What do you by Function Overloading in C++?
0 Answers Akamai Technologies, Infogain,
What is a COPY CONSTRUCTOR and when is it called?
Discuss about iteration statements in C++ .
What is meant by exit controlled loop?
What are the different scope C++ provide ?
What is conversion constructor in C++
What is the difference between member functions and static member functions?
To solve the 8 Queens problem, which algorithm is used?
what is a pragma in C++?
In C++ what is a vtable and how does it work?
What is Boyce Codd Normal form?
What is the difference between an ARRAY and a LIST in C++?