Finding The Avg and Sum using any kind of loops

How do i find the avg and sum yung any kind of loop? answer pls this is my code so far
#include <iostream>
using namespace std;

int main()
{
int num,avg;
float input, sum=0, AVG;
cout<<"Input a number"<<endl;
cout<<"Input No 1"<<endl;
cin >> num;
cout<<"Input No 2"<<endl;
cin >> num;
cout<<"Input No 3"<<endl;
cin >> num;
for(int i = 1; i <= 2; i++){
cin >> input;
sum += input;
}

avg = sum / num;
cout << "average = " << avg << sum << endl;

return 0;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
int num = 0;
float input = 0, sum = 0;

while (input != -1)
{
   cout<<"Input a number (-1 to quit) : " ;
   cin >> input;
   if( input !=-1 )
   {
      num++;
      sum += input;
      cout << "       Current total: " << sum << endl;
      cout << " Current average: " << sum/num << endl;
   }  
}

return 0;
}


Input a number (-1 to quit) : 5
 Current total: 5
 Current average: 5
Input a number (-1 to quit) : 6
 Current total: 11
 Current average: 5.5
Input a number (-1 to quit) : -1
 
Exit code: 0 (normal program termination)
Last edited on
ThankYou bro ^_^ But i need this kind of output can you make me one like that :)
Input No 1:10
Input No2:20
Input No3:30

average is:20
Sum is:60
closed account (48T7M4Gy)
If you want to store the numbers as you go then use an array. Beyond that, sorry, I think I've given you enough.
I finish it bro thankyou so much btw i didn't use array :)
closed account (48T7M4Gy)
Excellent bro, well done, best wishes :)
Topic archived. No new replies allowed.