Category: Example

https://cdn-icons-png.flaticon.com/256/5486/5486145.png

  • Java Program to Calculate Simple Interest:

    import java.util.Scanner;
    
    public class SimpleInterest {
    
     public static void main(String[] args) {
    
     Scanner input = new Scanner(System.in);
    
     System.out.print(“Enter principal amount: “);
    
     double principal = input.nextDouble();
    
     System.out.print(“Enter rate of interest: “);
    
     double rate = input.nextDouble();
    
     System.out.print(“Enter time period in years: “);
    
     double time = input.nextDouble();
    
     double simpleInterest = (principal * rate * time) / 100;
    
     System.out.println(“Simple Interest: ” + simpleInterest);
    
     input.close();
    
     }

    Output:

    Enter principal amount: 5000
    
    Enter rate of interest: 2.5
    
    Enter time period in years: 3
    
    Simple Interest: 375.0
  • Calculate Compound Interest in Java:

    public class CompoundInterest {
    
     public static void main(String[] args) {
    
     double principal = 15000, rate = 5.5, time = 3;
    
     double compoundInterest = principal * (Math.pow((1 + rate / 100), time)) – principal;
    
     System.out.println(“Compound Interest: ” + compoundInterest);
    
     }

    Output:

    Compound Interest: 2653.4375
  • Check Vowel or Consonant in Java:

    public class VowelConsonant {
    
     public static void main(String[] args) {
    
     char ch = ‘A’;
    
     if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’
    
     || ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’) {
    
     System.out.println(ch + ” is a vowel.”);
    
     } else {
    
     System.out.println(ch + ” is a consonant.”);
    
     }
    
     }

    Output:

    A is a vowel.
  • Check Leap Year in Java:

    public class LeapYear {
    
     public static void main(String[] args) {
    
     int year = 2024;
    
     if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
    
     System.out.println(year + ” is a leap year.”);
    
     } else {
    
     System.out.println(year + ” is not a leap year.”);
    
     }
    
     }

    Output:

    2024 is a leap year.
  • Multiply Two Numbers in Java:

    public class MultiplyTwoNumbers {
    
     public static void main(String[] args) {
    
     double first = 2.5, second = 4.5;
    
     double product = first * second;
    
     System.out.println(“The product is: ” + product);
    
     }

    Output:

    The product is: 11.25
  • Add Two Complex Numbers in Java:

    class Complex {
    
     double real, imaginary;
    
     Complex(double r, double i) {
    
     this.real = r;
    
     this.imaginary = i;
    
     }
    
     public static Complex add(Complex c1, Complex c2) {
    
     Complex temp = new Complex(0, 0);
    
     temp.real = c1.real + c2.real;
    
     temp.imaginary = c1.imaginary + c2.imaginary;
    
     return temp;
    
     }
    
    }
    
    public class AddComplexNumbers {
    
     public static void main(String[] args) {
    
     Complex c1 = new Complex(4.5, 5);
    
     Complex c2 = new Complex(2.5, 3.5);
    
     Complex temp = Complex.add(c1, c2);
    
     System.out.println(“Sum = ” + temp.real + ” + ” + temp.imaginary + “i”);
    
     }
    
    }

    Output:

    Sum = 7.0 + 8.5i
  • Add Two Binary Numbers in Java:

    public class AddBinaryNumbers {
    
     public static void main(String[] args) {
    
     long binary1 = 1010, binary2 = 1101;
    
     int i = 0, remainder = 0;
    
     long[] sum = new long[20];
    
     while (binary1 != 0 || binary2 != 0) {
    
     sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
    
     remainder = (int) (binary1 % 10 + binary2 % 10 + remainder) / 2;
    
     binary1 = binary1 / 10;
    
     binary2 = binary2 / 10;
    
     }
    
     if (remainder != 0) {
    
     sum[i++] = remainder;
    
     }
    
     –i;
    
     System.out.print(“Sum of two binary numbers: “);
    
     while (i >= 0) {
    
     System.out.print(sum[i–]);
    
     }
    
     }

    Output:

    Sum of two binary numbers: 11011
  • Print Pattern in Java:

    public class PrintPattern {
    
     public static void main(String[] args) {
    
     int rows = 5;
    
     for (int i = 1; i <= rows; ++i) {
    
     for (int j = 1; j <= i; ++j) {
    
     System.out.print(“* “);
    
     }
    
     System.out.println();
    
     }
    
     }
    
    }

    Output:

    * 
    
    * * 
    
    * * * 
    
    * * * * 
    
    * * * * *
  • Factorial of a Number:

    public class Factorial {
    
     public static void main(String[] args) {
    
     int num = 5, factorial = 1;
    
     for(int i = 1; i <= num; ++i) {
    
     factorial *= i;
    
     }
    
     System.out.println(“Factorial of ” + num + ” is: ” + factorial);
    
     }

    Output:

    Factorial of 5 is: 120
  • Check Even or Odd Number:

    public class EvenOdd {
    
     public static void main(String[] args) {
    
     int num = 5;
    
     if(num % 2 == 0)
    
     System.out.println(num + ” is even.”);
    
     else
    
     System.out.println(num + ” is odd.”);
    
     }

    Output:

    5 is odd.