Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

lab-Report-Java

The document contains a series of Java programs that demonstrate various programming concepts including checking if a number is odd or even, finding the largest of three numbers, solving quadratic equations, determining the day of the week based on user input, calculating grades based on marks, and more. Each program includes code snippets, expected outputs, and explanations of the logic used. The programs cover a range of topics suitable for beginners learning Java programming.

Uploaded by

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

lab-Report-Java

The document contains a series of Java programs that demonstrate various programming concepts including checking if a number is odd or even, finding the largest of three numbers, solving quadratic equations, determining the day of the week based on user input, calculating grades based on marks, and more. Each program includes code snippets, expected outputs, and explanations of the logic used. The programs cover a range of topics suitable for beginners learning Java programming.

Uploaded by

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

1) WAP that identifies whether the given number is odd or even.

Program:
class OddEven {
public static void main(String[] args) {
int num = 2;

if(num%2==0){
System.out.println("Given number is prime");
}else{
System.out.println("Given number is odd");
}
}
}

Output:
Given number is prime

2) WAP that takes three numbers and prints the largest number.

Program:
import java.util.Scanner;

class LargestNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter any three numbers:");

int num1 = sc.nextInt();


int num2 = sc.nextInt();
int num3 = sc.nextInt();

if (num1 > num2 && num1 > num3) {


System.out.println("largest num is " + num1);
} else if (num2 > num1 && num2 > num3) {
System.out.println("largest num is " + num2);
} else {
System.out.println("largest num is " + num3);
}

sc.close();
}
}

Output:
Enter any three numbers:
25 80 60
largest num is 80
3) WAP to Solve the quadratic equation: Ax2+Bx+C. (print imaginary roots also)
Program:
import java.util.Scanner;

public class QuadraticEq {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the coefficients A, B, and C of the quadratic equation:");


double A = scanner.nextDouble();
double B = scanner.nextDouble();
double C = scanner.nextDouble();

double discriminant = B * B - 4 * A * C;

if (discriminant > 0) {
double root1 = (-B + Math.sqrt(discriminant)) / (2 * A);
double root2 = (-B - Math.sqrt(discriminant)) / (2 * A);
System.out.println("The roots are real and distinct:");
System.out.println("Root 1 = " + root1);
System.out.println("Root 2 = " + root2);
} else if (discriminant == 0) {
double root = -B / (2 * A);
System.out.println("The roots are real and equal:");
System.out.println("Root 1 = Root 2 = " + root);
} else {
double realPart = -B / (2 * A);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * A);
System.out.println("The roots are complex and imaginary:");
System.out.println("Root 1 = " + realPart + " + " + imaginaryPart + "i");
System.out.println("Root 2 = " + realPart + " - " + imaginaryPart + "i");
}

scanner.close();
}
}

Output:
Enter the coefficients A, B, and C of the quadratic equation:
25 1 5
The roots are complex and imaginary:
Root 1 = -0.02 + 0.4467661580737735i
Root 2 = -0.02 - 0.4467661580737735i
4) WAP to print the day depending upon the number inputted by the user.
Program:
import java.util.Scanner;
class Days {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter a number from 1-7:");


int num = sc.nextInt();

switch (num) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturdday");
break;
default:
System.out.println("Invalid input");
break;
}
sc.close();
}
}
Output:

Enter a number from 1-7:


5
Thursday
5) WAP that asks the user to enter his/her marks of subjects and prints the corresponding
Grade. Grade Table: >=80: A, >=60: B, >=50: C, >=40: D, <40: F

Program:
import java.util.Scanner;

class GradeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter marks of each subjects:");


int subject1 = sc.nextInt();
int subject2 = sc.nextInt();
int subject3 = sc.nextInt();
double averageMarks = (subject1 + subject2 + subject3) / 3.0;

char grade;
if (averageMarks >= 80) {
grade = 'A';
} else if (averageMarks >= 60) {
grade = 'B';
} else if (averageMarks >= 50) {
grade = 'C';
} else if (averageMarks >= 40) {
grade = 'D';
} else {
grade = 'F';
}

System.out.println("Your grade is: " + grade);


sc.close();
}
}

Program:
Enter marks of each subjects:
80 90 60
Your grade is: B
6) WAP that reads a year from user and find if the year is a leap year.
Program:
import java.util.Scanner;

class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter a year:");
int year = sc.nextInt();

boolean isLeapYear = false;


if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
isLeapYear = true;
}
} else {
isLeapYear = true;
}
}

if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}

sc.close();
}
}
Output:
Enter a year:
2006
2006 is not a leap year.
7) A program should be able to calculate sum, difference, product, division of two numbers. Your program
should display the list of options from which user selects one of them. (Use switch case) a.Add b. Subtract c.
Product d. Division

Program:
import java.util.Scanner;

class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers:");
double num1 = sc.nextInt();
double num2 = sc.nextInt();

System.out.println("Choose operation:");
System.out.println("a:Sum b:Difference c:Multipy d:Division");
char operator = sc.next().charAt(0);

switch (operator) {
case 'a':
double sum = num1 + num2;
System.out.println("sum is " + sum);
break;
case 'b':
double difference = num1 - num2;
System.out.println("Difference is " + difference);
break;
case 'c':
double multiple = num1 * num2;
System.out.println("Product is " + multiple);
break;
case 'd':
double division = num1 / num2;
System.out.println("Division is " + division);
break;

default:
System.out.println("Invalid choice");
break;
}
sc.close();
}
}
Output:
Enter two numbers:
55 88
Choose operation:
a:Sum b:Difference c:Multipy d:Division
c
Product is 4840.0
8) WAP to read three sides of triangle and check the validity of triangle, also decide the type of triangle.
(Isosceles, equilateral, right angle) [If a, b, c are sides of a triangle then, a+b>c or b+c>a or a+c>b]

Program:
import java.util.Scanner;

public class TriangleClassifier {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the lengths of the three sides of the triangle:");


double side1 = scanner.nextDouble();
double side2 = scanner.nextDouble();
double side3 = scanner.nextDouble();

if (isValidTriangle(side1, side2, side3)) {


if (isEquilateral(side1, side2, side3)) {
System.out.println("It is an equilateral triangle.");
} else if (isIsosceles(side1, side2, side3)) {
System.out.println("It is an isosceles triangle.");
} else if (isRightAngled(side1, side2, side3)) {
System.out.println("It is a right-angled triangle.");
} else {
System.out.println("It is a scalene triangle.");
}
} else {
System.out.println("Invalid triangle!");
}
scanner.close();
}

public static boolean isValidTriangle(double side1, double side2, double side3) {


return (side1 + side2 > side3) && (side2 + side3 > side1) && (side1 + side3 > side2);
}

public static boolean isEquilateral(double side1, double side2, double side3) {


return side1 == side2 && side2 == side3;
}

public static boolean isIsosceles(double side1, double side2, double side3) {


return side1 == side2 || side1 == side3 || side2 == side3;
}

public static boolean isRightAngled(double side1, double side2, double side3) {


double[] sides = { side1, side2, side3 };
java.util.Arrays.sort(sides);
return Math.pow(sides[2], 2) == Math.pow(sides[0], 2) + Math.pow(sides[1], 2);
}
}
Output:
Enter the lengths of the three sides of the triangle:
55 23 56
It is a scalene triangle.
9) WAP that read one character and convert it into upper character case, if it is in lower case.
Program:
import java.util.Scanner;

class CheckChar {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a character:");
char ch = scanner.next().charAt(0);

if (Character.isLowerCase(ch)) {
char upperCaseChar = Character.toUpperCase(ch);
System.out.println("Uppercase character: " + upperCaseChar);
} else {
System.out.println("The character is not in lowercase.");
}
scanner.close();
}
}
Output:
Enter a character:
s
Uppercase character: S

10) WAP to find all the prime number between 1 to 100.

Program:

class PrimeNum {

public static void main(String[] args) {

int i, j;

for (i = 2; i < 100; i++) {

boolean isPrime = true;

for (j = 2; j < i / 2; j++) {

if (i % j == 0) {

isPrime = false;

break;

}}

if (isPrime) {

System.out.println(i + " ");

}}}}

Output:

2 3 4 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
11) WAP to find all the Armstrong number between 100 to 500.

Program:

