Place Your Query
-
C Programming
-
- Control flow-based C Programs
- Enjoy Conditional Programming in C using If...else and switch statement.
- Good, Now write a C program to display "Hello World" on the screen.
- Write a C program to display Your Name, Address and City in different lines.
- C program to find sum of n numbers
- Write a C program For Addition of two numbers using Function.
- Write a Program to Find Simple Interest and Compound Interest.
- Write a Program to Convert Celsius To Fahrenheit and Vice Versa.
- Write a Program to Find Area and Perimeter of Circle, Triangle, Rectangle and Square.
- Write a Program to Swap Two Numbers Using Temporary Variables and Without Using Temporary Variables
- Write a C Program to Design a Simple Menu Driven Calculator
- Simple Expression Based C Programs
-
- 7. Components of C language
- 1. Introduction to C Programming Language
- 10. Operator Precedence and Associativity
- 11. Comments in C Programming
- 14. Fundamental Control Structure Statements in C [Part-1]
- 15. Fundamental Control Structure Statements in C [Part-2]
- 16. Looping Statements [Fundamental Control Structure Statements in C. #Part-3]
- 17. Keyword break, continue, return and exit [Fundamental Control Structure Statements in C. #Part-4]
- 2. Computer Languages
- 3. Interpreters vs Compilers vs Assemblers in programming languages
- 4. C Program Structure
- 5. Compile and Execute C Program
- 6. Errors in C Program
- 8. C Datatypes
- 9. Operators in C
- Control flow-based C Programs
- Demystifying Bit Masking: Unlocking the Power of Bitwise Operators in C-Language with best Examples.
- 18. Fundamentals of C Functions
- Show Remaining Articles (3)Collapse Articles
-
-
Java
-
AI ML
-
FAQs
- A Program to find Tokens in C code?
- What are the Common Coding Mistakes.
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
-
Python Interview Questions
- Easy Learning, Python QAs for Beginner’s to Pro Part-1
- Easy Learning, Python QAs for Beginner’s to Pro Part-2
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-1
- Easy Learning, Python Strings QAs for Beginner’s to Pro Part-2
- Easy Learning, Python String Functions QAs for Beginner to Pro Part-3
C program to find sum of n numbers
Hello, this is Milind Bhatt. Here I’m going to show you how to write a simple C program to find the sum of n numbers. This is a common problem that you might encounter in your programming journey, and it can be solved using a simple algorithm. Let’s get started!
First, we need to declare some variables that we will use in our program. We need a variable to store the value of n, which is the number of terms we want to add. We also need a variable to store the sum of the terms, which we will initialize to zero. And we need a variable to store the current term, which we will use in a loop.
Next, we need to ask the user to enter the value of n. We can use the scanf function to read the input from the keyboard and store it in our n variable. We also need to check if the input is valid, that is, if it is a positive integer. If not, we can print an error message and exit the program.
Then, we need to write a loop that will iterate from 1 to n and add each term to the sum. We can use a for loop for this purpose, and assign the loop variable i to our current term variable. Inside the loop, we simply add the current term to the sum and update it.
Finally, we need to print the result of our calculation. We can use the printf function to display the sum on the screen.
That’s it! We have written a C program to find the sum of n numbers using a simple algorithm. Here is the complete Algorithm and code for your reference:
Algorithm
Algorithm for C program to find sum of n numbers
- Step 1: Declare and initialize variables n, sum, and i.
- Step 2: Prompt the user to enter the value of n.
- Step 3: Read the value of n from the user.
- Step 4: Use a looping statement to iterate from 1 to n.
- Step 4.1: Add the current value of i to the sum.
- Step 5: Display the value of sum.
Program to find sum of n numbers
C program to find sum of n numbers using for loop
//C program to find sum of n numbers using for loop
// C Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n, sum = 0, term;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input.\n");
return 1;
}
for (int i = 1; i <= n; i++)
{
printf("Enter the value for Term no %d : ");
scanf("%d", &term);
sum = sum + term;
}
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}
Output
Enter the value of n: 5
Enter the value for Term no 1 : 10
Enter the value for Term no 2 : 20
Enter the value for Term no 3 : 30
Enter the value for Term no 4 : 40
Enter the value for Term no 5 : 50
The sum of the given 5 numbers is 150.
--------------------------------
Process exited after 16.05 seconds with return value 0
Press any key to continue . . .
C program to find sum of n numbers using while loop
//C program to find sum of n numbers using while loop
// Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n, sum = 0, term;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input.\n");
return 1;
}
int i = 1;
while ( i <= n )
{
printf("Enter the value for Term no %d : ",i);
scanf("%d", &term);
sum = sum + term;
i++;
}
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}
Output
Enter the value of n: 5
Enter the value for Term no 1 : 10
Enter the value for Term no 2 : 20
Enter the value for Term no 3 : 30
Enter the value for Term no 4 : 40
Enter the value for Term no 5 : 50
The sum of the given 5 numbers is 150.
--------------------------------
Process exited after 16.05 seconds with return value 0
Press any key to continue . . .
C program to find sum of n numbers using do…while loop
//C program to find sum of n numbers using do...while loop
// Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n, sum = 0, term;
printf("Enter the value of n: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input.\n");
return 1;
}
int i = 1;
do
{
printf("Enter the value for Term no %d : ",i);
scanf("%d", &term);
sum = sum + term;
i++;
}while ( i <= n );
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}
Output
Enter the value of n: 2
Enter the value for Term no 1 : 4
Enter the value for Term no 2 : 5
The sum of given 2 numbers is 9.
--------------------------------
Process exited after 5.508 seconds with return value 0
Press any key to continue . . .
C program to find sum of n numbers using function
//C program to find sum of n numbers using funtion
// Program@EzyLearning.in
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
int sum= doSum(n);
printf("The sum of given %d numbers is %d.\n", n, sum);
return 0;
}// end main
int doSum(int count)
{int sum=0, term;
if (count <= 0)
{
printf("Invalid input.\n");
return 1;
}
int i = 1;
do
{
printf("Enter the value for Term no %d : ",i);
scanf("%d", &term);
sum = sum + term;
i++;
}while ( i <= count );
return sum;
}// end funtion
Output
Enter the value of n: 4
Enter the value for Term no 1 : 40
Enter the value for Term no 2 : 45
Enter the value for Term no 3 : 67
Enter the value for Term no 4 : 43
The sum of given 4 numbers is 195.
--------------------------------
Process exited after 20.68 seconds with return value 0
Press any key to continue . . .
Conclusion
Congratulations! You have successfully written a C program to find sum of n numbers using simple for() loop, while() loop, do…while() loop and functionstatement. I hope you enjoyed this blog post and learned something new. If you have any questions or feedback, please leave a comment below.
Thank you for reading!
EzyLearning!! Happy Learning!!