Saturday, July 20, 2019

Prime Numbers code Question



1.      Write code to find out the number of prime numbers that satisfy the below mentioned property in a given range.

Some prime numbers can be expressed as 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.

Follow the given instructions while generating input and outputs.

Input Format:
·         First line contains a number N
Output Format:
·         Print the total number of all such prime numbers which are less than or equal to N.
Constraints:
·         1. 2
The result should be as follows:
20
2
Explanation for the output:
Below 20, there are 2 such numbers: 5 and 17.
5=2+3
17=2+3+5+7



#include <stdio.h>

int main()
{
    int i,j,no,sum;
    scanf("%d",&no);
    for(i=2;i<no;i++)
    {
        for(j=2;j<i;j++)
        {
            if(i%j==0)
            break;
        }
        if(j==i)
        {
        sum+=i;
        printf("%d+",j);
           
        }
        if(sum==no)
        {
         printf("\b=%d",sum);   
        break;
        }
    }
  

    return 0;
}

No comments:

Post a Comment