Write a C++ Program to Find Sum and Average of n numbers using for loop.



Write a C++ Program to Find Sum and Average of n numbers using for loop...

Answer / 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

More C++ Interview Questions

What is the 4 difference between delete[] and delete?

0 Answers   Alter,


What Are The Differences Between A C++ Struct And C++ Class?

2 Answers   Wipro,


What is a class in C++?

0 Answers   Amazon, TCS, UGC Corporation,


Declare a pointer to a function that takes a char pointer as argument and returns a void pointer.

0 Answers   Alter,


How to convert integer to string in C++

0 Answers  






What is Advantage and Use of THIS pointer in C++ – Scenarios?

0 Answers  


Write a syntax and purpose of switch statement.

0 Answers   Agilent,


What is the difference between virtual functions and pure virtual functions?

1 Answers  


What is Boyce Codd Normal form?

0 Answers   IBS,


What are string library functions(syntax).

0 Answers   Accenture,


Write a C++ program to print strings in reverse order.

0 Answers   Amdocs,


Write a C++ Program to find Square Root of a number using sqrt() function.

1 Answers  


Categories