C++ Program that finds prime numbers between two intervals

 Code is :

 
  
 Explanation :: 
  Steps
 1 :  Declare a function with name prime_number which will take 2 integer and its return type is void.
 2 : Take two values from user and store them into variable named a and b.
 3 :Then we call the function 
 4 :: Explanation of Function definition
                                  Tools :- boolean , nested for loop , break statement
 first of all we set the flag true that  mean its value is  1 at start. Then outer loop which have variable i . In this we set the loop from starting value to ending value and set condition of flag is true. If it is still true then it will go in inner-loop otherwise just stop
 in inner loop we have variable j. we are starting loop from 2 to half of starting number . Actually we use this to check if number is divisible or not(for prime number ) so if its modulus is equal to zero then flag will be false . and loop will break . 
if modulus is not zero then flag will true and it will print the number .And this process will continue.

For example if user put starting value 10 then  it will check if its divisible by numbers starting from 2 . half of it is 5. ....
This way all prime numbers will be print out between the range set by user.

Output of code :: 



Comments

Popular posts from this blog

OOP Assignment 3 helping materials

Square root Algorithm in C++

OOP lab 7 task solution