Gopi

                                                                CORE JAVA 

1. Write a program to multiply two numbers and display the result.

Program

import java.util.Scanner;
public class q1{

        public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();

        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();
        double result = num1 * num2;
        System.out.println("The result of multiplication is: " + result);

        scanner.close();
    }
}

Output:
   

2.Write a program to divide two numbers and display the quotient.

Program :

import java.util.Scanner;
public class q2 {

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

        try {
            System.out.print("Enter the dividend : ");
            double dividend = scanner.nextDouble();

            System.out.print("Enter the divisor : ");
            double divisor = scanner.nextDouble();
            if (divisor == 0) {
                System.out.println("Error: Not divide by zero.");
            } else {
                double quotient = dividend / divisor;
                System.out.println("The quotient is: " + quotient);
            }
        } catch (Exception e) {
            System.out.println("Invalid input! Please enter valid numbers.");
        } finally {
            scanner.close();
        }
    }
}

Output:


3.Write a program to find the remainder when one number is divided by another.

Program:

import java.util.Scanner;
public class q3 {

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

        try {
            System.out.print("Enter the dividend (number to be divided): ");
            int dividend = scanner.nextInt();

            System.out.print("Enter the divisor (number to divide by): ");
            int divisor = scanner.nextInt();
            if (divisor == 0) {
                System.out.println("Error: Cannot divide by zero.");
            } else {
                int remainder = dividend % divisor;
                System.out.println("The remainder is: " + remainder);
            }

        } catch (Exception e) {
            System.out.println("Invalid input! Please enter valid integers.");
        } finally {
            scanner.close();
        }
    }
}

Output:

4.Write a program to swap two numbers using a temporary variable.

Program:

import java.util.Scanner;
public class q4{

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

        try {
            System.out.print("Enter the first number (a): ");
            int a = scanner.nextInt();

            System.out.print("Enter the second number (b): ");
            int b = scanner.nextInt();

            System.out.println("\nBefore Swapping:");
            System.out.println("a = " + a);
            System.out.println("b = " + b);
            int temp = a;
            a = b;
            b = temp;

            System.out.println("\nAfter Swapping:");
            System.out.println("a = " + a);
            System.out.println("b = " + b);

        } catch (Exception e) {
            System.out.println("Invalid input! Please enter valid integers.");
        } finally {
            scanner.close();
        }
    }
}

Output:

5.Write a program to swap two numbers without using a temporary variable.

Program :

import java.util.Scanner;
public class q5{

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

        try {
            System.out.print("Enter the first number (a): ");
            int a = scanner.nextInt();

            System.out.print("Enter the second number (b): ");
            int b = scanner.nextInt();

            System.out.println("\nBefore Swapping:");
            System.out.println("a = " + a);
            System.out.println("b = " + b);
            a = a + b;
            b = a - b;
            a = a - b;

            System.out.println("\nAfter Swapping:");
            System.out.println("a = " + a);
            System.out.println("b = " + b);

        } catch (Exception e) {
            System.out.println("Invalid input! Please enter valid integers.");
        } finally {
            scanner.close();
        }
    }
}

Output :

6.Write a program to calculate the area of a rectangle (length × width).

Program:


import java.util.Scanner;

public class q6 {

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

        try {
            System.out.print("Enter the length of the rectangle: ");
            double length = scanner.nextDouble();

            System.out.print("Enter the width of the rectangle: ");
            double width = scanner.nextDouble();
            double area = length * width;
            System.out.println("The area of the rectangle is: " + area);

        } catch (Exception e) {
            System.out.println("Invalid input! Please enter valid numbers.");
        } finally {
            scanner.close();
        }
    }
}

Output :

7.Write a program to calculate the perimeter of a rectangle.

Program :

import java.util.Scanner;
public class q7 {

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

        try{
            System.out.print("Enter the length of the rectangle: ");
            double length = scanner.nextDouble();

            System.out.print("Enter the width of the rectangle: ");
            double width = scanner.nextDouble();

            if (length < 0 || width < 0) {
                System.out.println("Length and width must be non-negative.");
            } else {

                double perimeter = 2 * (length + width);

                System.out.println("The perimeter of the rectangle is: " + perimeter);
            }

        } catch (Exception e) {
            System.out.println("Invalid input! Please enter valid numbers.");
        } finally {
            scanner.close();
        }
    }
}
Output:

8.Write a program to calculate the area of a circle (π × r × r).

Program:

import java.util.Scanner;

public class q8 {

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

