Programming Examples
C program to print the sum of negative numbers, sum of positive odd numbers and sum of positive even numbers
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