0% found this document useful (0 votes)
5 views30 pages

Java Conditional Statements

The document contains a series of Java programs that demonstrate basic programming concepts such as input handling, conditional statements, and arithmetic operations. Each program addresses a specific problem, including checking equality of integers, determining even or odd numbers, finding the largest of three numbers, and identifying leap years. Additional programs cover character classification, menu-driven calculations, and eligibility checks based on user input.

Uploaded by

Poorab patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

Java Conditional Statements

The document contains a series of Java programs that demonstrate basic programming concepts such as input handling, conditional statements, and arithmetic operations. Each program addresses a specific problem, including checking equality of integers, determining even or odd numbers, finding the largest of three numbers, and identifying leap years. Additional programs cover character classification, menu-driven calculations, and eligibility checks based on user input.

Uploaded by

Poorab patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

1.

Write a program to accept two integers and check whether they are equal or not

import java.io.*;

class Equal {

public static void main(String[] args) throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int a,b;

System.out.println("enter first number");

a = Integer.parseInt(br.readLine() );

System.out.println("enter second number");

b=Integer.parseInt(br.readLine() );

if(a==b)

System.out.println("they are equal");

else

System.out.println("they are not equal");

}
2 Write a program to check whether a given number is even or odd

import java.io.*;

class Check {

public static void main(String[] args) throws IOException

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

int num;

System.out.println("value for even or odd ");

num= Integer.parseInt(br.readLine() );

if (num % 2 == 0) {

System.out.println("Entered Number is Even");

else {

System.out.println("Entered Number is Odd");

}
3. W.A.P to enter two number find number is greater or smaller.

import java.io.*;

public class Largest {

public static void main(String[] args) throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int number1, number2;

System.out.print(" Please Enter the First Number : ");

number1 = Integer.parseInt(br.readLine() );

System.out.print(" Please Enter the Second Number : ");

number2 =Integer.parseInt(br.readLine() );

if(number1 > number2)

System.out.println("\n The Largest Number = " + number1);

else if (number2 > number1)

System.out.println("\n The Largest Number = " + number2);

else

System.out.println("\n Both are Equal");

}
4. Write a program to find whether a given year is a leap year or not.

import java.io.*;

public class leapyear

public static void main(String[] args) throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

long year;

System.out.print("enter any calendar year :");

year= Long.parseLong(br.readLine());

if(year!=0)

if(year%400==0)

System.out.println(year+" is a leap year");

else

if(year%100==0)

System.out.println(year+" is not a leap year");

else

if(year%4==0)

System.out.println(year+" is a leap year");


else

System.out.println(year+" is not a leap year");

else

System.out.println("Year zero does not exist ");

5 Write a program to find the largest of three numbers

import java.io.*;

public class three

public static void main(String[] args) throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int a, b, c;

System.out.print(" Please Enter the First Number : ");

a = Integer.parseInt(br.readLine() );
System.out.print(" Please Enter the second Number : ");

b= Integer.parseInt(br.readLine() );

System.out.print(" Please Enter the third Number : ");

c = Integer.parseInt(br.readLine() );

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

System.out.println(a + " is the largest number.");

else

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

System.out.println(b + " is the largest number.");

else

System.out.println(c + " is the largest number.");

}
6 W.A.P enter a number to check number is positive, negative or Zero.

import java.io.*;

public class six


{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int num;

System.out.print(" Please Enter the Number : ");


num = Integer.parseInt(br.readLine() );

if(num>0)
{
System.out.println("The number is positive.");
}

else if(num<0)
{
System.out.println("The number is negative.");
}

else
{
System.out.println("The number is zero.");
}

}
}
7. W.A.P to enter a character find Vowel or Consonant using nester if statement

import java.io.*;

public class seven


{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Please Enter the alphabets in lowercase : ");


char x = (char)br.read();

if (x == 'a')
{
System.out.println("Vowel");
}

else if (x == 'e')
{
System.out.println("Vowel");
}

else if (x == 'i')
{
System.out.println("Vowel");
}
else if (x == 'o')
{
System.out.println("Vowel");
}
else if (x == 'u')
{
System.out.println("Vowel");
}

else{
System.out.println("it is Consonant");
}

}
}

8 W.A.P to enter a character find Vowel or Consonant using OR Operator

import java.io.*;

public class eight


