Java Code Examples with Comments
prac 2.1. WAP to find a diameter from given area of circle.
import java.util.Scanner;
public class prac_2_1 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter area");
Float a = SC.nextFloat(); // Input area of the circle
System.out.println(Math.sqrt((a / 3.14)) * 2); // Calculate diameter using the area
}
}
prac 2.2. WAP to check whether the given number is positive or negative.
import java.util.Scanner;
public class prac_2_2 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter number");
int n = SC.nextInt(); // Input the number
if (n > 0) {
System.out.println("Number is positive");
} else if (n == 0) {
System.out.println("Number is zero");
} else {
System.out.println("Number is negative");
}
}
}
prac 2.3. WAP to make a Simple Calculator using switch...case.
import java.util.Scanner;
public class prac_2_3 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter 1 for Addition");
System.out.println("enter 2 for Subtraction");
System.out.println("enter 3 for Multiplication");
System.out.println("enter 4 for division");
System.out.println("enter Choice");
int choice = SC.nextInt(); // Input operation choice
System.out.println("enter a and b");
int a = SC.nextInt(); // Input operands
int b = SC.nextInt();
switch (choice) {
case 1:
System.out.println(a + b); // Addition
break;
case 2:
System.out.println(a - b); // Subtraction
break;
case 3:
System.out.println(a * b); // Multiplication
break;
case 4:
System.out.println(a / b); // Division
break;
default:
System.out.println("enter valid choice");
}
}
}
prac 2.4. WAP to print numbers between two given numbers which are
divisible by 2 but not divisible by 3.
import java.util.Scanner;
public class prac_2_4 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter n1 and n2");
int n1 = SC.nextInt(); // Input lower bound
int n2 = SC.nextInt(); // Input upper bound
int n;
for (n = n1; n <= n2; n++) {
if (n % 2 == 0 && n % 3 != 0) { // Check if divisible by 2 but not by 3
System.out.println(n);
}
}
}
}
prac 2.5. WAP to find factorial of the given number.
import java.util.Scanner;
public class prac_2_5 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
int n, i, fact = 1;
System.out.println("enter n:");
n = SC.nextInt(); // Input number
for (i = 1; i <= n; i++) {
fact = fact * i; // Calculate factorial
}
System.out.println("factorial of " + n + " is " + fact);
}
}
prac 2.6. WAP that prompts the user to input a letter and check whether
it is a vowel or consonant.
import java.util.Scanner;
public class prac_2_6 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter character:");
char a = SC.next().charAt(0); // Input character
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' ||
a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U') {
System.out.println(a + " is a vowel"); // Check if vowel
} else {
System.out.println(a + " is a consonant"); // Otherwise consonant
}
}
}
prac 2.7. WAP to calculate the monthly telephone bill based on the
number of calls.
import java.util.Scanner;
public class prac_2_7 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter the number of calls:");
int n = SC.nextInt(); // Input number of calls
double bill = 200.0; // Minimum bill amount
if (n > 100) { // Calculate additional charges based on call count
int ex = n - 100;
bill += ex * 0.60;
if (n > 150) {
ex = n - 150;
bill += ex * 0.50;
if (n > 200) {
ex = n - 200;
bill += ex * 0.40;
}
}
}
System.out.println("the total bill amount is " + bill); // Display total bill
}
}
prac 2.8. WAP to find whether the given number is prime or not.
import java.util.Scanner;
public class prac_2_8 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter the number");
int n = SC.nextInt(); // Input number
int flag = 0, i;
for (i = 2; i < n; i++) {
if (n % i == 0) { // Check for factors
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println(n + " is prime number"); // Prime number check
} else {
System.out.println(n + " is not prime number");
}
}
}
prac 2.9. WAP to find out the largest number from given three numbers
without using Logical Operator.
import java.util.Scanner;
public class prac_2_9 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter the numbers");
int n1 = SC.nextInt(); // Input numbers
int n2 = SC.nextInt();
int n3 = SC.nextInt();
int max;
// Determine maximum using ternary operator
max = n1 > n2 ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
System.out.println("Largest no is " + max); // Display maximum number
}
}
prac 2.10. WAP to read marks of five subjects and calculate percentage
and print class accordingly.
import java.util.Scanner;
public class prac_2_10 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
int n1, n2, n3, n4, n5, total;
System.out.println("enter marks of five subjects (out of 20)");
n1 = SC.nextInt(); // Input marks
n2 = SC.nextInt();
n3 = SC.nextInt();
n4 = SC.nextInt();
n5 = SC.nextInt();
total = n1 + n2 + n3 + n4 + n5; // Calculate total marks
System.out.println("total is " + total);
// Determine class based on total percentage
if (total > 70) {
System.out.println("Distinction!!!");
} else if (total >= 60 && total < 70) {
System.out.println("Distinction!!!");
} else if (total >= 45 && total < 60) {
System.out.println("Second Class");
} else if (total >= 35 && total < 45) {
System.out.println("Pass");
} else if (total < 35 && total >= 0) {
System.out.println("Fail!!");
}
}
}
prac 2.11. WAP to check type of triangle based on its sides (isosceles,
equilateral, scalene).
import java.util.Scanner;
public class prac_2_11 {
public static void main(String[] args) {
Scanner SC = new Scanner(System.in);
System.out.println("enter sides of triangle");
int n1 = SC.nextInt(); // Input sides of triangle
int n2 = SC.nextInt();
int n3 = SC.nextInt();
if (n1 == n2 && n2 == n3) {
System.out.println("it is an equilateral triangle"); // Check if equilateral
}
else if (n1 == n2 || n1 == n3 || n2 == n3) {
System.out.println("it is an isosceles triangle"); // Check if isosceles
}
else {
System.out.println("it is a scalene triangle"); // Otherwise scalene
}
}
}
Comments
Post a Comment