Write a c program to find sum of 10 numbers and skip negative numbers using for loop. In this c example, for loop iterate from 1 to 10, the if statement checks whether the number is less than zero. If true, the continue statement will skip that number to perform addition.
#include <stdio.h> int main() { int num, sum = 0; printf("Please Enter the 10 Numbers\n"); for(int i = 1; i <= 10; i++) { printf("Number %d = ", i); scanf("%d", &num); if(num < 0) { continue; } sum = sum + num; } printf("\nSum by Skipping Negative Numbers = %d\n", sum); }
This c program reads ten numbers from user input and calculates the sum of 10 numbers by skipping the negative numbers using a while loop.
#include <stdio.h> int main() { int i, num, sum = 0; printf("Please Enter the 10 Numbers\n"); i = 1; while(i <= 10) { printf("Number %d = ", i); scanf("%d", &num); if(num < 0) { i++; continue; } sum = sum + num; i++; } printf("\nSum by Skipping Negative Numbers = %d\n", sum); }
Please Enter the 10 Numbers
Number 1 = 12
Number 2 = 23
Number 3 = -99
Number 4 = 14
Number 5 = -3
Number 6 = 77
Number 7 = 88
Number 8 = -98
Number 9 = -14
Number 10 = 109
Sum by Skipping Negative Numbers = 323