{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Please Enter the alphabets : ");


char x = (char)br.read();

if (x == 'a' || x == 'e' || x == 'i' ||


x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
{
System.out.println("Vowel");
}
else
System.out.println("Consonant");

}
}
9. Write a program to read any Month Number in integer and display Month
name in the word.

import java.io.*;

public class nine


{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int num;

System.out.print(" Please Enter month Number : ");


num = Integer.parseInt(br.readLine() );

switch (num)
{
case 1:
System.out.println ("The name of month number 1 is January");
break;
case 2:
System.out.println ("The name of month number 2 is February");
break;
case 3:
System.out.println ("The name of month number 3 is March");
break;
case 4:
System.out.println ("The name of month number 4 is April");
break;
case 5:
System.out.println ("The name of month number 5 is May");
break;
case 6:
System.out.println ("The name of month number 6 is June");
break;
case 7:
System.out.println ("The name of month number 7 is July");
break;
case 8:
System.out.println ("The name of month number 8 is August");
break;
case 9:
System.out.println ("The name of month number 9 is September");
break;
case 10:
System.out.println ("The name of month number 10 is October");
break;
case 11:
System.out.println ("The name of month number 11 is November");
break;
case 12:
System.out.println ("The name of month number 12 is December");
break;
default:
System.out.println ("You have entered an invalid number");
}

10. W.A.P to enter a character find Vowel or Consonant using switch statement

import java.io.*;

public class ten


{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Please Enter the alphabets : ");


char x = (char)br.read();

switch (x)
{
case 'a': System.out.println ("it is vowel "); break;
case 'e': System.out.println ("it is vowel "); break;
case 'i': System.out.println ("it is vowel "); break;
case 'o': System.out.println ("it is vowel "); break;
case 'u': System.out.println ("it is vowel "); break;
case 'A': System.out.println ("it is vowel "); break;
case 'E': System.out.println ("it is vowel "); break;
case 'I': System.out.println ("it is vowel "); break;
case 'O': System.out.println ("it is vowel "); break;
case 'U': System.out.println ("it is vowel "); break;

default:
System.out.println ("it is Consonant");
}

}
}

11. Write a program which is a Menu-Driven Program to perform a simple


calculation

import java.io.*;

public class eleven


{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int a,b,c;
int choice;

while(true)
{

System.out.println("Press 1 for Addition");


System.out.println("Press 2 for Subtraction");
System.out.println("Press 3 for Multiplication");
System.out.println("Press 4 for Division");
System.out.println("Press 5 to Quit\n \n ");

System.out.println("Make your choice");


choice = Integer.parseInt(br.readLine() );

switch (choice) {

case 1:
System.out.println("Enter the first number ");
a = Integer.parseInt(br.readLine() );
System.out.println("Enter the second number");
b = Integer.parseInt(br.readLine() );
c = a + b;
System.out.println("The sum of the numbers is = " + c +"\n");
break;

case 2:
System.out.println("Enter the first number ");
a = Integer.parseInt(br.readLine() );
System.out.println("Enter the second number");
b = Integer.parseInt(br.readLine() );
c = a - b;
System.out.println("The difference of the numbers is = " + c +"\n");
break;

case 3:
System.out.println("Enter the first number");
a = Integer.parseInt(br.readLine() );
System.out.println("Enter the second number");
b = Integer.parseInt(br.readLine() );
c = a * b;
System.out.println("The product of the numbers is = " + c + "\n");
break;

case 4:
System.out.println("Enter the first number");
a = Integer.parseInt(br.readLine() );
System.out.println("Enter the second number");
b = Integer.parseInt(br.readLine() );
c = a / b;
System.out.println("The quotient is = " + c +"\n");
break;

case 5:
System.exit(0);

default:
System.out.println("Invalid choice!!! Please make a valid choice. \\n\\n");
}
}

}
}

12. W.A.P to display odd number prime pairs.

import java.io.*;

