Java Code Examples

ALL Examples can find >> https://www.programiz.com/java-programming/examples


Example: How to Print an Integer entered by an user

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {

        // Creates a reader instance which takes
        // input from standard input - keyboard
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter a number: ");

        // nextInt() reads the next integer from the keyboard
        int number = reader.nextInt();

        // println() prints the following line to the output screen
        System.out.println("You entered: " + number);
    }
}

Output

Enter a number: 10
You entered: 10

Example: Program to Add Two Integers

class Main {

  public static void main(String[] args) {
    
    System.out.println("Enter two numbers");
    int first = 10;
    int second = 20;
    
    System.out.println(first + " " + second);

    // add two numbers
    int sum = first + second;
    System.out.println("The sum is: " + sum);
  }
}

Output:

Enter two numbers
10 20
The sum is: 30

Example: Multiply Two Floating-Point Numbers

public class MultiplyTwoNumbers {

    public static void main(String[] args) {

        float first = 1.5f;
        float second = 2.0f;

        float product = first * second;

        System.out.println("The product is: " + product);
    }
}

Output

The product is: 3.0

Example: Find ASCII value of a character

public class AsciiValue {

    public static void main(String[] args) {

        char ch = 'a';
        int ascii = ch;
        // You can also cast char to int
        int castAscii = (int) ch;

        System.out.println("The ASCII value of " + ch + " is: " + ascii);
        System.out.println("The ASCII value of " + ch + " is: " + castAscii);
    }
}

Output

The ASCII value of a is: 97
The ASCII value of a is: 97

Example: Compute Quotient and Remainder

public class QuotientRemainder {

  public static void main(String[] args) {

    int dividend = 25, divisor = 10;

    int quotient = dividend / divisor;
    int remainder = dividend % divisor;

    System.out.println("Quotient = " + quotient);
    System.out.println("Remainder = " + remainder);
  }
}

Output:

Quotient = 2
Remainder = 5

Example : Swap two numbers without using temporary variable

public class SwapNumbers {

    public static void main(String[] args) {

        float first = 12.0f, second = 24.5f;

        System.out.println("--Before swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);

        first = first - second;
        second = first + second;
        first = second - first;

        System.out.println("--After swap--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + second);
    }
}

Output:

--Before swap--
First number = 12.0
Second number = 24.5
--After swap--
First number = 24.5
Second number = 12.0

No comments:

Post a Comment