Print Prime Numbers in Range in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main(){
    int l,r; scanf("%d%d",&l,&r);
    for(int i=l;i<=r;i++){
        int p=1;
        if(i<2) p=0;
        for(int j=2;j*j<=i;j++)
            if(i%j==0){ p=0; break; }
        if(p) printf("%d ",i);
    }
    return 0;
}

C Output

Input:  
10 30 

Output:  
11 13 17 19 23 29


C++ Program

#include <bits/stdc++.h>
using namespace std;
int main(){
    int l,r; cin>>l>>r;
    for(int i=l;i<=r;i++){
        bool prime = i>1;
        for(int j=2;j*j<=i;j++)
            if(i%j==0){ prime=false; break; }
        if(prime) cout<<i<<" ";
    }
}

C++ Output

Input:  
2 15 

Output:  
2 3 5 7 11 13


JAVA Program

import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int l=sc.nextInt(), r=sc.nextInt();
    for(int i=l;i<=r;i++){
      boolean prime = i>1;
      for(int j=2;j*j<=i;j++)
        if(i%j==0){ prime=false; break; }
      if(prime) System.out.print(i+" ");
    }
  }
}

JAVA Output

Input:  
50 70 

Output:  
53 59 61 67


Python Program

l,r=map(int,input().split())
for i in range(l,r+1):
    if i>1:
        for j in range(2,int(i**0.5)+1):
            if i%j==0: break
        else:
            print(i,end=" ")

Python Output

Input:  
90 110  

Output:  
97 101 103 107 109


In-Depth Learning – Entire Concept in Paragraphs
Example
If you consider the range 10 to 30, the prime numbers are 11, 13, 17, 19, 23, and 29. A prime number is a number greater than 1 that has no divisors other than 1 and itself. The code tests each number in the range and checks for divisibility only up to its square root for efficiency.

Real-Life Analogy
Imagine prime numbers as "VIP guests" at a club — they can only come in if they possess precisely two invitations: one from themselves and one from the manager (1). If anyone else can get them in (a divisor), they're no longer exclusive and aren't permitted at the prime club.

Why It Matters
Prime numbers are the backbone of modern encryption algorithms like RSA. Without prime numbers, secure online communication wouldn’t exist in its current form. They’re also essential in hashing, pseudo-random number generation, and advanced algorithms in computer science.

Learning Insights
This issue is instructive on nested loops, conditional checks, and optimization (divisibility checks up to the square root, not the number) and introduces primality testing, a key aspect of number theory and competitive programming.

Interview Relevance and Real Projects
In interviews, generating prime numbers is a traditional warm-up, but the way you optimize it decides how ready you are. An interviewer may ask you to extend this to work with very big ranges, which introduces ideas like Sieve of Eratosthenes or Segmented Sieve. In practical projects, prime checks are utilized in cryptography, load balancing, hashing functions, and mathematical modeling.

Why This Iterative Method Works Well
The code eschews superfluous tests by checking divisors only up to the square root of each number. This maintains time complexity as O((r-l+1) * √n), which is good for small to moderate intervals. For very large intervals, fancier sieve-based techniques are required, but for all-around programming and newbie learning, this iterative solution is ideal.

SEO-Friendly Summary
This C, C++, Java, and Python prime number range program illustrates the most simple and efficient method for listing all primes between two input values using a maximally optimized trial division approach. It's ideal for first-time learners of nested loops and primality tests, and just as useful for interview prep and competitive programming. From number theory basics to cryptographic uses, prime numbers are an essential concept every programmer needs to master, and this simple iterative solution is a perfect place for both educational discovery and practical application.