WAP to calculate the average of +ve number and average of -ve number until hit 0.

Write a program to calculate the average of positive number and average of negative number until hit zero.

#include<stdio.h>
#include<conio.h>

int main() {
	int n,pcount=0,ncount=0;
	float psum=0,nsum=0,pavg,navg;
	while(1) {
		printf("Enter your numbers: ");
		scanf("%d",&n);
		if (n==0)
		break;
		if (n>0) {
			psum+=n;
			pcount++;
		} else {
			nsum+=n;
			ncount++;
		}
	}
	pavg=psum/pcount;
	navg=nsum/ncount;
	printf("Average of positive number is %.2f\n",pavg);
	printf("Average of negative number is %.2f",navg);
	return 0;
}

Output

WAP to calculate the average of +ve number and average of -ve number until hit 0.
Average calculation
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments