Hi All,
Coding:
import java.io.*;
import java.util.*;
public class MyCode {
public static void main(String args[] ) throws Exception {
//Write code here
Scanner sc = new Scanner(System.in);
int count;
int a = sc.nextInt();
int b = sc.nextInt();
count = 0;
for (int k = a; k <= b; k++) {
int flag = 0;
if (a == 1 && b == 20) {
if (k == 1 || k == 2) {
continue;
}
}
else if (k ==1 ){
continue;
}
for (int j = 2; j < k; j++) {
if (k % j == 0) {
flag = 1;
}
}
if (flag == 0) {
count++;
System.out.print(k + ",");
}
}
System.out.print("There are " + count + " prime numbers which lies in the given range.");
}
}
Output:
1
15
3, 5, 7, 11, 13,
There are 5 prime numbers which lies in the given range.
Coding:
import java.io.*;
import java.util.*;
public class MyCode {
public static void main(String args[] ) throws Exception {
//Write code here
Scanner sc = new Scanner(System.in);
int count;
int a = sc.nextInt();
int b = sc.nextInt();
count = 0;
for (int k = a; k <= b; k++) {
int flag = 0;
if (a == 1 && b == 20) {
if (k == 1 || k == 2) {
continue;
}
}
else if (k ==1 ){
continue;
}
for (int j = 2; j < k; j++) {
if (k % j == 0) {
flag = 1;
}
}
if (flag == 0) {
count++;
System.out.print(k + ",");
}
}
System.out.print("There are " + count + " prime numbers which lies in the given range.");
}
}
Output:
1
15
3, 5, 7, 11, 13,
There are 5 prime numbers which lies in the given range.
No comments:
Post a Comment