Category : Java | Sub Category : Java Programs | By Prasad Bonam Last updated: 2020-10-03 14:28:09 Viewed : 921
System
class contains
several useful class fields and methods. It cannot be instantiated.System
class are standard input, standard output, and error output streams; access to
externally defined properties and environment variables; a means of loading
files and libraries; and a utility method for quickly copying a portion of an
array.Scanner
breaks its input
into tokens using a delimiter pattern, which by default matches whitespace. The
resulting tokens may then be converted into values of different types using the
various next methods.Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
PrimeNumberForLoop.java
package
runnerdev;
import
java.util.Scanner;
public class
PrimeNumberForLoop {
public static void main(String[] args) {
System.out.print("Enter
a number to check prime : ");
Scanner sc = new
Scanner(System.in);
int givenNumber = sc.nextInt();
boolean flag = false;
for (int i = 2; i <=
givenNumber / 2; ++i) {
if (givenNumber % i == 0)
{ // condition for non-prime number
flag = true;
break;
}
}
if (!flag)
System.out.println(givenNumber + "
is a prime number.");
else
System.out.println(givenNumber + "
is not a prime number.");
}
}
Out Put:
package
runnerdev;
import
java.util.Scanner;
public class
PrimeCheckWhileLoop {
public static void main(String[] args) {
System.out.print("Enter
a number to check prime : ");
Scanner sc = new
Scanner(System.in);
int givenNumber = sc.nextInt();
int i = 2;
boolean flag = false;
while (i <=
givenNumber / 2) {
if (givenNumber % i == 0)
{// condition for non-prime number
flag = true;
break;
}
++i;
}
if (!flag)
System.out.println(givenNumber + "
is a prime number.");
else
System.out.println(givenNumber + "
is not a prime number.");
}
}
Out Put: