Sum of n numbers in C
In this post, you will learn C program to find the sum of n numbers.
In mathematics, the natural numbers are those numbers used for counting and ordering. Natural numbers are all positive numbers like 1, 2, 3, 4, and so on. They are the numbers you usually count and they continue till infinity.
The sum of n natural numbers is:
1 + 2 + 3 + .... + n
Sum of n numbers in C using for loop
In the given C program, we take the number input from the user and store it in the variable num. Then, we use for loop to calculate the sum up to the num.
#include <stdio.h>
int main() {
int num, i, sum = 0;
printf("Please enter the number: ");
scanf("%d", &num);
for (i = 1; i <= num; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
Output of the above code:
Please enter the number: 15
Sum = 120
Please enter the number: 33
Sum = 561
Sum of n numbers in C using while loop
In the given C program, we take the number input from the user and store it in the variable num. Then, we use while loop to calculate the sum up to the num.
#include <stdio.h>
int main() {
int num, i, sum = 0;
printf("Please enter the number: ");
scanf("%d", &num);
i = 1;
while (i <= num) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}
Output of the above code:
Please enter the number: 19
Sum = 190
Please enter the number: 22
Sum = 253
Sum of n numbers in C using do-while loop
In the given C program, we take the number input from the user and store it in the variable num. Then, we use do while loop to calculate the sum up to the num.
#include <stdio.h>
int main() {
int num, i, sum = 0;
do {
printf("Please enter the number: ");
scanf("%d", &num);
} while (num <= 0);
for (i = 1; i <= num; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
Output of the above code:
Please enter the number: 18
Sum = 171
Sum of n numbers in C using recursion
Here is the C program to find the sum of n numbers using the recursion function. A recursion function is a function that is called by itself.
#include <stdio.h>
int sumOfNumbers(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum of numbers: %d", sumOfNumbers(num));
return 0;
}
int sumOfNumbers(int n) {
if (n != 0)
return n + sumOfNumbers(n - 1);
else
return n;
}
Output of the above code:
Enter a positive integer: 9
Sum of numbers: 45
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative