1.
Write a program to check given character is vowel or
consonant.
Code:-
import java.util.Scanner;
public class VowelOrConsonant {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input from the user
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
// Check if the input is a single alphabetic character
if (Character.isLetter(ch)) {
// Convert to lowercase for case-insensitive comparison
ch = Character.toLowerCase(ch);
// Check if the character is a vowel
if (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.");
}
} else {
System.out.println("Invalid input! Please enter a single
alphabetic character.");
}
scanner.close();
}
}
Output:-
2. Write a program to check if two strings are equals or not.
Code:-
import java.util.Scanner;
public class StringEqualityCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the first string
System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();
// Input the second string
System.out.print("Enter the second string: ");
String str2 = scanner.nextLine();
// Check if the strings are equal
if (str1.equals(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
scanner.close();
}
}
Output:-
3. Write a program to add two numbers.
Code:-
import java.util.Scanner;
public class thirdProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the first number
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
// Input the second number
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
// Add the two numbers
int sum = num1 + num2;
// Display the result
System.out.println("The sum of " + num1 + " and " + num2
+ " is: " + sum);
scanner.close();
}
}
Output:-
4. Write a program to find out how many digits in given number.
Code:-
import java.util.Scanner;
public class DigitCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Handle negative numbers by converting to positive
number = Math.abs(number);
// Count the digits
int count = 0;
if (number == 0) {
count = 1; // Special case for 0
} else {
while (number > 0) {
number /= 10;
count++;
}
}
// Display the result
System.out.println("The number of digits is: " + count);
scanner.close();
}
}
Output:-
5. Write a program to check given no. is palindrome or not and
Armstrong or not.
Code:-
import java.util.Scanner;
public class PalindromeAndArmstrong {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check if the number is a palindrome
if (isPalindrome(number)) {
System.out.println(number + " is a Palindrome.");
} else {
System.out.println(number + " is not a Palindrome.");
}
// Check if the number is an Armstrong number
if (isArmstrong(number)) {
System.out.println(number + " is an Armstrong
Number.");
} else {
System.out.println(number + " is not an Armstrong
Number.");
}
scanner.close();
}
// Method to check if a number is a Palindrome
public static boolean isPalindrome(int num) {
int original = num, reversed = 0;
// Reverse the number
while (num > 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
// Check if the original and reversed numbers are the same
return original == reversed;
}
// Method to check if a number is an Armstrong Number
public static boolean isArmstrong(int num) {
int original = num, sum = 0;
// Count the number of digits
int digits = String.valueOf(num).length();
// Calculate the sum of the power of each digit
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
// Check if the original number equals the sum
return original == sum;
}
}
Output:-
6. Write a program to display table of given number.
Code:-
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the number for which the table is to be displayed
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Display the multiplication table
System.out.println("Multiplication Table of " + number +
":");
for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i + " = " + (number *
i));
}
scanner.close();
}
}
Output:-
7. Write a program to find out factorial of given number with
and without recursion method.
Code:-
import java.util.Scanner;
public class FactorialProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Find factorial using iterative method
System.out.println("Factorial (Non-Recursive) of " + number
+ " is: " + factorialNonRecursive(number));
// Find factorial using recursive method
System.out.println("Factorial (Recursive) of " + number + "
is: " + factorialRecursive(number));
scanner.close();
}
// Non-Recursive method to calculate factorial
public static long factorialNonRecursive(int num) {
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
return factorial;
}
// Recursive method to calculate factorial
public static long factorialRecursive(int num) {
if (num == 0 || num == 1) {
return 1; // Base case
}
return num * factorialRecursive(num - 1); // Recursive call
}
}
Output:-
8. Write a program to find out how many words, characters and
sentence in given string.
Code:-
import java.util.Scanner;
public class StringAnalysis {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the string
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Calculate number of characters (excluding spaces)
int characterCount = input.replaceAll("\\s+", "").length();
// Calculate number of words
String[] words = input.trim().split("\\s+");
int wordCount = (input.trim().isEmpty()) ? 0 : words.length;
// Calculate number of sentences
String[] sentences = input.split("[.!?]");
int sentenceCount = (input.trim().isEmpty()) ? 0 :
sentences.length;
// Display results
System.out.println("Number of characters (excluding
spaces): " + characterCount);
System.out.println("Number of words: " + wordCount);
System.out.println("Number of sentences: " +
sentenceCount);
scanner.close();
}
}
Output:-
9. Write a program to check the given number is greater than or
less than 100.
Code:-
import java.util.Scanner;
public class CompareNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Compare the number with 100
if (number > 100) {
System.out.println(number + " is greater than 100.");
} else if (number < 100) {
System.out.println(number + " is less than 100.");
} else {
System.out.println(number + " is equal to 100.");
}
scanner.close();
}
}
Output:-
10. Write a program to find season of month.
Code:-
import java.util.Scanner;
public class SeasonOfMonth {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the month number
System.out.print("Enter the month number (1-12): ");
int month = scanner.nextInt();
// Determine the season based on the month
String season = "";
switch (month) {
case 1: case 2: case 12:
season = "Winter";
break;
case 3: case 4: case 5:
season = "Spring";
break;
case 6: case 7: case 8:
season = "Summer";
break;
case 9: case 10: case 11:
season = "Fall (Autumn)";
break;
default:
season = "Invalid month number! Please enter a
number between 1 and 12.";
}
// Output the result
System.out.println("The season for month " + month + " is:
" + season);
scanner.close();
}
}
Output:-
11. Write a program using Human as a class and derive its
members and methods.
Code:-
// Base class
class Human {
// Member variables
String name;
int age;
// Constructor
public Human(String name, int age) {
this.name = name;
this.age = age;
}
// Method to display human information
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Derived class Student
class Student extends Human {
// Additional member variable
String school;
// Constructor
public Student(String name, int age, String school) {
super(name, age); // Call to base class constructor
this.school = school;
}
// Method to display student information
@Override
public void displayInfo() {
super.displayInfo(); // Call to base class method
System.out.println("School: " + school);
}
}
// Derived class Employee
class Employee extends Human {
// Additional member variable
String company;
// Constructor
public Employee(String name, int age, String company) {
super(name, age); // Call to base class constructor
this.company = company;
}
// Method to display employee information
@Override
public void displayInfo() {
super.displayInfo(); // Call to base class method
System.out.println("Company: " + company);
}
}
public class HumanDemo {
public static void main(String[] args) {
// Creating objects of derived classes
Student student = new Student("John", 18, "Greenwood
High");
Employee employee = new Employee("Alice", 30,
"TechCorp");
// Displaying information of student and employee
System.out.println("Student Info:");
student.displayInfo();
System.out.println("\nEmployee Info:");
employee.displayInfo();
}
}
Output:-
12. Write a program to display following pattern_
*
**
***
****
*****
****
***
**
*
Code:-
public class Pattern {
public static void main(String[] args) {
int n = 5; // The middle width of the pattern (largest row)
// Upper part of the pattern (increasing stars)
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower part of the pattern (decreasing stars)
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output:-
13. Write a program to swap two variables using third variable
and swap two variables without using third variable.
Code:-
import java.util.Scanner;
public class SwapVariables {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input two numbers
System.out.print("Enter the first number: ");
int a = scanner.nextInt();
System.out.print("Enter the second number: ");
int b = scanner.nextInt();
// Swap using a third variable
System.out.println("\nSwapping using a third variable:");
int temp = a;
a = b;
b = temp;
System.out.println("After swapping - a: " + a + ", b: " + b);
// Re-input original values for next swap
System.out.print("\nEnter the first number again: ");
a = scanner.nextInt();
System.out.print("Enter the second number again: ");
b = scanner.nextInt();
// Swap without using a third variable
System.out.println("\nSwapping without using a third
variable:");
a = a + b; // Step 1: a = a + b
b = a - b; // Step 2: b = a - b (this gives the original value of
a)
a = a - b; // Step 3: a = a - b (this gives the original value of
b)
System.out.println("After swapping - a: " + a + ", b: " + b);
scanner.close();
}
}
Output:-