I want to apply a while loop and have written that while (a!=-1); it should exit but it’s printing “0” always and I don’t know why. Can you explain?
#include <stdio.h>
int main()
{
long num;
int a,i,j;
int arr[10000];
float x;
while( a != -1)
{
scanf("%d",&a);
int sum=0;
for(i=0;i<a;i++)
{
scanf("%d",&arr[i]);
sum = sum + arr[i];
}
x = sum%a;
if (x == 0)
{
int z = sum/a;
int y=0;
for(j=0;j<a;j++)
{
if (arr[j]>z)
{
y = (arr[j] - z) + y ;
}
}
printf("%d",y);
}
else
printf("-1");
}
}
I think it is printing y; how to avoid so that it only exits?
I got my answer actually i have to take my
scanf("%d",&a);outside while loop ;