Home » C Programs » Sum of n Numbers in C

Sum of n Numbers in C

Basic c program to execute the  sum of n numbers in c. This program will print the sum of numbers in c programs. To add a number which is entered by the users if you enter the number 3 it will print the number 6.

For Loop in C Program

A for loop is a repetition control structure that allows us to create a loop that runs a set number of times. The loop allows us to do an arbitrary number of steps in a single line. A loop variable is used to regulate the loop in for loop. 

AND Operator in C Program and Segmentation Fault

On two Boolean expressions, this operator conducts logical conjunction. The AND operator yields True if both expressions evaluate to True. The AND operator gives False if either or both phrases evaluate to False.

A Core Dump/Segmentation Fault is a type of error that occurs when you access memory that “doesn’t belong to you.” A core dump occurs when a piece of code tries to perform read and write operations in a read-only memory location or a released memory block.

Return Statement in C

A return statement marks the end of a function’s execution and hands control back to the calling function. The calling function continues execution at the place where the call was made. A return statement can give the caller function a value.

Sum of n Numbers in C

//sum of n numbers in c
#include <stdio.h>
int main() 
{
int n, i, total = 0;
printf("Enter the number ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) 
{
total += i;
}
printf("total = %d", total);
return 0;
}

Output

Enter the number

45

Total = 1035