In: Computer Science
Write a program that accepts an integer as input and then displays all the prime numbers smaller than or equal to that number.
//Java
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter n:
");
int n = sc.nextInt();
for (int i = 2; i < n; i++)
{
if(isPrime(i))
System.out.println(i);
}
}
//checking if given num is prime
private static boolean isPrime(int aI) {
for (int i = 2; i < aI;
i++)
if (aI % i ==
0)
return false;
return true;
}
}

// Cpp:
#include <iostream>
using namespace std;
//checks and returns true if given num is prime
bool isPrime(int aI) {
for (int i = 2; i < aI;
i++)
if (aI % i ==
0)
return false;
return true;
}
int main(){
int end;
cout<<"Please a numbers: ";
cin>>end;
for(int i=2;i<end;i++){
if(isPrime(i)){
cout<<i<<" ";
}
}
return 0;
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me