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 does it mean to declare a function or variable as static?
What is C++11?
Write a program to display the following output using a single cout statement Maths=90 Physics=77 Chemistry = 69
How to reverse a string in C++
What do you know about Volatile keyword in C++? Explain with an example code.
Explain the difference between method overriding and method overloading in C++?
Mention the default functions in C++, how would you detect that error has occurred inside the constructor and destructor.
When must you use a constructor initializer list?
What is an abstract class in C++
0 Answers SwanSoft Technologies,
Write a C++ Program to find Square Root of a number using sqrt() function.
What are the major differences between C and C++?
What is the difference between malloc, calloc and realloc?