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 |
Mention the default functions in C++, how would you detect that error has occurred inside the constructor and destructor.
What is an abstract class in C++
0 Answers SwanSoft Technologies,
What is the difference between creating an object, using 'new' and using 'malloc'?
What are the major differences between C and C++?
What is function overloading and operator overloading in C++?
How does stack look in function calls? Write a recursive function call, how will the stack look like?
What is Advantage and Use of THIS pointer in C++ – Scenarios?
How does stack look in function calls? When does stack overflow? What can you do to remedy it?
What is the 4 difference between delete[] and delete?
Can we call a virtual function from a constructor?
What are the fundamental features of an object-oriented language?
What are the costs and benefits of using exceptions?