Labels

Monday, December 7, 2015

Sum of prime numbers;

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;

}

Sum of prime numbers
Sum of prime 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++ 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.
  3. Now, run this file typing ./primesum and press enter.
To compile this program using cmd:

  1. Save this file with extension.c
  2. Type this in the cmd g++ primesum.c -o primesum.
  3. To run type primesum 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