In this tutorial you will learn about the C Program to Add n Numbers and its application with practical example.
C Program to Add n Numbers
In this tutorial, we will learn to create a C program that will Add n Numbers using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Arithmetic operations in C Programming.
Program to Add n Numbers
In c programming, it is possible to take numerical input from the user and Add n Numbers with the help of a very small amount of code. The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy. In C language, we can do many arithmetic operations.
With the help of this program, we can Add n Numbers.
Here,
We will first take the input
The second will make a series and add the numbers.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Passing that number to a for loop. 4. Adding the series to a variable. 5. Printing the result numbers. 6. End Program. |
Program to Add n Numbers:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { //declaring the variables for the program. int no, i, sum = 0; //taking input from the user a positive number printf("Enter any positive integer value : "); //scanning the input from the user. scanf("%d", &no); //using the for loop for the calculating the sum of n numbers for (i = 1; i <= no; ++i) { sum += i; } //Printing the output Sum for the n nubers printf("Sum = %d", sum); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- no = it will hold the integer value.
- i = it will hold the integer value.
- sum = it will hold the integer value.
Input number from the user.
Program Logic Code.
Printing output