Sum of n numbers till encounters -999 using C program will check the number is equal to -999. If it is same, then it will break the statement. Otherwise it will sum the given numbers and executes the condition until the condition is unsatisfied.
//Header files
# include <stdio.h>
# include <conio.h>
void main()
{
//Program variables
int num,sum=0;
clrscr();//Function used to clear previous output
do //Unconditional statement
{
printf("Enter the number(-999 to exit) : "); //Display function
scanf("%d",&num); //Getting input function
if(num!= -999)
sum += num;
else
break;
}while(num != -999);
printf("sum = %d",sum);
getch();
}
# include <stdio.h>
# include <conio.h>
void main()
{
//Program variables
int num,sum=0;
clrscr();//Function used to clear previous output
do //Unconditional statement
{
printf("Enter the number(-999 to exit) : "); //Display function
scanf("%d",&num); //Getting input function
if(num!= -999)
sum += num;
else
break;
}while(num != -999);
printf("sum = %d",sum);
getch();
}
No comments:
Post a comment
Note: only a member of this blog may post a comment.