0% found this document useful (0 votes)
1 views13 pages

Screenshot 2023-06-11 at 6.35.18 PM

The document contains multiple Java programs that demonstrate various functionalities such as swapping values, calculating the perimeter of shapes, checking for squares, determining leap years, and evaluating mathematical expressions. Each program utilizes user input through the Scanner class to perform calculations and display results. The examples cover basic programming concepts including conditionals, loops, and mathematical operations.

Uploaded by

kathirganeshkk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views13 pages

Screenshot 2023-06-11 at 6.35.18 PM

The document contains multiple Java programs that demonstrate various functionalities such as swapping values, calculating the perimeter of shapes, checking for squares, determining leap years, and evaluating mathematical expressions. Each program utilizes user input through the Scanner class to perform calculations and display results. The examples cover basic programming concepts including conditionals, loops, and mathematical operations.

Uploaded by

kathirganeshkk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1) public class SwapValues {

public static void main(String[] args) {

int a = 10;

int b = 20;

System.out.println("Before swapping:");

System.out.println("a = " + a);

System.out.println("b = " + b);

// Swapping without using a third variable

a = a + b;

b = a - b;

a = a - b;

System.out.println("After swapping:");

System.out.println("a = " + a);

System.out.println("b = " + b);

}
2) import java.util.Scanner;

public class CirclePerimeter {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the radius of the circle: ");

double radius = scanner.nextDouble();

// Calculate perimeter

double perimeter = 2 * Math.PI * radius;

System.out.println("Perimeter of the circle: " + perimeter);

scanner.close();

}
3) import java.util.Scanner;

public class SimpleInterestCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the principal amount: ");

double principal = scanner.nextDouble();

System.out.print("Enter the interest rate (in percentage): ");

double rate = scanner.nextDouble();

System.out.print("Enter the time (in years): ");

double time = scanner.nextDouble();

// Calculate simple interest

double interest = (principal * rate * time) / 100;

System.out.println("Simple Interest: " + interest);

scanner.close();

}
4)import java.util.Scanner;

public class QuadrilateralPerimeter {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the lengths of the four sides of the quadrilateral:");

System.out.print("Side 1: ");

double side1 = scanner.nextDouble();

System.out.print("Side 2: ");

double side2 = scanner.nextDouble();

System.out.print("Side 3: ");

double side3 = scanner.nextDouble();

System.out.print("Side 4: ");

double side4 = scanner.nextDouble();

// Calculate perimeter

double perimeter = side1 + side2 + side3 + side4;

System.out.println("Perimeter of the quadrilateral: " + perimeter);

scanner.close();

}
5) import java.util.Scanner;

public class SquareChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the length of the rectangle: ");

double length = scanner.nextDouble();

System.out.print("Enter the breadth of the rectangle: ");

double breadth = scanner.nextDouble();

// Check if it is a square

if (length == breadth) {

System.out.println("It is a square.");

} else {

System.out.println("It is not a square.");

scanner.close();

}
6) import java.util.Scanner;

public class CharacterChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

char ch = scanner.next().charAt(0);

// Check if it is a character

if (Character.isLetter(ch)) {

if (Character.isUpperCase(ch)) {

System.out.println("It is an uppercase character.");

} else {

System.out.println("It is a lowercase character.");

} else {

System.out.println("It is not a character.");

scanner.close();

}
7) import java.util.Scanner;

public class AscendingOrder {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");

int num2 = scanner.nextInt();

System.out.print("Enter the third number: ");

int num3 = scanner.nextInt();

// Arrange the numbers in ascending order

if (num1 > num2) {

int temp = num1;

num1 = num2;

num2 = temp;

if (num2 > num3) {

int temp = num2;

num2 = num3;

num3 = temp;

if (num1 > num2) {

int temp = num1;


num1 = num2;

num2 = temp;

System.out.println("Numbers in ascending order: " + num1 + ", " + num2 + ", " + num3);

scanner.close();

}
8) import java.util.Scanner;

public class LeapYearChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int year = scanner.nextInt();

// Check if it is a leap year

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.");

scanner.close();

}
9) import java.util.Scanner;

public class SeasonName {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the season code (S/W/A/G): ");

char code = scanner.next().charAt(0);

String seasonName;

// Determine season name based on code

switch (code) {

case 'S':

seasonName = "Summer";

break;

case 'W':

seasonName = "Winter";

break;

case 'A':
seasonName = "Autumn";

break;

case 'G':

seasonName = "Spring";

break;

default:
seasonName = "Invalid season code";

break;

System.out.println("Season name: " + seasonName);

scanner.close();

}
10) import java.util.Scanner;

public class EvaluationMenu {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int option;

double number;

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

number = scanner.nextDouble();

System.out.println("Evaluation Menu");

System.out.println("A. Absolute value of the number");

System.out.println("B. Square root of the number");

System.out.println("C. Random number between 1 and 0");

System.out.println("D. Natural logarithm of the number");

System.out.print("Select an option (A/B/C/D): ");

option = scanner.next().toUpperCase().charAt(0);

switch (option) {

case 'A':

double absoluteValue = Math.abs(number);

System.out.println("Absolute value: " + absoluteValue);

break;

case 'B':

if (number >= 0) {

double squareRoot = Math.sqrt(number);

System.out.println("Square root: " + squareRoot);

} else {

System.out.println("Invalid input. Cannot calculate square root of a negative number.");

break;
case 'C':

double randomNum = Math.random();

System.out.println("Random number between 1 and 0: " + randomNum);

break;

case 'D':

if (number > 0) {

double naturalLog = Math.log(number);

System.out.println("Natural logarithm: " + naturalLog);

} else {

System.out.println("Invalid input. Cannot calculate natural logarithm of a non-positive


number.");

break;

default:

System.out.println("Invalid option selected.");

break;

scanner.close();

}
11) import java.util.Scanner;

public class DiscriminantCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the coefficient a: ");

double a = scanner.nextDouble();

System.out.print("Enter the coefficient b: ");

double b = scanner.nextDouble();

System.out.print("Enter the coefficient c: ");

double c = scanner.nextDouble();

// Calculate the discriminant

double discriminant = b * b - 4 * a * c;

// Round the discriminant to the nearest whole number

long roundedDiscriminant = Math.round(discriminant);

System.out.println("Discriminant: " + roundedDiscriminant);


scanner.close();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy