[Go to site: main page, start]

0% found this document useful (0 votes)
11 views16 pages

Java Programs for Number Checks and Conditions

The document contains a list of Java programs that demonstrate various programming concepts such as checking even/odd numbers, determining positivity, finding the largest number, and implementing a simple ATM system. Each program includes input/output examples and the corresponding Java code. The programs cover basic control structures, user input handling, and conditional statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Java Programs for Number Checks and Conditions

The document contains a list of Java programs that demonstrate various programming concepts such as checking even/odd numbers, determining positivity, finding the largest number, and implementing a simple ATM system. Each program includes input/output examples and the corresponding Java code. The programs cover basic control structures, user input handling, and conditional statements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM LIST

1).WAJP to check whether an given integer number is


even ,if even print "hi" if not do nothing?

Ans) class EvenCheck {


public static void main(String[] args) {
int num = 10;

if (num % 2 == 0) {
[Link]("hi");
}
}
}

2).WAJP to check given number is positive or not?

Ans) import [Link];

class PositiveCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

if (num > 0) {
[Link]("Given number is Positive");
} else if (num < 0) {
[Link]("Given number is Negative");
} else {
[Link]("Given number is Zero");
}
[Link]();
}
}

1).WAJP find given number is positive or negative


I/p:- 4 O/p:-Positive number
I/p:- -21 O/p:-Negative number

Ans) class PositiveNegative {


public static void main(String[] args) {
int num = 4;

if (num > 0) {
[Link]("Positive number");
} else if (num < 0) {
[Link]("Negative number");
}
}
}

2).WAJP find largest among two number


I/p:-10,20 O/p: 20

Ans) class LargestOfTwo {


public static void main(String[] args) {
int a = 10;
int b = 20;

if (a > b) {
[Link](a);
} else {
[Link](b);
}
}
}

3).WAJP check given number is even or odd


I/p:-4 O/p:Even number
I/p:-9 O/p:-Oddnumber

Ans) class EvenOdd {


public static void main(String[] args) {
int num = 4;

if (num % 2 == 0) {
[Link]("Even number");
} else {
[Link]("Odd number");
}
}
}

4)WAJP check whether the given number is divisible by


5 or not
I/p:-25 O/p:-Given number is divisible by 5
I/p:-14 O/p:-Given number is not divisible by 5

Ans) class DivisibleByFive {


public static void main(String[] args) {
int num = 25;

if (num % 5 == 0) {
[Link]("Given number is divisible by 5");
} else {
[Link]("Given number is not divisible by
5");
}
}
}
5)Tom wants buy a Tshirt,tom tshirt size is small and
tshirt price should less than or equal to 1000

i/p char size='s'; int price =900;


o/p: eligible to buy

i/p: size=’l’ , price=800;


o/p: not eligible to buy

class TShirtEligibility {
public static void main(String[] args) {
char size = 's';
int price = 900;

if (size == 's' && price <= 1000) {


[Link]("Eligible to buy");
} else {
[Link]("Not eligible to buy");
}
}
}

1).WAJP check student is pass or fail based on student


grade ?
I/p:-'B' O/p:-Second Class

Ans) class StudentGrade {


public static void main(String[] args) {
char grade = 'B';

if (grade == 'A') {
[Link]("First Class");
} else if (grade == 'B') {
[Link]("Second Class");
} else if (grade == 'C') {
[Link]("Pass");
} else {
[Link]("Fail");
}
}
}
2).WAJP Take input from user as character ,check that
character
belong to uppercase or lower case or number or special
character
I/p:-'A' O/p:-Uppercase
I/p:-'$' O/p:-Special character

Ans) import [Link];

class CharacterCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a character: ");


char ch = [Link]().charAt(0);

if (ch >= 'A' && ch <= 'Z') {


[Link]("Uppercase");
} else if (ch >= 'a' && ch <= 'z') {
[Link]("Lowercase");
} else if (ch >= '0' && ch <= '9') {
[Link]("Number");
} else {
[Link]("Special character");
}
}
}

3).WAJP take input from user as integer based on that


number print particular month
I/p:-6 O/p:-June

Ans) import [Link];

class MonthName {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter month number: ");


int month = [Link]();

switch (month) {
case 1: [Link]("January"); break;
case 2: [Link]("February"); break;
case 3: [Link]("March"); break;
case 4: [Link]("April"); break;
case 5: [Link]("May"); break;
case 6: [Link]("June"); break;
case 7: [Link]("July"); break;
case 8: [Link]("August"); break;
case 9: [Link]("September"); break;
case 10: [Link]("October"); break;
case 11: [Link]("November"); break;
case 12: [Link]("December"); break;
default: [Link]("Invalid month number");
}
}
}

4).Largest among Three number


Int a=10,b=20,c=5;
o/p : b is largest
Ans) class LargestOfThree {
public static void main(String[] args) {
int a = 10, b = 20, c = 5;

if (a > b && a > c) {


[Link]("a is largest");
} else if (b > a && b > c) {
[Link]("b is largest");
} else {
[Link]("c is largest");
}
}
}

i).WAJP take input from user as integer formate


print that integer in String formate
I/p:- 5 O/p:-five

Ans) import [Link];

class NumberToWord {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number (0-9): ");


int num = [Link]();

switch (num) {
case 0: [Link]("zero"); break;
case 1: [Link]("one"); break;
case 2: [Link]("two"); break;
case 3: [Link]("three"); break;
case 4: [Link]("four"); break;
case 5: [Link]("five"); break;
case 6: [Link]("six"); break;
case 7: [Link]("seven"); break;
case 8: [Link]("eight"); break;
case 9: [Link]("nine"); break;
default: [Link]("Invalid input");
}
}
}

ii)Calculator Program

1)Addition
2)Substrcation
3)Multiplication
4)Division
.............................
enter choice
3
Enter 2 values
5
5
Multiplication : 25

Ans) import [Link];

class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("1) Addition");
[Link]("2) Subtraction");
[Link]("3) Multiplication");
[Link]("4) Division");
[Link](".............................");
[Link]("Enter choice: ");
int choice = [Link]();

[Link]("Enter first value: ");


int a = [Link]();

[Link]("Enter second value: ");


int b = [Link]();

switch (choice) {
case 1:
[Link]("Addition = " + (a + b));
break;

case 2:
[Link]("Subtraction = " + (a - b));
break;

case 3:
[Link]("Multiplication = " + (a * b));
break;

case 4:
if (b != 0) {
[Link]("Division = " + (a / b));
} else {
[Link]("Division by zero not
allowed");
}
break;

default:
[Link]("Invalid choice");
}
}
}

ATM Mini Project


1)A/c Details
2)Deposit
3)Withdraw
4)Check balance
5)Exit

Ans) import [Link];

class ATM {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

String name = "Suresh";


long accNo = 1234567890L;
int balance = 5000;
int choice;

do {
[Link]("\n===== ATM MENU =====");
[Link]("1) A/c Details");
[Link]("2) Deposit");
[Link]("3) Withdraw");
[Link]("4) Check Balance");
[Link]("5) Exit");
[Link]("Enter your choice: ");
choice = [Link]();

switch (choice) {

case 1:
[Link]("Account Holder Name: " +
name);
[Link]("Account Number: " +
accNo);
[Link]("Available Balance: " +
balance);
break;

case 2:
[Link]("Enter deposit amount: ");
int deposit = [Link]();
balance = balance + deposit;
[Link]("Amount deposited
successfully");
[Link]("Updated Balance: " +
balance);
break;

case 3:
[Link]("Enter withdraw amount: ");
int withdraw = [Link]();
if (withdraw <= balance) {
balance = balance - withdraw;
[Link]("Withdraw successful");
[Link]("Remaining Balance: " +
balance);
} else {
[Link]("Insufficient Balance");
}
break;

case 4:
[Link]("Current Balance: " +
balance);
break;

case 5:
[Link]("Thank you for using
ATM");
break;

default:
[Link]("Invalid choice");
}
} while (choice != 5);
}
}

1).Check Person is eligible for donate blood or not


(Weight should be greater than 50kg and bgroup should
be O+
I/p:-weight 50 , bgroup O+
o/p:-not eligible to donate

Ans) class BloodDonation {


public static void main(String[] args) {

int weight = 50;


String bgroup = "O+";

if (weight > 50 && [Link]("O+")) {


[Link]("Eligible to donate");
} else {
[Link]("Not eligible to donate");
}
}
}

2).Take input from user as integer, check that number is


even or odd
if it is even check that number is divisible by 6 or not.
if it is odd check that number is divisible by 3 or not

I/p:-12 O/p:-Given number is even and it is divisible by


6
I/p:-4 O/p:-Given number is even but it is not divisible
by 6
I/p:-9 O/p:-Given number is odd and it is divisible by 3
I/p:-13 O/p:-Given number is odd but it is not
divisible by 3

Ans) import [Link];

class EvenOddDivisibleCheck {
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int num = [Link]();

if (num % 2 == 0) {
// Even number
if (num % 6 == 0) {
[Link]("Given number is even and it is
divisible by 6");
} else {
[Link]("Given number is even but it is
not divisible by 6");
}
} else {
// Odd number
if (num % 3 == 0) {
[Link]("Given number is odd and it is
divisible by 3");
} else {
[Link]("Given number is odd but it is
not divisible by 3");
}
}
}
}

Sample Inputs & Outputs:


 Input: 12 → Given number is even and it is divisible
by 6
 Input: 4 → Given number is even but it is not divisible
by 6
 Input: 9 → Given number is odd and it is divisible by
3
 Input: 13 → Given number is odd but it is not
divisible by 3

Looping
1).WAJP To Print Even Number Between The Range
Ans) class EvenNumbersRange {
public static void main(String[] args) {
int start = 1;
int end = 20;
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
[Link](i + " ");
}
}
}
}
2).WAJP To Find Sum Of Even Number?
Ans) class SumOfEven {
public static void main(String[] args) {

int sum = 0;
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0) {
sum += i;
}
}
[Link]("Sum of Even Numbers = " + sum);
}
}
3).WAJP To Print Odd Number Between The Range
Ans) class OddNumbersRange {
public static void main(String[] args) {
int start = 1;
int end = 20;
for (int i = start; i <= end; i++) {
if (i % 2 != 0) {
[Link](i + " ");
}
}
}
}
4).WAJP To Find Sum Of Odd Number?
Ans) class SumOfOdd {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 20; i++) {
if (i % 2 != 0) {
sum += i;
}
}
[Link]("Sum of Odd Numbers = " + sum);
}
}

You might also like