Write a C++ Program to Find Sum and Average of n numbers using for loop.
Answer Posted / hr
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 |
Post New Answer View All Answers
What is a driver program?
What is the difference between abstraction and polymorphism?
Why is abstraction needed?
Which of the following is evaluated first: a) && b) || c) !
What are destructors?
What is command line arguments in C++? What are its uses? Where we have to use this?
What is helper in c++?
the first character in the variable name must be an a) special symbol b) number c) alphabet
What is meaning of in c++?
What is wrapper class in c++?
What is c++ and its features?
What two types of containers does the stl provide?
What is the use of seekg in c++?
When would you use a pointer? A reference?
What is java and c++?