Object Oriented Programming: Objective 1
Object Oriented Programming: Objective 1
02
LAB # 2
OBJECTIVE
To Study Control and Iteration Statements and Math Classes.
Question:
1. Write a program to solve quadratic equation using( if, else-if and else) .
Example:
Input a: 1
Input b: 5
Input c: 1
Expected Output :
The roots are -0.20871215252208009 and -4.7912878474779195
Source Code:
import java.util.Scanner;
public class LoopsLogic {
public static void main(String[] args) {
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));
}
}
}
Output:
Question:
2. Write a program to generate Fibonacci hypothesis for 19 generations given below.
Source Code:
import java.util.Scanner;
Question:
3. Write a program to read salary of an employee and designate according to their
salary using if-else ladder.
Output:
Question:
4. Write a Java program to read a number from the user and print whether it is positive
or negative.
Example:
Input number: 35
Source Code:
import java.util.Scanner;
public class LoopsLogic {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number you want to check:");
int number = s.nextInt();
s.close();
if(number > 0)
{
System.out.println(number+" is positive number");
}
else if(number < 0)
{
Question:
5. Write a program to read 3 integer values and print the greater and lesser number respectively.
Example:
A=29
B=25
C=72
Output:
Greater number=72
Lesser number=29
Source Code:
import java.util.Scanner;
else
System.out.println(n3 + " is the greater number.");
else
System.out.println(n3 + " is the lesser number.");
}
}
Output: