C Program to calculate sum of multiple positive numbers (Stop if user enters negative number)

Description

Get postive integers from  user till user enters a negative integer calculate sum of postive integers.
Input:
5
6
7
2 
1
8
-9
Ouput:
29
Input:
3
23
-76
Output:
26

Solution

#include<stdio.h>
int main()
{
	int num,sum=0;
	printf("Enter Postive numbers to sum:");
	while(1)
	{
		scanf("%d",&num);
		if(num<0)
		{
			break;
		}
		sum=sum+num;
	}
	printf("%d",sum);
	return 0;
}

Explanation

since 1 is always true, scanf statement gets input from user continously, if user enters a negative number it exit from while loop using break statement.



Interesting Pattern Programs: for more Patterns

Input:
5
Output:
*
**
***
****
*****
****
***
**
*Solution
Input:
5
Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *Solution
Input:
5
Output:
*        *
**      **
***    ***
****  ****
**********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *Solution
Input:
5
Output:
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********Solution