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
What is array give example?
What are libraries in c++?
Where and why do I have to put the "template" and "typename" keywords?
What is the advantage of an external iterator.
Is dev c++ free?
Explain the problem with overriding functions
Where Malloc(), Calloc(), and realloc() does get memory?
How do I tokenize a string in c++?
What is the purpose of ios::basefield in the following statement?
How do you flush std cout?
#include
Describe the process of creation and destruction of a derived class object?
What is string in c++ programming?
Explain data encapsulation?
What is interface in oop?