C program to print sum of digits using for loop

C program to print sum of digits using for loop. This program will print the sum of numbers in c programs. To add number which is entered by the users like if you enter the number 3 it will print number 6.

#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

What is 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.

What is AND operator in c program and segmentation fault in c program

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.

What is return statement in c program

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.