Check whether the sum of all printed prime numbers are equal to the given input or not ?
Sample Input: 5
Sample Output:
Prime list: 1, 2, 3, 5,
The sum is:11
The sum is not equal to 5
Source Code:
#include<stdio.h>
#include<math.h>
#include<math.h>
int main()
{
long long num, temp, ar[100000]={0}, i, j, sum=0;
printf("Enter the Input: ");
scanf("%lld", &num);
temp=(int)sqrt(num);
for(i=4;i<=num;i+=2)
{
ar[i]=1;
}
for(i=3;i<=temp;i+=2)
{
for(j=i*i;j<=num;j+=(2*i))
{
ar[j]=1;
}
}
printf("Prime list: ");
for(i=1;i<=num;i++)
{
if(ar[i]==0)
{
printf("%lld, ", i);
sum+=i;
}
}
scanf("%lld", &num);
temp=(int)sqrt(num);
for(i=4;i<=num;i+=2)
{
ar[i]=1;
}
for(i=3;i<=temp;i+=2)
{
for(j=i*i;j<=num;j+=(2*i))
{
ar[j]=1;
}
}
printf("Prime list: ");
for(i=1;i<=num;i++)
{
if(ar[i]==0)
{
printf("%lld, ", i);
sum+=i;
}
}
printf("\nThe Sum is : %lld", sum);
if(sum==num)
printf("\nThe sum is equal to %lld", num);
else
printf("\nThe sum is not equal to %lld", num);
return 0;
}
0 comments:
Post a Comment