Scanner Exercises

What is your name?

Write a programme that asks the user for their name and prints it.

Show answer:
"import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Your name is: " + name);
        sc.close();
    }
}

Add two numbers

Write a programme that asks the user to input two numbers and prints their sum.

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        int num1 = sc.nextInt();
        System.out.print("Enter the second number: ");
        int num2 = sc.nextInt();
        int sum = num1 + num2;
        System.out.println("The sum is: " + sum);
        sc.close();
    }
}

Multiply two numbers

Write a programme that asks the user to input two numbers and prints their sum.

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        double num1 = sc.nextDouble();
        System.out.print("Enter the second number: ");
        double num2 = sc.nextDouble();
        double product = num1 * num2;
        System.out.println("The product is: " + product);
        sc.close();
    }
}

Read and display a sentence

Ask the user for a sentence and print it.

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String sentence = sc.nextLine();
        System.out.println("Your sentence: " + sentence);
        sc.close();
    }
}

Check prime number

Ask the user to input a number and check if it is prime.

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        boolean isPrime = true;

        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime && num > 1) {
            System.out.println(num + " is a prime number.");
        } else {
            System.out.println(num + " is not a prime number.");
        }
        sc.close();
    }
}

Count vowels in a string

Ask the user for a string and count the number of vowels (a, e, i, o, u).

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = sc.nextLine();
        int vowelCount = 0;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            String vowels = "aeiouAEIOU";
            If(vowels.contains(Character.toString(c)))
               vowelCount++;
        }
        System.out.println("Number of vowels: " + vowelCount);
        sc.close();
    }
}

Factorial of a number

Write a programme to calculate the factorial of a number using a loop.

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        long factorial = 1;

        for (int i = 1; i <= num; i++) {
            factorial *= i;
        }
        System.out.println("Factorial of " + num + " is: " + factorial);
        sc.close();
    }
}

Sum of digits

Write a programme that calculates the sum of digits of a number entered by the user.

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        int sum = 0;

        while (num != 0) {
            sum += num % 10;
            num /= 10;
        }
        System.out.println("Sum of digits: " + sum);
        sc.close();
    }
}

Palindrome checker

Write a programme that checks if the input string is a palindrome (a word or phrase that reads the same forwards and backwards, ignoring spaces, punctuation, and case).

Show answer:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Create a scanner object to read input from the user
        Scanner sc = new Scanner(System.in);

        // Ask the user for a string input
        System.out.print("Enter a string: ");
        String input = sc.nextLine();

        // Remove spaces and convert to lowercase to ensure comparison is case insensitive
        String cleanedInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

        // Initialise a boolean flag to check if it's a palindrome
        boolean isPalindrome = true;

        // Loop to check if the string is the same forwards and backwards
        for (int i = 0; i < cleanedInput.length() / 2; i++) {
            if (cleanedInput.charAt(i) != cleanedInput.charAt(cleanedInput.length() - 1 - i)) {
                isPalindrome = false; // Set flag to false if not a palindrome
                break; // Exit the loop if we find a mismatch
            }
        }

        // Display the result
        if (isPalindrome) {
            System.out.println(input + " is a palindrome.");
        } else {
            System.out.println(input + " is not a palindrome.");
        }

        // Close the scanner
        sc.close();
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *

This site builds beginner confidence through fundamental coding concepts and regular practice. Java is the primary language, but the techniques apply across all programming languages.

Learn. Practice. Master

Categories