P029.c (655B)
1 /* 2 * Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 12 Dec 2025 3 * Repo: https://github.com/notamitgamer/bsc 4 * License: MIT 5 */ 6 7 /* WAP to perform addition of first n natural numbers. sum = 1 + 2 + 3 + ... */ 8 9 #include <stdio.h> 10 int main() 11 { 12 int num, i = 0, result = 0; 13 printf("Enter the value for n : "); 14 if (scanf("%d", &num) != 1) 15 { 16 printf("\nPlease enter a number."); 17 return 1; 18 } 19 if (num < 1) 20 { 21 printf("\nPlease enter a positive number."); 22 return 1; 23 } 24 while (i <= num) 25 { 26 result = result + i; 27 i++; 28 } 29 printf("\nResult : %d", result); 30 return 0; 31 }