Kmspico Download | Official KMS Activator Website [New Version 2024] Fast and Easy Converter YouTube to MP3 Online KMSAuto Net Activator Download 2024 Immediate Byte Pro Neoprofit AI Blacksprut without borders. Discover new shopping opportunities here where each link is an entrance to a world ruled by anonymity and freedom.

How to Calculate the Sum of Natural Numbers Using C Program?

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.

  1. C for Loop
  2. 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