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
Social Plugin