1.
Area of Rectangle:
import [Link];
public class RectangleArea {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the length of the rectangle: ");
double length = [Link]();
[Link]("Enter the width of the rectangle: ");
double width = [Link]();
// Calculate the area of the rectangle
double area = length * width;
[Link]("The area of the rectangle is: " + area);
[Link]();
}
}
2. Addition of Two Numbers.
import [Link];
public class Sum
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter First Number");
int a=[Link]();
[Link]("Enter Second Number");
int b=[Link]();
int sum=a+b;
[Link]("Addition is"+sum);
}
}
3. Factorial of Number
import [Link];
public class SimpleFactorial
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number to calculate its factorial: ");
int number = [Link]();
// Calculate the factorial
int factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial *= i;
//factorial=factorial*i;
}
[Link]("The factorial is: " + factorial);
[Link]();
}
1
}
4. Even or Odd
import [Link];
public class EvenOddChecker {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
// Check if the number is even or odd
if (number % 2 == 0) {
[Link](number + " is even.");
} else {
[Link](number + " is odd.");
}
[Link]();
}
}
5. Fibonacci Series:
import [Link];
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of terms: ");
int n = [Link]();
int a = 0, b = 1;
[Link]("Fibonacci Series: " + a + ", " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
[Link](", " + next);
a = b;
b = next;
}
}
}
6. Reverse the number:
import [Link];
class Main {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the number to reverse:");
int num = [Link]();
int reversed = 0;
[Link]("Original Number: " + num);
// run loop until num becomes 0
while(num != 0)
{
// get last digit from num
int digit = num % 10;
reversed = reversed * 10 + digit;
// remove the last digit from num
num /= 10;
2
}
[Link]("Reversed Number: " + reversed);
}
}
7. Armstrong or not:
import [Link];
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int originalNumber = number;
int result = 0;
int n = 0;
// Count the number of digits
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}
originalNumber = number;
// Calculate the sum of the nth power of its digits
while (originalNumber != 0) {
int digit = originalNumber % 10;
result += [Link](digit, n);
originalNumber /= 10;
}
if (result == number) {
[Link](number + " is an Armstrong number.");
} else {
[Link](number + " is not an Armstrong number.");
}
}
}
8. Prime or Not:
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
boolean isPrime = true;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
[Link](number + " is a prime number.");
} else {
[Link](number + " is not a prime number.");
}
}
}
3
9. Simple Calculator:
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
[Link]("Enter an operator (+, -, *, /): ");
char operator = [Link]().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
[Link]("Addition is: " +result);
break;
case '-':
result = num1 - num2;
[Link]("Subtraction is: " +result);
break;
case '*':
result = num1 * num2;
[Link]("Multiplication is: " +result);
break;
case '/':
result = num1 / num2;
[Link]("Devision is: " +result);
break;
default:
[Link]("Invalid operator!");
return;
}
// [Link]("Result: " + result);
}
}
10. Swap two Number:
import [Link];
public class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the first number: ");
int num1 = [Link]();
[Link]("Enter the second number: ");
int num2 = [Link]();
// Display numbers before swapping
[Link]("Before swapping: ");
[Link]("First number = " + num1);
[Link]("Second number = " + num2);
// Swap the numbers
int temp = num1;
num1 = num2;
num2 = temp;
// Display numbers after swapping
4
[Link]("After swapping: ");
[Link]("First number = " + num1);
[Link]("Second number = " + num2);
[Link]();
}
}
11. Pyramid Pattern of Star
public class PyramidPattern
{
public static void main(String args[])
{
int i, j, row = 6;
for (i=0; i<row; i++)
{
for (j=row-i; j>1; j--)
{
[Link](" ");
}
for (j=0; j<=i; j++ )
{
[Link]("* ");
}
[Link]();
} } }
12. Pattern Program:
public class Pattern1
{
public static void main(String args[])
{
int i, j,number, n=7;
for(i=0; i<n; i++)
{
number=1;
for(j=0; j<=i; j++)
{
[Link](number+ " ");
5
number++;
}
[Link]();
} } }
13. Pattern Program:
public class Pattern2
{
public static void main(String[] args)
{
int i, j, k = 1;
for (i = 1; i <= 7; i++)
{
for (j = 1; j< i + 1; j++)
{
[Link](k++ + " ");
}
[Link]();
}} }
14. Pattern Program:
import [Link].*;
public class Pattern5
{
public static void main(String[] args)
{
int i, j, rows;
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows you want to print: ");
rows = [Link]();
for (i = 1; i <= rows; i++)
6
{
for (j = 1; j <= i; j++)
{
[Link](i+" ");
}
[Link]();
} }}
15. Pattern Program:
public class RightAlphabaticPattern
{
public static void main(String[] args)
{
int alphabet = 65; //ASCII value of capital A is 65
for (int i = 0; i <= 8; i++)
{
for (int j = 0; j <= i; j++)
{
[Link]((char) (alphabet + j) + " ");
}
[Link]();
}}}
16. Array program
Find the Sum of Array Elements
public class Main {
public static void main(String[] args) {
// Declare and initialize an array of 5 integers
int[] array = {10, 20, 30, 40, 50};
// Calculate the sum of the elements of the array
int sum = 0;
for (int i = 0; i < [Link]; i++) {
sum += array[i];
}
// Print the sum of the elements
[Link]("Sum of the array elements is: " + sum);
}
7
}
Output
Sum of the array elements is: 150
17. Find the Maximum Element in an Array
public class Main {
public static void main(String[] args) {
// Declare and initialize an array of 5 integers
int[] array = {10, 20, 30, 40, 50};
// Find the maximum element in the array
int max = array[0];
for (int i = 1; i < [Link]; i++) {
if (array[i] > max) {
max = array[i];
}
}
// Print the maximum element
[Link]("Maximum element in the array is: " + max);
}
}
Output
Maximum element in the array is: 50
X-X-X-X-X-X-X-X-X-X-X
18. Sum of All Elements in a 2D Array
public class Main {
public static void main(String[] args) {
// Declare and initialize a 2D array
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Calculate the sum of all elements in the 2D array
int sum = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
}
[Link]("Sum of all elements in the 2D array is: " + sum);
}
}
Output
Sum of all elements in the 2D array is: 45
8
19. Transpose of a 2D Array
public class Main {
public static void main(String[] args) {
// Declare and initialize a 2D array
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Transpose the 2D array
int[][] transpose = new int[array[0].length][[Link]];
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < array[i].length; j++) {
transpose[j][i] = array[i][j];
}
}
// Print the transposed array
[Link]("Transpose of the 2D array is:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < transpose[i].length; j++) {
[Link](transpose[i][j] + " ");
}
[Link]();
}
}
}
Output
Transpose of the 2D array is:
147
258
369
20. Multiplication of Two 2D Arrays
public class Main {
public static void main(String[] args) {
// Declare and initialize two 2D arrays
int[][] array1 = {
{1, 2, 3},
{4, 5, 6}
};
int[][] array2 = {
{7, 8},
{9, 10},
{11, 12}
};
// Multiply the two 2D arrays
int[][] product = new int[[Link]][array2[0].length];
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < array2[0].length; j++) {
9
for (int k = 0; k < array1[0].length; k++) {
product[i][j] += array1[i][k] * array2[k][j];
}
}
}
// Print the product of the two arrays
[Link]("Product of the two 2D arrays is:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < product[i].length; j++) {
[Link](product[i][j] + " ");
}
[Link]();
}
}
}
Output
Product of the two 2D arrays is:
58 64
139 154
21. Java Program To print Day of Week.
22.
import [Link];
public class DayOfWeek {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Prompt user for input
[Link]("Enter a number (1-7) for the day of the week: ");
int day = [Link]();
// Determine the day of the week using switch case
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
10
dayName = "Sunday";
break;
default:
dayName = "Invalid input! Please enter a number between 1 and 7.";
}
// Output the result
[Link]("The day of the week is: " + dayName);
// Close the scanner
[Link]();
}
}
23. Write a Java program that checks whether a given character is a vowel or
consonant using a switch statement.
import [Link];
public class VowelOrConsonant {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a letter: ");
char letter = [Link]().toLowerCase().charAt(0);
switch (letter) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
[Link](letter + " is a vowel.");
break;
default:
if ([Link](letter)) {
[Link](letter + " is a consonant.");
} else {
[Link]("Invalid input, not a letter.");
}
}
[Link]();
}
}
24. Write a Java program to determine a student's grade based on marks using a
switch statement. The program should output a grade based on ranges of
marks.
import [Link];
public class GradeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your marks (0-100): ");
int marks = [Link]();
char grade;
switch (marks / 10)
{
case 10:
case 9:
grade = 'A';
10
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5:
grade = 'E';
break;
default:
grade = 'F';
}
[Link]("Your grade is: " + grade);
[Link]();
}
}
25. Write a Java program that takes a number between 1 and 12 from the user
and prints the corresponding month name using a switch statement.
import [Link];
public class MonthFinder {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter month number (1-12): ");
int month = [Link]();
String monthName;
switch (month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
10
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
default:
monthName = "Invalid month number!";
}
[Link]("The month is: " + monthName);
[Link]();
}
}
26. Write a Java program that calculates the sum of natural numbers from 1 to a
user-defined value using a while loop.
import [Link];
public class SumOfNum {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the upper limit: ");
int n = [Link]();
int sum = 0;
int i = 1;
while (i <= n) {
sum += i; // add current number to sum
i++; // increment the counter
}
[Link]("Sum of natural numbers from 1 to " + n + " is: " + sum);
[Link]();
}
}
27. Write a Java program that reverses a given number using a while loop.
import [Link];
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
[Link]("Reversed number: " + reverse);
[Link]();
}
}
10