Sum of n prime numbers;
#include <stdio.h>
int main()
{
int n,sum=0;
printf("Enter the value of n(highest number): ");
scanf("%d",&n);
for (int i=0;i<=n;i++)
{
if(i%2==0)
{
sum=i+sum;
}
else
{
sum=sum;
}
}
printf("The sum of prime number: %d \n",sum);
return 0;
}
To compile this program in Linux:
#include <stdio.h>
int main()
{
int n,sum=0;
printf("Enter the value of n(highest number): ");
scanf("%d",&n);
for (int i=0;i<=n;i++)
{
if(i%2==0)
{
sum=i+sum;
}
else
{
sum=sum;
}
}
printf("The sum of prime number: %d \n",sum);
return 0;
}
Sum of prime 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++ primesum.c -o primesum. Here, I have assumed that the file name is primesum.c. -o fact will provide me with an output file with extension exe file and name fact.
- Now, run this file typing ./primesum and press enter.
- Save this file with extension.c
- Type this in the cmd g++ primesum.c -o primesum.
- To run type primesum and press enter.
No comments:
Post a Comment