Lab #2 Greater number between two given numbers

Original price was: \$35.00.Current price is: \$30.00.

Categories: ,
5/5 - (1 vote)

Lab #2
1) Objective: To find the greater number between two given numbers.
Problem Statement:- Write a Java program to print the greater number from two given numbers
using if-else statements.
a=100
b=50
Code:-
package pro2;
public class lab21
{
public static void main(String[] args)
{
int a=100,b=50;
if(a < b){
System.out.println(“b is greater than a”);
}
else

System.out.println(“a is greater than b”);
}
}
Screenshot:-
Explanation:-
This type of decision taking is done using if statement in Java.
Let’s have a look at the syntax of if statement before looking at its example.
if(expression)
{
statements
}
If the expression written within the brackets of if statement is true, then the statements written in the body
of the if (enclosed by curly brackets) are executed.
if(expression)
statement 1
else
statement 2
If the expression is true, statement 1 will be executed. Otherwise, statement 2 will be executed.
2) Objective: To find that a given number is even or odd.
Problem Statement:- Write a Java program to find any given number is even or odd.
Code:-
package pro2;
public class lab22 {
public static void main(String[] args){
int n=9;
if( n%2 == 0 )
System.out.println(“Number is even”);
else
System.out.println(“Number is odd”);
}
}
Screenshot:-
Explanation:-
For a number to be even, it must be divisible by 2. This means that it should give a remainder 0 if divided
by 2.
We have entered 9 here and n%2 i.e. 9%2 is 1. So, else will be executed and Number is odd will be
printed on the screen.
Lab Practice
3) Objective: To find the number which is the greatest among three given numbers.
Problem Statement:- Write a Java program to find a number which is the greatest among three
given numbers using if-else statements.
Test Data:
A=5
B=2
C=8
4) Objective: To test a given number is positive or negative.
Problem Statement:- Write a Java program to test a given number is positive or negative.
Test Data:
Input number(a)=35
5) Objective: Use of if-else statements.
Problem Statement:- Write a Java program that reads a floating-point number and prints “zero” if
the number is zero. Otherwise, print “positive” or “negative”. Add “small” if the absolute value of the
number is less than 1, or “large” if it exceeds 1,000,000.
6) Objective: To solve the quadratic equation (Use of if-else statements).
Problem Statement:- Write a Java program to solve(find roots) quadratic equation (use if, else if
and else).
Test Data
Input a: 1
Input b: 5
Input c: 1
Equation: a*(x^2)+b*x +c
7) Objective: Use of if-else statements.
Problem Statement:- Write a Java program that will read three types of given scores(quiz, midterm, and final scores) and determine the grade based on the following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
Take the sample data below: (Test data)
Quiz score: 80
Mid-term score: 68
Final score: 90
8) Objective: Choose a day from the week.
Problem Statement:- Write a Java program to print a week’s day name using switch case
statement.
Code:-
package pro2;
public class class22 {
public static void main(String[] args) {
int week = 3;
switch (week){
case 1:
System.out.println(“monday”);
break;
case 2:
System.out.println(“tuesday”);
break;
case 3:
System.out.println(“wednesday”);
break;
case 4:
System.out.println(“thursday”);
break;
case 5:
System.out.println(“friday”);
break;
case 6:
System.out.println(“saturday”);
break;
case 7:
System.out.println(“sunday”);
break;
default:
System.out.println(“Invalid week”);
break;
}
// TODO Auto-generated method stub
}
}
Screenshot:-
9) Objective: Use of switch case statement.
Problem Statement:- Write a Java program that should declare an int named day whose value
represents a day(1-7). The code should display the name of the day, based on the value of day, using the
switch statement.
Test data:-
day=2
Sample ouput for above test data:-
Tuesday
10) Objective: Use of switch case statement.
Problem Statement:- Write a Java program that should declare an int named day whose value
represents a day(1-7).The code should display the name of the day and the condition: – like Weekday (if
number entered is between 1-5), and Weekend (if number entered is between 6 or 7) using the switch
statement.
Test data:-
day=2
Sample ouput for above test data:-
Tuesday is a Weekday
11) Objective: Use of switch case statement.
Problem Statement:- Write a Java program that should declare an string branch named CSE,ECE
and ME and int year=2.
Sample ouput for above test data:-
If year =2 and branch= CSE then output should be
elective courses : Machine Learning, Big Data
If year =2 and branch= ECE then output should be
elective courses : Advance math’s, English
12) Objective: Use of switch case statement.
Problem Statement:- Write a Java program program named switchdemo1 which declares
an int named month whose value represents a month out of the year. The program should display the
name of the month, based on the value of month, using the switch statement.
Test data:-
month=3
Sample ouput for above test data:-
March
13) Objective: Use of switch case statement.
Problem Statement:- Write a Java program program named switchdemo2 which declares
Test data:-
int month = 2;
int year = 2016;
int numDays = 0;
an int named month, numDays(number of days) and year. The program should display number of days
in the given month of the following year using the switch statement.
Sample ouput for above test data:-
Number of Days = 29

Reviews

There are no reviews yet.

Scroll to Top