C program to Add N numbers

The programs below takes a positive integer (let say n) as an input from the user and calculates the sum up to n.

 

Let’s write a program to print sum of N number.

 

Step 1: Let’s open turbo C and write the following code .

 

//Add preprocessor

#include<stdio.h>

#include<conio.h>

void main()

{

long int   n,b=0,i;

 

// Clear scren

clrscr();

printf(“enter any number :”);

scanf(“%li”,&n);

for(i=0;i<=n;i++)

{

b=b+i;

}

 

// Print the Result

printf(“ sum =%li”,b);

getch();

}

 

Step 2: Save it and name it SUM.CPP

 

 

Step 3: Now compile program to check errors and correct them.

 

 

Step 4:Run the program and see output.

 

 

//output

 

output

 

Happy Coding.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tags: , , ,