lab-Report-Java
lab-Report-Java
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);
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;
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);
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:
Program:
import java.util.Scanner;
class GradeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
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';
}
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();
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;
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
Program:
class PrimeNum {
int i, j;
if (i % j == 0) {
isPrime = false;
break;
}}
if (isPrime) {
}}}}
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:
if (isArmstrong(i)) {
int sum = 0;
number /= 10;
Output:
Program:
int prevNumber = 0;
int currentNumber = 1;
prevNumber = currentNumber;
currentNumber = nextNumber;
Output:
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;
System.out.println("Enter a number:");
[while loop]
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;
double tax = 0;
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) {
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;
int sum = 0;
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]
public static void main(String[] args) { public static void main(String[] args) {
if (exponent < 0) { x = 1 / x;
x = 1 / x; y = -y;
exponent = -exponent; }
result *= x; }
scanner.close(); }
Output:
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;
Output:
1
12
123
1234
12345
123456
1234567
21) WAP to generate the following pattern.
*
**
***
****
*****
******
Program:
int rows = 6;
System.out.print(" ");
System.out.print("*");
System.out.println();
Output:
**
***
****
*****
******