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;
}
To compile this program in Linux:
#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:
- Save this file with extension .c
- 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.
- Now, run this file typing ./sum and press enter.
- Save this file with extension.c
- Type this in the cmd g++ sum.c -o sum.
- To run type sum and press enter.
No comments:
Post a Comment