#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); //to clear the screen
int i,n,x,sum=0;
cout<<“How many numbers?”;
cin>>n;
for(i=1;i<=n;++i)
{
cout<<“nEnter number “<<i<<“: “;
cin>>x;
sum+=x;
}
x=sum/n;
cout<<“nnAverage=”<<x;
getch(); //to stop the screen
}
I am trying to find the average of positive numbers entered by a user using break in c++, but am not getting it right. What is my problem?
Q: @ Write a program to find the average of n positive numbers entered by a user. But if a negative number is entered, the average of the positive numbers is displayed and end the program.
Here is the code:
#include
using namesapce std;
int main()
{float num,average,sum=0;
int i,n;
cout<>n;
for(i=1;1<=n;++1)
{cout<<"enter n:"<>num;
if(num<0.0){break;
}
average=sum/(i-1);
cout<<"average="<<average;
return 0;
}}