Hello friends, today I will tell you through this tutorials how you can calculate sum natural numbers through c program language. So let’s go.
The positive numbers 1, 2, 3… are known as natural numbers. The sum of natural numbers up to 10 is: You can calculate sum natural number in 2 ways.
- C for Loop
- C while Loop
Sum of Natural Numbers Using for Loop
The first method is that you can easily calculate the sum natural number through the c program via for loop. Now you are told by the example below. How you can sum sum natural numbers with the help of for loop Can calculate by c language.
#include <stdio.h> int main() { int n, i, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); for (i = 1; i <= n; ++i) { sum += i; } printf("Sum = %d", sum); return 0; }
Sum of Natural Numbers Using while Loop
The second method is that you can easily calculate the sum natural number through the c program via while loop. Now you are told by the example below. How you can sum sum natural numbers with the help of while loop Can calculate by c language.
#include <stdio.h> int main() { int n, i, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); i = 1; while (i <= n) { sum += i; ++i; } printf("Sum = %d", sum); return 0; }
Output
Enter a positive integer: 100 Sum = 5050