# Mathematical Calculations: Prime Numbers | iMocha coding questions

## Mathematical Calculations: Prime Numbers

Find the sum of all prime numbers between two numbers X and Y, inclusive of both X and Y.

Note :-

Prime Number: Any number greater than 1, whose only factors are 1 and itself.

Function Description :

In the provided code snippet, implement the provided primeSum(...) method using the variables to print a single integer denoting the sum of all prime numbers between X & Y both inclusive. You can write your code in the space below the phrase "WRITE YOUR LOGIC HERE"

There will be multiple test cases running so the Input and Output should match exactly as provided.

The base output variable result is set to a default value of -404 which can be modified. Additionally, you can add or remove these output variables.

Input Format

The first line contains X.

The second line contains Y

Sample Input

1 -- denotes X

5 -- denotes Y

Output format

The output contains a single integer denoting the sum of all prime numbers between X & Y, both inclusive.

Sample output :-

10

Explanation

Prime numbers between 1 and 5 are: 2, 3, and 5.

2+3+5=10.

Hence, the output is 10.

C++ code :-

#include <iostream>
using namespace std;

// function for checking prime number
int isprime(int n)
{
if(n<2)
return 0;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
return 0;
}
return 1;
}

// function for finding the sum of all the prime numbers
int sumofprime(int x,int y)
{
int result=0;
for(int i=x;i<=y;i++)
{
if(isprime(i)==1)
result+=i;
}
return result;
}
int main() {
int x,y;
cin>>x;
cin>>y;
cout<<sumofprime(x,y);
return 0;
}

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-

C-tutorial:-

See more:-