Sum of the input numbers

Ask user to enter numbers and script will print sum of all user entered numbers. User can exit entering numbers by entering 0.

We can ask user to enter numbers by using a WHILE loop. Each time the loop will check the user entered number if it is equal to 0. While loop will exit if the user entered number is 0.
#include <stdio.h>
int main(void){
int sum,x;

while(x != 0){
printf("\n Enter a number :  ");
scanf("%d",&x);
sum=sum+x;
}
printf("Sum of the entered  numbers : %d ", sum);
return 0;
}
Here is one sample output. You can enter different numbers and check the output.
 Enter a number :  4

 Enter a number :  5

 Enter a number :  7

 Enter a number :  0
Sum of the entered  numbers : 16


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer