Last Updated: 10 Feb, 2021

Total Prime

Easy
Asked in company
LTIMindtree

Problem statement

Write a program to find the total number of a primes number in a given interval.

Given two integers S and E, count all primes between S and E.

Input format :
The only line of input contains two integers S and E separated by a single space.
Output format :
The only line of the output prints the total number of primes.
Constraints
1 <= N <= 100

Approaches

01 Approach

  • Create an outer loop where we iterate from S to E(inclusive).
  • We create a flag variable whose value is set to True in the beginning.
  • Now in a nested loop(inner loop) we iterate from 2 to i / 2 and check if i is divisible by j , if i becomes divisible by j, then it means i has a factor so it is not a prime and thus set the value of the flag to False and break from this inner loop.
  • At the end of the outer loop, we check if the value of the flag is True increment the value of count denoting the number of primes by 1.