c program to print the sum of negative numbers, sum of positive odd numbers and sum of positive even numbers

Programming Example

Write a program in C to print the sum of negative numbers, sum of positive odd numbers and sum of positive even numbers from a list of numbers entered by the user. The list terminates when the user enters a Zero.

Solution

#include<stdio.h>
int main()
{
	int num,sum_negative=0,sum_pos_odd=0,sum_pos_even=0;
	do
	{
		printf("Enter Any Number");
		scanf("%d",&num);
		if(num<0)
		{
			sum_negative=sum_negative+num;
		}
		else
		{
			if(num%2==0)
			{
				sum_pos_even=sum_pos_even+num;
			}
			else
			{
				sum_pos_odd=sum_pos_odd+num;
			}
		}
	}while(num!=0);
	printf("\nSum of Negative Number: %d",sum_negative);
	printf("\nSum of Positive Even Number: %d",sum_pos_even);
	printf("\nSum of Positive Odd Number: %d",sum_pos_odd);
	return 0;
}
Output

Enter Any Number5

Enter Any Number6

Enter Any Number4

Enter Any Number-3

Enter Any Number-6

Enter Any Number4

Enter Any Number3

Enter Any Number2

Enter Any Number1

Enter Any Number0


Sum of Negative Number: -9

Sum of Positive Even Number: 16

Sum of Positive Odd Number: 9

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad