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


Please Help Members By Posting Answers For Below Questions

is there any choice in opting subjects like 4 out of 7

1727


Assume an array of structure is in order by studentID field of the record, where student IDs go from 101 to 500. Write the most efficient pseudocode algorithm you can to find the record with a specific studentID if every single student ID from 101 to 500 is used and the array has 400 elements. Write the most efficient pseudocode algorithm you can to find a record with a studentID near the end of the IDs, say in the range from 450 to 500, if not every single student ID in the range of 101 to 500 is used and the array size is only 300

1783


What does I ++ mean in c++?

581


What is the difference between public and private data members?

662


What is a far pointer? where we use it?

611






Why is the function main() special?

619


How a modifier is similar to mutator?

630


What is the difference between a reference and a pointer?

591


Why do we use classes in programming?

570


What is vector pair in c++?

709


What are the 2 main types of data structures?

583


What is c++ 11 and c++ 14?

581


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

513


What is the difference between malloc, calloc and realloc?

528


What is the function to call to turn an ascii string into a long?

595