1.
Write a program to check given character is vowel or
consonant.
Code:-
import [Link];
public class VowelOrConsonant {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input from the user
[Link]("Enter a character: ");
char ch = [Link]().charAt(0);
// Check if the input is a single alphabetic character
if ([Link](ch)) {
// Convert to lowercase for case-insensitive comparison
ch = [Link](ch);
// Check if the character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
[Link](ch + " is a vowel.");
} else {
[Link](ch + " is a consonant.");
}
} else {
[Link]("Invalid input! Please enter a single
alphabetic character.");
}
[Link]();
}
}
Output:-
2. Write a program to check if two strings are equals or not.
Code:-
import [Link];
public class StringEqualityCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the first string
[Link]("Enter the first string: ");
String str1 = [Link]();
// Input the second string
[Link]("Enter the second string: ");
String str2 = [Link]();
// Check if the strings are equal
if ([Link](str2)) {
[Link]("The strings are equal.");
} else {
[Link]("The strings are not equal.");
}
[Link]();
}
}
Output:-
3. Write a program to add two numbers.
Code:-
import [Link];
public class thirdProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the first number
[Link]("Enter the first number: ");
int num1 = [Link]();
// Input the second number
[Link]("Enter the second number: ");
int num2 = [Link]();
// Add the two numbers
int sum = num1 + num2;
// Display the result
[Link]("The sum of " + num1 + " and " + num2
+ " is: " + sum);
[Link]();
}
}
Output:-
4. Write a program to find out how many digits in given number.
Code:-
import [Link];
public class DigitCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input a number
[Link]("Enter a number: ");
int number = [Link]();
// Handle negative numbers by converting to positive
number = [Link](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
[Link]("The number of digits is: " + count);
[Link]();
}
}
Output:-
5. Write a program to check given no. is palindrome or not and
Armstrong or not.
Code:-
import [Link];
public class PalindromeAndArmstrong {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input a number
[Link]("Enter a number: ");
int number = [Link]();
// Check if the number is a palindrome
if (isPalindrome(number)) {
[Link](number + " is a Palindrome.");
} else {
[Link](number + " is not a Palindrome.");
}
// Check if the number is an Armstrong number
if (isArmstrong(number)) {
[Link](number + " is an Armstrong
Number.");
} else {
[Link](number + " is not an Armstrong
Number.");
}
[Link]();
}
// 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 = [Link](num).length();
// Calculate the sum of the power of each digit
while (num > 0) {
int digit = num % 10;
sum += [Link](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 [Link];
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the number for which the table is to be displayed
[Link]("Enter a number: ");
int number = [Link]();
// Display the multiplication table
[Link]("Multiplication Table of " + number +
":");
for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number *
i));
}
[Link]();
}
}
Output:-
7. Write a program to find out factorial of given number with
and without recursion method.
Code:-
import [Link];
public class FactorialProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the number
[Link]("Enter a number: ");
int number = [Link]();
// Find factorial using iterative method
[Link]("Factorial (Non-Recursive) of " + number
+ " is: " + factorialNonRecursive(number));
// Find factorial using recursive method
[Link]("Factorial (Recursive) of " + number + "
is: " + factorialRecursive(number));
[Link]();
}
// 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 [Link];
public class StringAnalysis {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the string
[Link]("Enter a string: ");
String input = [Link]();
// Calculate number of characters (excluding spaces)
int characterCount = [Link]("\\s+", "").length();
// Calculate number of words
String[] words = [Link]().split("\\s+");
int wordCount = ([Link]().isEmpty()) ? 0 : [Link];
// Calculate number of sentences
String[] sentences = [Link]("[.!?]");
int sentenceCount = ([Link]().isEmpty()) ? 0 :
[Link];
// Display results
[Link]("Number of characters (excluding
spaces): " + characterCount);
[Link]("Number of words: " + wordCount);
[Link]("Number of sentences: " +
sentenceCount);
[Link]();
}
}
Output:-
9. Write a program to check the given number is greater than or
less than 100.
Code:-
import [Link];
public class CompareNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the number
[Link]("Enter a number: ");
int number = [Link]();
// Compare the number with 100
if (number > 100) {
[Link](number + " is greater than 100.");
} else if (number < 100) {
[Link](number + " is less than 100.");
} else {
[Link](number + " is equal to 100.");
}
[Link]();
}
}
Output:-
10. Write a program to find season of month.
Code:-
import [Link];
public class SeasonOfMonth {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the month number
[Link]("Enter the month number (1-12): ");
int month = [Link]();
// 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
[Link]("The season for month " + month + " is:
" + season);
[Link]();
}
}
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) {
[Link] = name;
[Link] = age;
}
// Method to display human information
public void displayInfo() {
[Link]("Name: " + name);
[Link]("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
[Link] = school;
}
// Method to display student information
@Override
public void displayInfo() {
[Link](); // Call to base class method
[Link]("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
[Link] = company;
}
// Method to display employee information
@Override
public void displayInfo() {
[Link](); // Call to base class method
[Link]("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
[Link]("Student Info:");
[Link]();
[Link]("\nEmployee Info:");
[Link]();
}
}
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++) {
[Link]("*");
}
[Link]();
}
// Lower part of the pattern (decreasing stars)
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Output:-
13. Write a program to swap two variables using third variable
and swap two variables without using third variable.
Code:-
import [Link];
public class SwapVariables {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input two numbers
[Link]("Enter the first number: ");
int a = [Link]();
[Link]("Enter the second number: ");
int b = [Link]();
// Swap using a third variable
[Link]("\nSwapping using a third variable:");
int temp = a;
a = b;
b = temp;
[Link]("After swapping - a: " + a + ", b: " + b);
// Re-input original values for next swap
[Link]("\nEnter the first number again: ");
a = [Link]();
[Link]("Enter the second number again: ");
b = [Link]();
// Swap without using a third variable
[Link]("\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)
[Link]("After swapping - a: " + a + ", b: " + b);
[Link]();
}
}
Output:-