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 |
Explain the importance of method overloading in C++?
0 Answers Akamai Technologies, Infogain,
Explain the FOR loop with a help of a code.
Discuss the role of C++ shorthands.
What are the costs and benefits of using exceptions?
What does it mean to declare a function or variable as static?
Question on Copy constructor.
Without using third variable write a code to swap two numbers.
What is latest update of C++ ?
Difference between function overloading and function overriding.
What is the difference between public, private, and protected inheritance?
What are string library functions(syntax).
C++ supports multiple inheritance. What is the “diamond problem” that can occur with multiple inheritance? Give an example.