SlideShare a Scribd company logo
1 of 10
Coding in JAVA
TCS NQT 2019
By
PHANEENDRA BOLLA
(Bachelor of Technology)
In
Computer Science and Engineering
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
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;
}
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);
}
else if(!res)
{
System.out.println("Not Prime Number");
System.exit(0);
}
}
catch(InputMismatchException e)
{
System.out.println("Wrong Number");
}
}
boolean check(int n)
{
PrimeNum p=new PrimeNum();
Scanner sc=new Scanner(System.in);
boolean res= false;
if(n>=0)
{
res= p.prime(n);
}
else
{
System.out.println("Try Number Again");
int m=sc.nextInt();
res=p.check(m);
}
return res;
}
boolean prime(int n)
{
int i;
if(n==0||n==1)
return false;
else
{
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
}
if(i==n)
return true;
else
return false;
}
}
Output:
2
Prime Number
-----------------------------------------------------------------------------------------------------
-1
Try Number Again
-10
Try Number Again
6
Not Prime Number
-----------------------------------------------------------------------------------------------------
Ab92g
Wrong Number
-----------------------------------------------------------------------------------------------------
To be continued…
To be continued…

More Related Content

What's hot

Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
test(3)arithmetic in c
test(3)arithmetic in ctest(3)arithmetic in c
test(3)arithmetic in cJaya Malathy
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structuresMicheal Ogundero
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expressionAkhil Ahuja
 
Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversionPdr Patnaik
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markersMicheal Ogundero
 
Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackSoumen Santra
 
Data structures Lecture no. 4
Data structures Lecture no. 4Data structures Lecture no. 4
Data structures Lecture no. 4AzharIqbal710687
 
Arithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded CArithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded CVikas Dongre
 
Arithmetic and logic operations in c
Arithmetic and logic operations in cArithmetic and logic operations in c
Arithmetic and logic operations in cVikas Dongre
 

What's hot (20)

Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
test(3)arithmetic in c
test(3)arithmetic in ctest(3)arithmetic in c
test(3)arithmetic in c
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
P3
P3P3
P3
 
Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversion
 
Infix to postfix
Infix to postfixInfix to postfix
Infix to postfix
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
Activity m3
Activity m3Activity m3
Activity m3
 
Infix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using StackInfix to Postfix Conversion Using Stack
Infix to Postfix Conversion Using Stack
 
4. algorithm
4. algorithm4. algorithm
4. algorithm
 
Data structures Lecture no. 4
Data structures Lecture no. 4Data structures Lecture no. 4
Data structures Lecture no. 4
 
201505 CSE340 Lecture 02
201505 CSE340 Lecture 02201505 CSE340 Lecture 02
201505 CSE340 Lecture 02
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
 
201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01
 
Arithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded CArithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded C
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Arithmetic and logic operations in c
Arithmetic and logic operations in cArithmetic and logic operations in c
Arithmetic and logic operations in c
 
201506 CSE340 Lecture 14
201506 CSE340 Lecture 14201506 CSE340 Lecture 14
201506 CSE340 Lecture 14
 

Similar to Tcs nqt 2019 p 2

C_Lab Manual_Part A.docx
C_Lab Manual_Part A.docxC_Lab Manual_Part A.docx
C_Lab Manual_Part A.docxPandiya Rajan
 
C programming assignment presentation file
C programming assignment presentation fileC programming assignment presentation file
C programming assignment presentation filesantoshkumarhpu
 
exp unit 1 internet programing.docx
exp unit 1 internet programing.docxexp unit 1 internet programing.docx
exp unit 1 internet programing.docxApkaAmitbro
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C LabNeil Mathew
 
I need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfI need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfvenkatesh24685
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfssuserc77a341
 

Similar to Tcs nqt 2019 p 2 (11)

C_Lab Manual_Part A.docx
C_Lab Manual_Part A.docxC_Lab Manual_Part A.docx
C_Lab Manual_Part A.docx
 
c programing
c programingc programing
c programing
 
Python programs
Python programsPython programs
Python programs
 
C programming assignment presentation file
C programming assignment presentation fileC programming assignment presentation file
C programming assignment presentation file
 
exp unit 1 internet programing.docx
exp unit 1 internet programing.docxexp unit 1 internet programing.docx
exp unit 1 internet programing.docx
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
I need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdfI need the code for a Java programming project. Allow the user to ty.pdf
I need the code for a Java programming project. Allow the user to ty.pdf
 
keyword
keywordkeyword
keyword
 
keyword
keywordkeyword
keyword
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
 

Recently uploaded

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Tcs nqt 2019 p 2

  • 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); }
  • 5. else if(!res) { System.out.println("Not Prime Number"); System.exit(0); } } catch(InputMismatchException e) { System.out.println("Wrong Number"); } } boolean check(int n) { PrimeNum p=new PrimeNum(); Scanner sc=new Scanner(System.in); boolean res= false; if(n>=0) { res= p.prime(n); }
  • 6. else { System.out.println("Try Number Again"); int m=sc.nextInt(); res=p.check(m); } return res; } boolean prime(int n) { int i; if(n==0||n==1) return false; else { for(i=2;i<n;i++) { if(n%i==0) break; } }
  • 8. Output: 2 Prime Number ----------------------------------------------------------------------------------------------------- -1 Try Number Again -10 Try Number Again 6 Not Prime Number ----------------------------------------------------------------------------------------------------- Ab92g Wrong Number -----------------------------------------------------------------------------------------------------