Given an integral input n, print all the numbers from 1 to n which are prime.
Example 1: Input: n=5 Output 23 5
Example 2:
Input: n=10 Output: 2357 9
Hint: Try dividing it into smaller sub-problems, just like we did in class :P
please help me
Answers
Answered by
0
Explanation:
SIMPLE PROGRAM IN PYTHON!
C = 1
FOR I IN RANGE(2,N):
IF I%2!=0:
print("__","Prime no",I)
IF C==1:
PRINT("NOT PRIME")
Answered by
1
Question:-
Write a program to print all the prime numbers between 1 to n.
Solution:-
import java.util.*;
class Prime
{
static boolean isPrime(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
return c==2;
}
public static void main(String args[])
{
int n, i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
n=sc.nextInt();
System.out.print("Prime numbers are: ");
for(i=1;i<=n;i++)
{
if(isPrime(i))
System.out.print(i+" ");
}
}
}
Similar questions
Math,
4 months ago
English,
4 months ago
Social Sciences,
4 months ago
English,
8 months ago
Music,
8 months ago
Political Science,
1 year ago
Math,
1 year ago
Science,
1 year ago