        try {
            System.out.print("Enter the radius of the circle: ");
            double radius = scanner.nextDouble();

            if (radius < 0) {
                System.out.println("Radius cannot be negative.");
            } else {
                double area = Math.PI * radius * radius;
                System.out.println("The area of the circle is: " + area);
            }

        } catch (Exception e) {
            System.out.println("Invalid input! Please enter a valid number.");
        } finally {
            scanner.close();
        }
    }
}

Output:

9.Write a program to calculate the circumference of a circle (2 × π × r).

Program:

import java.util.Scanner;
public class q9 {
    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();
        double circumference = 2 * Math.PI * radius;
        System.out.printf("The circumference of the circle is: %.2f%n", circumference);

        scanner.close();
    }
}

Output:



10. Write a program to calculate the average of three numbers.

Program:

import java.util.Scanner;
public class q10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();

        System.out.println("Enter the second number: ");
        double num2 = scanner.nextDouble();

        System.out.println("Enter the third number: ");
        double num3 = scanner.nextDouble();
        double average = (num1 + num2 + num3) / 3;
        System.out.printf("The average of the three numbers is: %.2f%n", average);
        scanner.close();
    }
}

Output:

11.Write a program to convert temperature from Celsius to Fahrenheit.

Program:

import java.util.Scanner;

public class q11{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter temperature in Celsius: ");
        double celsius = scanner.nextDouble();
        double fahrenheit = (celsius * 9 / 5) + 32;
        System.out.printf("%.2f°C is equal to %.2f°F%n", celsius, fahrenheit);
        scanner.close();
    }
}

Output:



12.Write a program to convert temperature from Fahrenheit to Celsius.

Program:

import java.util.Scanner;

public class q12{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter temperature in Fahrenheit: ");
        double fahrenheit = scanner.nextDouble();
        double celsius = (fahrenheit - 32) * 5 / 9;
        System.out.printf("fahrenheit", fahrenheit, celsius);
        scanner.close();
    }
}

Output:




13.Write a program to calculate the square of a number.

Program:

import java.util.Scanner;

public class q13{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        double g = scanner.nextDouble();
        double square = g * g;
        System.out.print("The square of %.2f is %.2f%n :"+g+"|"+square);
        scanner.close();
    }
}

Output:


14.Write a program to calculate the cube of a number.

Program:

import java.util.Scanner;

public class q14 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a num to find its a: ");
        double num= scanner.nextDouble();
        double a = num * num * num;
        System.out.println("A of " + num + " is: " + a);
        scanner.close();
    }
}

Output:


15.Write a program to add the digits of a 2-digit number (e.g., 47 → 4+7=11).

Program:


import java.util.Scanner;

public class q15{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a 2-digit number: ");
        int number = scanner.nextInt();
        if (number >= 10 && number <= 99) {
            int firstDigit = number / 10;
            int secondDigit = number % 10; 
            int sum = firstDigit + secondDigit;
            System.out.println(firstDigit + " + " + secondDigit + " = " + sum);
        } else {
            System.out.println("enter a 2-digit number");
        }

        scanner.close();
    }
}
Output:


16.Write a program to find the simple interest (SI = P × R × T / 100).

Program:

import java.util.Scanner;

public class q16 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the Principal amount (P): ");
        double principal = scanner.nextDouble();

        System.out.print("Enter the Rate of interest (R) per annum: ");
        double rate = scanner.nextDouble();

        System.out.print("Enter the Time period in years (T): ");
        double time = scanner.nextDouble();
        double simpleInterest = (principal * rate * time) / 100;
        System.out.println("Simple Interest = " + simpleInterest);

        scanner.close();
    }
}


Output:


17.Write a program to find the total and average marks of 5 subjects.

Program:

import java.util.Scanner;

public class q17{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] marks = new int[5];
        int total = 0;
        for (int i = 0; i < 5; i++) {
            System.out.print("Enter marks for Subject " + (i + 1) + ": ");
            marks[i] = scanner.nextInt();
            total += marks[i]; // Add to total
        }
        double average = (double) total / 5;
        System.out.println("Total Marks = " + total);
        System.out.println("Average Marks = " + average);

        scanner.close();
    }
}

Output:


18.Write a program to calculate the monthly salary when daily wage and working days are given.

Program:


import java.util.Scanner;

public class q18{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the daily wage: ");
        double dailyWage = scanner.nextDouble();

        System.out.print("Enter the number of working days in the month: ");
        int workingDays = scanner.nextInt();
        double monthlySalary = dailyWage * workingDays;
        System.out.println("Monthly Salary = " + monthlySalary);

        scanner.close();
    }
}

Output:


Comments

Popular posts from this blog

Animation

looping