Sunday, 29 July 2018

Program to input number from user ( negative or positive ) calculate addition of only negative number

Using while loop ::

#include <stdio.h>
void main ()
{
int a ;
int sum = 0 ;
clrscr () ;
while ( a ! = 0 )
{
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
sum = sum + a ;
a-- ;
}
printf ("\n %d", sum) ;
getch () ;
}

Using for loop ::

#include <stdio.h>
void main ()
{
int a ;
int sum = 0 ;
clrscr () ;
for ( ; a != 0 : a-- )
{
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
sum = sum + a ;
}
printf ("\n %d", sum) ;
getch () ;
}

Output ::

Enter number :   5 , 6 , -7 ,-8 
sum =  -7 + -8 = -15
Enter a number : 5 , 6
output = nothing 

No comments:

Post a Comment

Featured post

Kalgudi Interview Round!!

Those who cleared the second round of kalgudi got an email from exceller to complete their Kalgudi Interview Round. So here we will discuss ...