public class Armstrong {

public static void main(String[] args) {

System.out.println("Armstrong numbers between 100 and 500:");

for (int i = 100; i <= 500; i++) {

if (isArmstrong(i)) {

System.out.print(i + " ");

public static boolean isArmstrong(int number) {

int originalNumber = number;

int numberOfDigits = String.valueOf(number).length();

int sum = 0;

while (number > 0) {

int digit = number % 10;

sum += Math.pow(digit, numberOfDigits);

number /= 10;

return sum == originalNumber;

Output:

Armstrong numbers between 100 and 500:

153 370 371 407


12) WAP to generate the Fibonacci series. {0 1 1 2 3 5 8 ..... up to 100}

Program:

public class Fibonacci {

public static void main(String[] args) {

int maxNumber = 100;

int prevNumber = 0;

int currentNumber = 1;

System.out.println("Fibonacci series up to " + maxNumber + ":");

System.out.print(prevNumber + " " + currentNumber + " ");

int nextNumber = prevNumber + currentNumber;

while (nextNumber <= maxNumber) {

System.out.print(nextNumber + " ");

prevNumber = currentNumber;

currentNumber = nextNumber;

nextNumber = prevNumber + currentNumber;

Output:

Fibonacci series up to 100:

0 1 1 2 3 5 8 13 21 34 55 89
13) WAP to generate multiplication table using while and for loop.

Program:

[for loop]

import java.util.Scanner;

public class Multiplication {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number:");

int number = sc.nextInt();

System.out.println("Multiplication table for " + number + ":");

for (int i = 1; i <= 10; i++) {

int result = number * i;

System.out.println(number + " x " + i + " = " + result);

[while loop]

while (i <= 10) {

int result = number * i;

System.out.println(number + " x " + i + " = " + result);

i++;

Output:

Enter a number:

5x1=5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50
14) WAP to read Annual salary of an employee and decide tax withheld as follows.
Salary tax up to 1,00,000 0%, UPTO 1,50,000 15% and above 1,50,000 25%

Program:
import java.util.Scanner;

public class TaxCalculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter the annual salary of the employee:");


double salary = sc.nextDouble();

double tax = 0;

if (salary <= 100000) {


tax = 0;
} else if (salary <= 150000) {
tax = (salary - 100000) * 0.15;
} else {
tax = (50000 * 0.15) + ((salary - 150000) * 0.25);
}

System.out.println("Tax withheld: " + tax);

sc.close();
}
}

Program:
Enter the annual salary of the employee:
200000
Tax withheld: 20000.0
15) WAP that prints the numbers from 201 t0 300(use while loop, do while, for loop)
Program:
public class PrintNum {
public static void main(String[] args) {

// for (int i = 201; i <= 300; i++) {


// System.out.println(i);
// }

int i = 201;
// while (i<=300) {
// System.out.println(i);
// i++;
// }

do {
System.out.println(i);
i++;
} while (i <= 300);
}
}
Output:
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273274 275 276 277 278
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

16) WAP that finds the sum of odd number from 0 to 100.
Program:
public class SumOfOdd {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 100; i++) {
if (i % 2 != 0) {
sum += i;
}
}
System.out.println(sum);
}
}

Output:
2500
17) WAP which display the average and sum of nth number input by the user.

Program:
import java.util.Scanner;

public class SumAndAverage {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the value of n: ");


int n = scanner.nextInt();

int sum = 0;

System.out.println("Enter " + n + " numbers:");


for (int i = 0; i < n; i++) {
int num = scanner.nextInt();
sum += num;
}

double average = (double) sum / n;

System.out.println("Sum of the numbers: " + sum);


System.out.println("Average of the numbers: " + average);

scanner.close();
}
}

Output:
Enter the value of n: 5
Enter 5 numbers:
42865
Sum of the numbers: 25
Average of the numbers: 5.0
18) WAP that reads two numbers x and y from the user and calculates the value of x to the power y. use while
and for loop.
Program:
[while loop] [for loop]

import java.util.Scanner; import java.util.Scanner;

public class PowerCalculatorWhileLoop { public class PowerCalculatorForLoop {

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

Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);

System.out.print("Enter the value of x: "); System.out.print("Enter the value of x: ");

double x = scanner.nextDouble(); double x = scanner.nextDouble();

System.out.print("Enter the value of y: "); System.out.print("Enter the value of y: ");

int y = scanner.nextInt(); int y = scanner.nextInt();

double result = 1; double result = 1;

int exponent = y; if (y < 0) {

if (exponent < 0) { x = 1 / x;

x = 1 / x; y = -y;

exponent = -exponent; }

} for (int i = 0; i < y; i++) {

while (exponent > 0) { result *= x;

result *= x; }

exponent--; System.out.println(x + " raised to the power " + y


+ " is: " + result);
}
scanner.close();
System.out.println(x + " raised to the power " + y
+ " is: " + result); }

scanner.close(); }

Output:

Enter the value of x: 5

Enter the value of y: 8

5.0 raised to the power 8 is: 390625.0


19) WAP to read a number and find whether the given number is palindrome or not.
Program:
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = scanner.nextInt();

if (isPalindrome(number)) {
System.out.println(number + " is a palindrome.");
} else {
System.out.println(number + " is not a palindrome.");
}

scanner.close();
}
public static boolean isPalindrome(int number) {
int originalNumber = number;
int reversedNumber = 0;

while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
return originalNumber == reversedNumber;
}
}

Output:
Enter a number: 88
88 is a palindrome.
20) WAP to generate the following pattern
1
12
123
1234
12345
123456
1234567

Program:
public class NumberPattern {
public static void main(String[] args) {
int rows = 7;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}

Output:
1
12
123
1234
12345
123456
1234567
21) WAP to generate the following pattern.

*
**
***
****
*****
******

Program:

public class StarPattern {

public static void main(String[] args) {

int rows = 6;

for (int i = 1; i <= rows; i++) {

for (int j = rows; j > i; j--) {

System.out.print(" ");

for (int k = 1; k <= i; k++) {

System.out.print("*");

System.out.println();

Output:

**

***

****

*****

******

You might also like