Write a program to accept any 20 numbers and display only those numbers which are prime.
Answers
Answer:
Display Prime Numbers Between Two Intervals
#include <stdio.h>
int main() {
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Prime numbers between %d and %d are: ", low, high);
// iteration until low is not equal to high
while (low < high) {
flag = 0;
// ignore numbers less than 2
if (low <= 1) {
++low;
continue;
}
// if low is a non-prime number, flag will be 1
for (i = 2; i <= low / 2; ++i) {
if (low % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
// to check prime for the next number
// increase low by 1
++low;
}
return 0;
}
Output
Enter two numbers(intervals): 20
50
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47
Question:-
Write a program to accept 20 numbers and display only those numbers which are prime.
Program:-
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[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 20 elements....");
int a[]=new int[20];
for(int i=0;i<20;i++)
{
System.out.print(">> ");
a[i]=sc.nextInt();
}
System.out.print("Prime numbers are: ");
for(int i=0;i<20;i++)
{
if(isPrime(a[i]))
System.out.print(a[i]+" ");
}
}
}