Print prime numbers in a given range(1 to n)

Prime Number:- A number which only has two factors-itself and 1.

Example:-

 2, 3, 5, 7, 17........

Problem statement:- Program to find out all prime numbers present within a given range.

Sample Input/Output:-

Sample Input First: 2 10                      Sample Output First: 2 3 5 7

Sample Input Second: 50 100             Sample Output Second53 59 61 67 71 73 79 83 89 97

Data requirement:-

   Input Data:- fristnumber, endnumber

  Output Data:-i

  Additional Data:- j, count

Program in C
  
Here is the source code of the C Program to Find out all primes numbers present within a given range.

Code:

#include <stdio.h>
#include<math.h>
int main()
{
  int fristnumber, endnumber, i,j;
  printf ("Enter first number\n");
  scanf ("%d", &fristnumber);
   printf ("Enter last number\n");
  scanf ("%d", &endnumber);
  printf ("Prime numbers between %d and %d are: ",fristnumber,endnumber);
   for (j =fristnumber; j <=endnumber; j++)
    {
       int count=0;
      for (i = 2; i <= sqrt(j);i++)
{
  if (j % i == 0)
    {
      count++;
      break;
    }
}
      if (count == 0)
printf ("%d ",j);
    }

  return 0;

}

Input/Output:
Enter first number
2
Enter last number
10
Prime numbers between 2 and 10 are: 2 3 5 7

Program in C++
  
Here is the source code of the C++ Program to print prime numbers in a given range.

Code:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int range1,range2;
    cout<<"Enter a range to find all prime numbers within that range:";
    cin>>range1>>range2;
    int count=0;
    cout<<"Prime numbers between "<<range1<<" and "<<range2<<" are: ";
    for(int j=range1;j<=range2;j++)
    {
    for(int i=2;i<=sqrt(j);i++)
    {
       if(j%i==0)
        count++;
    }
   if(count==0)
     {cout<<j<<" ";
     }
    count=0;
    }
}

Input/Output:
Enter a range to find all prime numbers within that range:
50
100
Prime numbers between 50 and 100 are: 53 59 61 67 71 73 79 83 89 97

Program in Java
  
Here is the source code of the C Program to print prime numbers in a given range.

Code:

import java.util.Scanner;
public class PrimesNumberInRange {

public static void main(String[] args) {

       Scanner cs=new Scanner(System.in);
       int fristnumber, endnumber, i,j;
       System.out.println("Enter the First number:");
       fristnumber=cs.nextInt();
       System.out.println("Enter the Last number:");
       endnumber=cs.nextInt();
 System.out.println("Prime numbers between "+fristnumber+" and "+endnumber+" are:");
        for (j =fristnumber; j <=endnumber; j++)
         {
            int count=0;

           for (i = 2; i <=Math.sqrt(j);i++)

      {
        if (j % i == 0)
          {
            count++;
            break;
          }
      }
           if (count == 0)
      System.out.print(j+" ");
         }      
       cs.close();
}
}

Input/Output:
Enter the First number:
200
Enter the Last number:
300
Prime numbers between 200 and 300 are:
211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 

Program in Python
  
Here is the source code of the Python Program to Find out all primes numbers present within a given range.

Code:

import math
print("Enter a range to find all prime numbers within that range:")
range1=int(input())
range2=int(input())
print("Prime numbers between ",range1," and ",range2," are: ")
for j in range(range1,range2+1): 
     count=0 for i in range(2,int(math.sqrt(j))+1): 
     if j%i==0: 
       count+=1
if count==0: 
print(j,end=" ")

Input/Output:
Enter a range to find all prime numbers within that range:
500
600
Prime numbers between  500  and  600  are:
503 509 521 523 541 547 557 563 569 571 577 587 593 599



Post a Comment

0 Comments