Prime Numbers between an interval | MATLAB Tutorial

Prime Numbers between an interval | MATLAB Tutorial

Printing all prime numbers between an interval using MATLAB

What is a Prime number?

  • A Prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
  • It should not be divisible by any other number, else it is composite.
  • 1 is neither prime nor composite.

Example:- Prime numbers between

  1. 1 and 10 are
    2, 3, 5, 7
  2. 10 and 20 are
    11, 13, 17, 19
  3. 30 and 40 are
    31, 37.
  4. 100 and 200 are
    101103107109113127131137139149,151157163167173179181, 191, 193197,199.



a=input('Enter first no.');
b=input('Enter second no.');

for j= 0:b-a
   for i=2:(a+j)/2;
      if (mod((a+j),i)==0)
   if flag==0



  • The user should enter two numbers which then becomes the two extreme points of an interval.
  • Those two numbers are then stored in two variables.
  • The number entered first is divided by every number from 2 to half of the number and then the remainder is taken.
  • If the remainder results in 0 then the ‘flag’ is made to be one. This ‘flag’ is used to decide whether a certain number is prime or not.
  • If the number makes the flag as one it is a composite number, whereas if it turns out that a number doesn’t turn or change the flag to one then it is not a composite number.
  • Above process is repeated for every number between the interval entered by the user and the desired number is printed on the screen.



1. Numbers between 10-100

2.Numbers Between 200-300


3. Error when a greater number is added first and a smaller number later

I am currently pursuing my btech in Electronics and communication engineering from Amrita Vishwa Vidyapeetham. My areas of interest are signal processing, Communication systems, Control system.