Practice problems
- Write a java program to check whether a character is alphabet, digit or special character.
Example
Input
Input any character: E
Expected output
Alpabet
Hint
Every character, like letters, numbers, or special symbols, has a unique number assigned to it, called the ASCII value. When you input a character, we can figure out if it's a letter, number, or special character based on its ASCII value.
Here are the ranges:
For big letters (A to Z): ASCII values are between 65 and 90.
For small letters (a to z): ASCII values are between 97 and 122.
For numbers (0 to 9): ASCII values are between 48 and 57.
So, by checking the ASCII value, we can tell what kind of character you entered!
Solution Code
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter any input:");
Scanner sc = new Scanner(System.in);
char input = sc.next().charAt(0);
// check small and capital letters.
if (input >= 48 && input <= 57)
System.out.println("Digit ");
// Check digit
else if ((input >= 65 && input <= 90)
|| (input >= 97 && input <= 122))
System.out.println("Alphabet ");
// Else Special character
else
System.out.println("Special Character ");
}
}
- Write a Java program to input amount from user and print minimum number of notes (Rs. 500, 100, 50, 20, 10, 5, 2, 1) required for the amount.
Input
567890
Output
Total Number of Notes
500 = 1135
100 = 3
50 = 1
20 = 2
10 = 0
5 = 0
2 = 0
1 = 0
Solution Code
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the Amount :");
int total_amount = input.nextInt();
int cnt_500, cnt_100, cnt_50, cnt_20, cnt_10, cnt_5, cnt_2, cnt_1;
cnt_500 = cnt_100 = cnt_50 = cnt_20 = cnt_10 = cnt_5 = cnt_2 = cnt_1 = 0;
if(total_amount >= 500)
cnt_500 = total_amount/500;
total_amount -= cnt_500 * 500;
if(total_amount >= 100)
cnt_100 = total_amount/100;
total_amount -= cnt_100 * 100;
if(total_amount >= 50)
cnt_50 = total_amount/50;
total_amount -= cnt_50 * 50;
if(total_amount >= 20)
cnt_20 = total_amount/20;
total_amount -= cnt_20 * 20;
if(total_amount >= 10)
cnt_10 = total_amount/10;
total_amount -= cnt_10 * 10;
if(total_amount >= 5)
cnt_5 = total_amount/5;
total_amount -= cnt_5 * 5;
if(total_amount >= 2)
cnt_2 = total_amount /2;
total_amount -= cnt_2 * 2;
if(total_amount >= 1)
cnt_1 = total_amount;
System.out.println("Total number of Notes");
System.out.println("500 = "+ cnt_500);
System.out.println("100 = "+ cnt_100);
System.out.println("50 = "+ cnt_50);
System.out.println("20 = "+ cnt_20);
System.out.println("10 = "+ cnt_10);
System.out.println("5 = "+ cnt_5);
System.out.println("2 = "+ cnt_2);
System.out.println("1 = "+ cnt_1);
}
}
- Write a Java program to print natural numbers in reverse from n to 1.
Input
7
Expected Output
7 6 5 4 3 2 1
Solution Code using for loop
import java.util.*;
class Main {
public static void main(String[] args) {
System.out.print("Enter the number : ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = n; i >= 1; i--)
{
System.out.print(i +" ");
}
}
}
Solution Code using while loop
import java.util.*;
class Main {
public static void main(String[] args) {
System.out.print("Enter the number : ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = n;
while(i >= 1)
{
System.out.print(i +" ");
i--;
}
}
}
- Write a Java program to print alphabets from a to z.
Expected output
a b c d e f g h i j k l m n o p q r s t u v w x y z
Solution Code
class Main {
public static void main(String[] args) {
// Print the letters
System.out.println("Alphabets from a to z are:");
// Using for loop to traverse each character.
for (char i = 'a'; i <= 'z'; i++) {
// Print the alphabet
System.out.printf("%c ", i);
}
}
}
- Write a Java program to find sum of even numbers between 1 to n.
Input
10
Expected Output
Total sum of even numbers: 30
Solution Code
import java.util.*;
class Main {
public static void main(String[] args) {
System.out.println("Enter the number : ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for(int i=2; i<=n; i+=2){
sum += i;
}
System.out.println("Total sum of even numbers: " + sum);
}
}