0% found this document useful (0 votes)
34 views9 pages

OOP Lab: Loops & Logic Tasks

The document discusses a lab assignment on loops and logic in object oriented programming. It provides 5 programming tasks - to solve a quadratic equation, generate the Fibonacci sequence, determine employee designation based on salary range, determine if a number is positive or negative, and find the greatest and least of three numbers. For each task, it lists the expected input/output and provides the required Java code.

Uploaded by

amnamuzammil131
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)
34 views9 pages

OOP Lab: Loops & Logic Tasks

The document discusses a lab assignment on loops and logic in object oriented programming. It provides 5 programming tasks - to solve a quadratic equation, generate the Fibonacci sequence, determine employee designation based on salary range, determine if a number is positive or negative, and find the greatest and least of three numbers. For each task, it lists the expected input/output and provides the required Java code.

Uploaded by

amnamuzammil131
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

Object Oriented Programming SSUET/QR/114

(SWE-103L)

LAB # 2
LOOPS AND LOGIC

OBJECTIVE:

To Study Control and Iteration Statements and Math Classes.

LAB TASK

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
CODE:
package quadraticequationsolver ;
import java.util.Scanner;

public class QuadraticEquationSolver {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input a: ");
double a = input.nextDouble();
System.out.print("Input b: ");

2023F-BSE-054 LAB 2 (spring)


Object Oriented Programming SSUET/QR/114
(SWE-103L)

double b = input.nextDouble();
System.out.print("Input c: ");
double c = input.nextDouble();

double discriminant = b * b - 4 * a * c;
double root1, root2;

if (discriminant > 0) {
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("The roots are " + root1 + " and " + root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
System.out.println("The root is " + root1);
} else {
System.out.println("The equation has no real roots.");
}
}
}

OUTPUT:

2023F-BSE-054 LAB 2 (spring)


Object Oriented Programming SSUET/QR/114
(SWE-103L)

2. Write a program to generate Fibonacci hypothesis for 19 generations


given below.

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181


6765

CODE:
package fibonacci;

public class Fibonacci {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int n = 19; // Number of generations
int[] fibonacci = new int[n];

fibonacci[0] = 0;
fibonacci[1] = 1;

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


fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}

// Print the Fibonacci sequence


for (int i = 0; i < n; i++) {
System.out.print(fibonacci[i] + " ");
}
}
}

2023F-BSE-054 LAB 2 (spring)


Object Oriented Programming SSUET/QR/114
(SWE-103L)

OUTPUT:

3. Write a program to read salary of an employee and designate


according to their salary using if-else ladder.

Note: 25k-35k Research Assistant


36k-50k Junior Lecturer
51k-65k Lecturer
66k-80k Assistant Professor
CODE:
package salarydesignation;

import java.util.Scanner;
public class SalaryDesignation {
public static void main(String[] args) {
2023F-BSE-054 LAB 2 (spring)
Object Oriented Programming SSUET/QR/114
(SWE-103L)

Scanner input = new Scanner(System.in);


System.out.print("Enter the salary: ");
double salary = input.nextDouble();

if (salary >= 25000 && salary <= 35000) {


System.out.println("Research Assistant");
} else if (salary >= 36000 && salary <= 50000) {
System.out.println("Junior Lecturer");
} else if (salary >= 51000 && salary <= 65000) {
System.out.println("Lecturer");
} else if (salary >= 66000 && salary <= 80000) {
System.out.println("Assistant Professor");
} else {
System.out.println("Salary not in specified range");
}
}
}

2023F-BSE-054 LAB 2 (spring)


Object Oriented Programming SSUET/QR/114
(SWE-103L)

OUTPUT:

4. Write a Java program to read a number from the user and


print whether it is positive or negative.

Example:
Input number: 35
Expected Output:
Number is positive
CODE:
package positivenegative;
import java.util.Scanner;

public class PositiveNegative {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input number: ");
int number = input.nextInt();

if (number > 0) {
System.out.println("Number is positive");
} else if (number < 0) {
System.out.println("Number is negative");
} else {
System.out.println("Number is zero");
}
}
}

2023F-BSE-054 LAB 2 (spring)


Object Oriented Programming SSUET/QR/114
(SWE-103L)

OUTPUT:

5. Write a program to read 3 integer values and print the greater


and lesser number respectively.

Examp:
A=29
B=25
C=72
Output:
Greater number=72
Lesser number=29

CODE:
2023F-BSE-054 LAB 2 (spring)
Object Oriented Programming SSUET/QR/114
(SWE-103L)

import java.util.Scanner;

public class GreaterAndLesser {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three integer values separated by spaces: ");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();

int greater = Math.max(Math.max(a, b), c);


int lesser = Math.min(Math.min(a, b), c);

System.out.println("Greater number=" + greater + " Lesser number=" +


lesser);
}
}

OUTPUT:

2023F-BSE-054 LAB 2 (spring)


Object Oriented Programming SSUET/QR/114
(SWE-103L)

2023F-BSE-054 LAB 2 (spring)

You might also like