class PrimePair{

public static boolean isPrime(int x)

if (x == 0 || x == 1)

return false;

for (int i = 2; i * i <= x; ++i)

if (x % i == 0)

return false;

return true;

public static void findPrimes(int n)

if (isPrime(n))

System.out.print( n );

else if (isPrime(n - 2))

System.out.print( 2 + " " +

(n - 2) );

else

{
System.out.print( 3 + " ");

n = n - 3;

for (int i = 0; i < n; i++) {

if (isPrime(i) && isPrime(n - i)) {

System.out.print( i + " " +

(n - i));

break;

public static void main(String[] args)

int n = 27;

findPrimes(n);

13. Write a program to check whether a character is an alphabet, digit or special character.

import java.io.*;

public class AlphabetDigitSpecial {

public static void main(String[] args) throws IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Please Enter the alphabets : ");

char x = (char)br.read();

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
System.out.println(ch + " is A ALPHABET.");

} else if (ch >= '0' && ch <= '9') {

System.out.println(ch + " is A DIGIT.");

} else {

System.out.println(ch + " is A SPECIAL CHARACTER.");

14. Write a program to read the age of a candidate and determine whether it is eligible for casting
his/her own vote.

import java.io.*;

public class VotingAge

public static void main(String[] args)throws IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Please Enter the age : ");

Int age = Integer.parseInt(br.read());

if (age >= 18)

System.out.println("You are eligible for vote.");

else

{
System.out.println("You are not eligible for vote.");

15. Write a program to accept the height of a person in centimetre and categorize the person
according to their height.

import java.io.*;

public class HeightCategory

public static void main(String[] args)throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter height in cm");

m=Integer.parseInt(br.readLine());

if(m > 175)

System.out.println("The person is tall.");

else if(m > 155 && m <= 175)

System.out.println("The person has average height.");

else

System.out.println("The person is dwarf.");

}
}

16. Write a program to find the eligibility of admission for a professional course based on the
following criteria:

Eligibility Criteria :

Marks in Maths >=65 and Marks in Phy >=55 and Marks in Chem>=50 and Total in all three subject
>=190 or Total in Maths and Physics >=140

import java.io.*;

public class Eligibility

public static void main(String[] args)throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter Marks in Maths ");

int m=Integer.parseInt(br.readLine());

System.out.println("Enter Marks in Physics ");

int p=Integer.parseInt(br.readLine());

System.out.println("Enter Marks in Chemistry ");

int c=Integer.parseInt(br.readLine());

if((m >=65 && p>=55 && c>=50) || (m+p+c >190 || m+p>=140))

System.out.println("The candidate is eligible for admission.”);


}

else

System.out.println("The candidate is not eligible for admission.");

17. Write a program to calculate the root of a Quadratic Equation.

import java.io.*;

public class RootsOfQuadraticEquation {

public static void main(String[] args)throws IOException

double secondRoot = 0, firstRoot = 0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter value of a ");

double a=Double.parseDouble(br.readLine());

System.out.println("Enter value of b ");

double b=Double.parseDouble(br.readLine());

System.out.println("Enter value of c ");


double c=Double.parseDouble(br.readLine());

double determinant = (b*b)-(4*a*c);

double sqrt = Math.sqrt(determinant);

if(determinant>0){

firstRoot = (-b + sqrt)/(2*a);

secondRoot = (-b - sqrt)/(2*a);

System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot);

}else if(determinant == 0){

System.out.println("Root is :: "+(-b + sqrt)/(2*a));

18. Write a program to read roll no, name and marks of three subjects and calculate the total,
percentage and division.

import java.io.*;

public class StudentMarks {

public static void main(String[] args) throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.print("Enter the roll number: ");

int roll=Integer.parseInt(br.readLine());

System.out.print("Enter the name: ");

String name=br.readLine();

System.out.print("Enter the marks in Physics: ");

int marks1 =Integer.parseInt(br.readLine());

System.out.print("Enter the marks in Chemistry: ");

int marks2 =Integer.parseInt(br.readLine());

System.out.print("Enter the marks in Computer Application: ");

int marks3=Integer.parseInt(br.readLine());

// Calculate the total, percentage, and division

int total = marks1 + marks2 + marks3;

double percentage = total / 3.0;

String division = "";

if (percentage >= 60) {

division = "First";

} else if (percentage >= 50) {

division = "Second";

} else if (percentage >= 40) {

division = "Third";

} else {

division = "Fail";

// Print the results

System.out.println("Name: " + name);

System.out.println("Roll no: " + roll);

System.out.println("Marks in Physics: " + marks1);

System.out.println("Marks in Chemistry: " + marks2);

System.out.println("Marks in Computer Application: " + marks3);

System.out.println("Total: " + total);

System.out.println("Percentage: " + percentage + "%");


System.out.println("Division: " + division);

19. Write a program to read temperature in centigrade and display a suitable message according
to

temperature state below:

import java.io.*;

class Temperature

public static void main(String args[])

throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the temperature: ");

int temp=Integer.parseInt(br.readLine());

String s="";

if(temp<0)

s="Freezing ";

else if(temp>=0&&temp<=10)

s="Very Cold ";

else if(temp>=11&&temp<=20)

s="Cold ";
else if(temp>=21&&temp<=30)

s="Normal ";

else if(temp>=31&&temp<=40)

s="Hot ";

else if(temp>40)

s="Very hot ";

System.out.println(s+ "weather.");

20. Write a program to check whether a triangle is Equilateral, Isosceles or Scalene.

class Triangle{

static void checkTriangle(int x, int y, int z)

if (x == y && y == z )

System.out.println("Equilateral Triangle");

else if (x == y || y == z || z == x )

System.out.println("Isosceles Triangle");

else

System.out.println("Scalene Triangle");

public static void main(String[] args)

int x = 8, y = 7, z = 9;

checkTriangle(x, y, z);

}
21. Write a program to check whether a triangle can be formed by the given value for the angles.

class TriangleAngle {

public static bool Valid(int a, int b, int c)

if (a + b + c == 180 && a != 0 && b != 0 && c != 0)

return true;

else

return false;

public static void main(String args[])

int a = 40, b = 55, c = 65;

if ((Valid(a, b, c)))

System.out.print("Triangle can be Formed");

else

System.out.print("Triangle Cannot be Formed");

22. Write a program to calculate and print the Electricity bill of a given customer. The customer id.,
name and unit consumed by the user should be taken from the keyboard and display the total
amount to pay to the customer.

import java.io.*;

class ElectricityBill

public static void main(String args[])throws IOException{


BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the ID: ");

int id =Integer.parseInt(br.readLine());

System.out.print("Enter Name: ");

String name = br.readLine();

System.out.print("Enter Units: ");

int units = Integer.parseInt(br.readLine());

double billToPay = 0;

if(units >= 0 && units <= 199)

billToPay = units * 1.20;

else if(units >= 200 && units < 400){

billToPay = units * 1.50;

else if(units >= 400 && units < 600)

billToPay = units * 1.80;

else if(units >= 600)

billToPay = units * 2.0;

if(billToPay > 400){

billToPay += ((billToPay/100) * 15);

if(billToPay < 100){

billToPay = 100;

System.out.println("ID : " + id);


System.out.println("Name: " + name);

System.out.println("The electricity bill for units: " +units+ " is : " + billToPay);

23. Write a program to accept a grade and declare the equivalent description

import java.io.*;

public class Grade

public static void main(String[] args) throws IOException

char a;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter grade of the student:");

a = (char) br.read();

if(a == 'A' || a == 'a')

System.out.println("Average");

else if(a == 'E' || a == 'e')

System.out.println("Excellent");

else if(a == 'V' || a == 'v')

System.out.println("Very Good");

}
else if(a == 'G' || a == 'g')

System.out.println(“Good");

else if(a == 'F' || a == 'f')

System.out.println("Fail");

24. Write a program to read any Month Number in integer and display the number of days for this

month.

import java.io.*;

class Monthdaysdisplay

public static void main(String[] args)

throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter month no.: ");

int month =Integer.parseInt(br.readLine());

if(month == 1)

System.out.println("JANUARY 31 days");

else if(month == 2)

{
System.out.println("FEBRUARY 28 or 29 days");

else if(month == 3)

System.out.println("MARCH 31 days");

else if(month == 4)

System.out.println("APRIL 30 days");

else if(month == 5)

System.out.println("MAY 31 days");

else if(month == 6)

System.out.println("JUNE 30 days");

else if(month == 7)

System.out.println("JULY 31 days");

else if(month == 8)

System.out.println("AUGUST 31 days");

else if(month == 9)

System.out.println("SEPTEMBER 30 days");

else if(month == 10)


{

System.out.println("OCTOBER 31 days");

else if(month == 11)

System.out.println("NOVEMBER 30 days");

else if(month == 12)

System.out.println("DECEMBER 31 days");

else

System.out.println("Invalid input! Please enter month number between (1-12).");

25. Write a program to calculate profit and loss on a transaction.

import java.io.*;

class profitandLoss

public static void main(String args[]) throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the Costprice ");

int cp =Integer.parseInt(br.readLine());

System.out.println("Enter the Selling price:");

int sp =Integer.parseInt(br.readLine());

if(cp-sp>0)

{
System.out.println("loss:" +(cp-sp));

else if(cp-sp<0)

System.out.println("profit:" +(sp-cp));

else

System.out.println("NEUTRAL");

You might also like