0% found this document useful (0 votes)
2 views

IT Java Programme

The document contains various Java programming examples demonstrating the use of conditional statements such as if-else and switch. It includes programs for checking odd/even numbers, finding the maximum of three numbers, determining if a number is positive, negative, or zero, calculating discounts on pen costs, and computing gross salary based on basic salary. Additionally, it features programs to check if a rectangle is a square and to compare two numbers for equality.

Uploaded by

SamarendraRout
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IT Java Programme

The document contains various Java programming examples demonstrating the use of conditional statements such as if-else and switch. It includes programs for checking odd/even numbers, finding the maximum of three numbers, determining if a number is positive, negative, or zero, calculating discounts on pen costs, and computing gross salary based on basic salary. Additionally, it features programs to check if a rectangle is a square and to compare two numbers for equality.

Uploaded by

SamarendraRout
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

https://www.csdt.co.in/IT_Jobs/ProgrammingPractice.aspx?

Uid=Java%20if%20else,%20if
%20else%20ladder%20and%20switch%20Questions
1.
2. //A Java Program to demonstrate the use of if-else statement.
3. //It is a program of odd and even number.
4. public class IfElseExample {
5. public static void main(String[] args) {
6. //defining a variable
7. int number=13;
8. //Check if the number is divisible by 2 or not
9. if(number%2==0){
10. System.out.println("even number");
11. }else{
12. System.out.println("odd number");
13. }
14. }
15. }

Check whether the number is divisible by 5.


class example
{
public static void main(String args[])
{
int x=30;
int y=50;

if(x==30){
if(y==40){
System.out.println("Correct Answer");
}
else{
System.out.println("Wrong Answer");
}
}
}
}

import java.util.*;

class Geeks {
public static void main(String args[])
{
int i = 20;

if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}

import java.util.Scanner;
class Three_Maximum
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Number 1 : ");
int num1 = input.nextInt();
System.out.print("Enter the Number 2 : ");
int num2 = input.nextInt();
System.out.print("Enter the Number 3 : ");
int num3 = input.nextInt();
if(num1>num2 && num1>num3)
System.out.println("Maximum Number is " +num1);
else if(num1<num2 && num3<num2)
System.out.println("Maximum Number is " +num2);
else
System.out.println("Maximum Number is " +num3);
}
}

Output
Enter the Number 1 : 23
Enter the Number 2 : 78
Enter the Number 3 : 90
Maximum Number is 90

Write a program to check whether a number is


negative, positive or zero
This Java program prompts the user to input a number and then determines
whether the number is positive, negative, or zero using conditional
statements.

The Scanner class is used to read input from the user, and the program
prompts the user to enter a number and stores it in the variable num.

The program then uses an if-else statement to determine the sign of the
number. If num is greater than 0, it prints the message "Positive Number".
If num is less than 0, it prints the message "Negative Number". If num is
equal to 0, it prints the message "Zero".
Source Code
import java.util.Scanner;
class Negative_Positive
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Number : ");
int num = input.nextInt();
if(num>0)
System.out.println("Positive Number");
else if(num<0)
System.out.println("Negative Number");
else
System.out.println("Zero");
}
}

Output
Enter the Number : 45
Positive Number

Enter the Number : -4


Negative Number
uestion- Write a java program to input a cost of a pen and
calculate 70 pens cost with some discount. if total pen cost
greater than 1000 then we calculate 20% discount otherwise 10%.

Answer-
package csdtpatna;
import java.util.Scanner;
class PenCost
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from
User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter Cost of a Pen");
int one_pen_cost=sc.nextInt();
int TotalCost=70*one_pen_cost;
int dis;
if(TotalCost>=1000)
{
dis=TotalCost*20/100;
System.out.println("Dear customer you Pay only Rs. "+
(TotalCost-dis));
}
else
{
dis=TotalCost*10/100;
System.out.println("Dear customer you Pay only Rs. "+
(TotalCost-dis));
}
System.out.println("One Pen Cost is:: Rs.
"+one_pen_cost);
System.out.println("Total Pen Cost without discount is::
Rs. "+TotalCost);
System.out.println("Total discount Amount is:: Rs.
"+dis);
}

}
Question- Write a java program to take Basic salary of employee
from user and calculate gross salary with given condition, if Basic
salary >= 10000 then we calculate 40% da and 30% ta of Basic
salary otherwise 30% da and 20% ta of Basic salary.

Answer-
package csdtpatna;
import java.util.Scanner;
class EmployeeSalary
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from
User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter Basic Salary");
int bs=sc.nextInt();
int da,ta,gs;
if(bs>=10000)
{
da=bs*40/100;
ta=bs*30/100;

}
else
{
da=bs*30/100;
ta=bs*20/100;
}
System.out.println("Basic Salary is:: Rs. "+bs);
System.out.println("DA is:: Rs. "+da+" and TA is Rs
"+ta);
System.out.println("Gross Salary is:: Rs. "+(bs+da+ta));
}

}
Write a java program to Take values of length and breadth of a
rectangle from user and check if it is square or not.

Answer-
import java.util.Scanner;
class Rectangle
{
public static void main(String[] args)
{ //Creating object of Scanner Class
Scanner s = new Scanner(System.in);
System.out.println("Enter length");
int x = s.nextInt();
System.out.println("Enter breadth");
int y = s.nextInt();
if(x==y)
{
System.out.println("Square");
}
else
{
System.out.println("Rectangle");
}
}
}
Write a program to check Two input numbers are equels or not.

Answer-
import java.io.*;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
double a,b;
System.out.println("Enter 1st number");
a=sc.nextDouble();
System.out.println("Enter 2nd number");
b=sc.nextDouble();
if(a==b)
{
System.out.println("They are the same");
}
else
{
System.out.println("They are different");
}
}
}

You might also like