P029.c (519B)
1 /* WAP to perform addition of first n natural numbers. sum = 1 + 2 + 3 + ... */ 2 3 #include <stdio.h> 4 int main() 5 { 6 int num, i = 0, result = 0; 7 printf("Enter the value for n : "); 8 if (scanf("%d", &num) != 1) 9 { 10 printf("\nPlease enter a number."); 11 return 1; 12 } 13 if (num < 1) 14 { 15 printf("\nPlease enter a positive number."); 16 return 1; 17 } 18 while (i <= num) 19 { 20 result = result + i; 21 i++; 22 } 23 printf("\nResult : %d", result); 24 return 0; 25 }