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

Program to find Volume of given box in java

class volume { public static void main (String args []) { int l=5,b=6,h=10; int volume; volume=l*b*h; System.out.println (" vol...