TCS-NQT-C-Program-Consecutive Prime Sum
Consecutive Prime Sum
Some prime numbers can be expressed as a sum of other consecutive prime numbers.
- For example
- 5 = 2 + 3,
- 17 = 2 + 3 + 5 + 7,
- 41 = 2 + 3 + 5 + 7 + 11 + 13.
Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.
Write code to find out the number of prime numbers that satisfy the above-mentioned property in a given range.
C-Program:
#include <stdio.h>
int main()
{
int n,i,j,count=0,k=0;
scanf("%d",&n);
int array[n];
for(i=2;i<=n;i++)
{
count=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
count=1;
break;
}
}
if(count==0)
{
array[k]=i;
k++;
}
}
int pcount=0;
for(i=0;i<k;i++)
{
int sum=0;
for(j=0;j<i;j++)
{
sum=sum+array[j];
if(sum==array[i])
{
pcount++;
}
}
}
printf("%d",pcount);
return 0;
}
INPUT : 43
OUTPUT : 4
Comments
Post a Comment