Java Program to Swap Two Numbers using temporary variable

Example 1: Swap two numbers using temporary variable
public class SwapNumbers {

    public static void main(String[] args) {

        float first = 1.20f, second = 2.45f;

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

        // Value of first is assigned to temporary
        float temporary = first;

        // Value of second is assigned to first
        first = second;

        // Value of temporary (which contains the initial value of first) is assigned to second
        second = temporary;

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

Output:

--Before swap--
First number = 1.2
Second number = 2.45
--After swap--
First number = 2.45
Second number = 1.2
[mai mult...]

Java Program to Find ASCII Value of a character

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
[mai mult...]

Java Program to Print an Integer (Entered by the User)

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
[mai mult...]

Create a lap timer with Python

The user needs to press ENTER to complete each lap. The timer keeps counting till CTRL+SHIFT is pressed.For each lap we calculate the lap time by subtracting the current time from the total time at the end of the previous lap. The time() function of the time module, returns the current epoch time in milliseconds.

[mai mult...]

Delete a file with Python

To delete a file with this script, we can use the os module.It’s recommended to check with a conditional if the file exist before calling the remove() function from the module:

import os

if os.path.exists("<file_path>"):
  os.remove("<file_path>")
else:
  <code>

[mai mult...]