Labels

Monday, December 7, 2015

Sum of n numbers;

To find the sum of n numbers;

#include <stdio.h>
int main()
{
int n,sum=0;
printf("Enter value of n (the highest number): ");
scanf("%d",&n);
for (int i=0; i<=n; i++)
{
sum=i+sum;
}
printf("Sum of %d \n",sum);
return 0;
}
Sum of n numbers


To compile this program in Linux:

  1. Save this file with extension .c
  2. g++ compiler come pre installed in linux. Type this in terminal g++ sum.c -o sum. Here, I have assumed that the file name is sum.c. -o fact will provide me with an output file with extension exe file and name fact.
  3. Now, run this file typing ./sum and press enter.
To compile this program using cmd:

  1. Save this file with extension.c
  2. Type this in the cmd g++ sum.c -o sum.
  3. To run type sum and press enter.
Note: To use g++ compiler in windows user has to install these compiler by itself as g++ compiler does not come per installed in windows operating system.

No comments:

Post a Comment