1. Coding in JAVA
TCS NQT 2019
By
PHANEENDRA BOLLA
(Bachelor of Technology)
In
Computer Science and Engineering
2. Write a program to find out the given year is prime number or not.
If the given input is leap year print Prime Number or Not Prime
Number.
Conditions:
read the input from the user and check whether the given number is
positive or negative by passing the input to check().
if it is negative number then print “Try Number again” and take another
input from the user.
Repeat till the given input is positive.
If the given input is positive number then pass the input to prime()
return the values to main()
Input: 2
Output: Prime Number
Note: if the input is not an integer (i.e., alphabets) then print Wrong
Number
3. Algorithm:
Step 1:
read the input n;
Step 2:
pass n to check();//to check the input is positive or negative
check(){
if n is positive
pass n to prime();
else
pass n to check();
}
Step 3:
prime(){
if n is prime
return true;
else
return false;
}
4. Program:
import java.util.*;
class PrimeNum
{
public static void main(String[] args) throws Exception
{
Scanner sc= new Scanner(System.in);
PrimeNum p= new PrimeNum();
int n;
try
{
n=sc.nextInt();
boolean res=p.check(n);
if(res)
{
System.out.println("Prime Number");
System.exit(0);
}