1
1. Area of Rectangle:
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of the rectangle: ");
double length = sc.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = sc.nextDouble();
// Calculate the area of the rectangle
double area = length * width;
System.out.println("The area of the rectangle is: " + area);
sc.close();
}
}
2. Addition of Two Numbers.
import java.util.Scanner;
public class Sum
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter First Number");
int a=sc.nextInt();
System.out.println("Enter Second Number");
int b=sc.nextInt();
int sum=a+b;
System.out.println("Addition is"+sum);
}
}
3. Factorial of Number
import java.util.Scanner;
public class SimpleFactorial
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to calculate its factorial: ");
int number = sc.nextInt();
// Calculate the factorial
int factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial *= i;
//factorial=factorial*i;
}
System.out.println("The factorial is: " + factorial);
sc.close();
}
2
}
4. Even or Odd
import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
// Check if the number is even or odd
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
sc.close();
}
}
5. Fibonacci Series:
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int a = 0, b = 1;
System.out.print("Fibonacci Series: " + a + ", " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
System.out.print(", " + next);
a = b;
b = next;
}
}
}
6. Reverse the number:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to reverse:");
int num = sc.nextInt();
int reversed = 0;
System.out.println("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;
3
}
System.out.println("Reversed Number: " + reversed);
}
}
7. Armstrong or not:
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
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 += Math.pow(digit, n);
originalNumber /= 10;
}
if (result == number) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}
8. Prime or Not:
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
boolean isPrime = true;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
}
4
9. Simple Calculator:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = sc.next().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
System.out.println("Addition is: " +result);
break;
case '-':
result = num1 - num2;
System.out.println("Subtraction is: " +result);
break;
case '*':
result = num1 * num2;
System.out.println("Multiplication is: " +result);
break;
case '/':
result = num1 / num2;
System.out.println("Devision is: " +result);
break;
default:
System.out.println("Invalid operator!");
return;
}
// System.out.println("Result: " + result);
}
}
10. Swap two Number:
import java.util.Scanner;
public class SwapNumbers {
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();
// Display numbers before swapping
System.out.println("Before swapping: ");
System.out.println("First number = " + num1);
System.out.println("Second number = " + num2);
// Swap the numbers
int temp = num1;
num1 = num2;
num2 = temp;
// Display numbers after swapping
5
System.out.println("After swapping: ");
System.out.println("First number = " + num1);
System.out.println("Second number = " + num2);
sc.close();
}
}
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--)
{
System.out.print(" ");
}
for (j=0; j<=i; j++ )
{
System.out.print("* ");
}
System.out.println();
} } }
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++)
{
System.out.print(number+ " ");
6
number++;
}
System.out.println();
} } }
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++)
{
System.out.print(k++ + " ");
}
System.out.println();
} } }
14. Pattern Program:
import java.util.*;
public class Pattern5
{
public static void main(String[] args)
{
int i, j, rows;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows you want to print: ");
rows = sc.nextInt();
for (i = 1; i <= rows; i++)
7
{
for (j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
} }}
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++)
{
System.out.print((char) (alphabet + j) + " ");
}
System.out.println();
}}}
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 < array.length; i++) {
sum += array[i];
}
// Print the sum of the elements
System.out.println("Sum of the array elements is: " + sum);
}
8
}
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 < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
// Print the maximum element
System.out.println("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 < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
}
// Print the sum of all elements
System.out.println("Sum of all elements in the 2D array is: " + sum);
}
}
Output
Sum of all elements in the 2D array is: 45
9
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][array.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
transpose[j][i] = array[i][j];
}
}
// Print the transposed array
System.out.println("Transpose of the 2D array is:");
for (int i = 0; i < transpose.length; i++) {
for (int j = 0; j < transpose[i].length; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
}
}
Output
Transpose of the 2D array is:
1 4 7
2 5 8
3 6 9
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[array1.length][array2[0].length];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2[0].length; j++) {
10
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
System.out.println("Product of the two 2D arrays is:");
for (int i = 0; i < product.length; i++) {
for (int j = 0; j < product[i].length; j++) {
System.out.print(product[i][j] + " ");
}
System.out.println();
}
}
}
Output
Product of the two 2D arrays is:
58 64
139 154

Simple Java Program for beginner with easy method.pdf

  • 1.
    1 1. Area ofRectangle: import java.util.Scanner; public class RectangleArea { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the length of the rectangle: "); double length = sc.nextDouble(); System.out.print("Enter the width of the rectangle: "); double width = sc.nextDouble(); // Calculate the area of the rectangle double area = length * width; System.out.println("The area of the rectangle is: " + area); sc.close(); } } 2. Addition of Two Numbers. import java.util.Scanner; public class Sum { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter First Number"); int a=sc.nextInt(); System.out.println("Enter Second Number"); int b=sc.nextInt(); int sum=a+b; System.out.println("Addition is"+sum); } } 3. Factorial of Number import java.util.Scanner; public class SimpleFactorial { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number to calculate its factorial: "); int number = sc.nextInt(); // Calculate the factorial int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; //factorial=factorial*i; } System.out.println("The factorial is: " + factorial); sc.close(); }
  • 2.
    2 } 4. Even orOdd import java.util.Scanner; public class EvenOddChecker { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); // Check if the number is even or odd if (number % 2 == 0) { System.out.println(number + " is even."); } else { System.out.println(number + " is odd."); } sc.close(); } } 5. Fibonacci Series: import java.util.Scanner; public class Fibonacci { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of terms: "); int n = sc.nextInt(); int a = 0, b = 1; System.out.print("Fibonacci Series: " + a + ", " + b); for (int i = 2; i < n; i++) { int next = a + b; System.out.print(", " + next); a = b; b = next; } } } 6. Reverse the number: import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number to reverse:"); int num = sc.nextInt(); int reversed = 0; System.out.println("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;
  • 3.
    3 } System.out.println("Reversed Number: "+ reversed); } } 7. Armstrong or not: import java.util.Scanner; public class ArmstrongNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); 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 += Math.pow(digit, n); originalNumber /= 10; } if (result == number) { System.out.println(number + " is an Armstrong number."); } else { System.out.println(number + " is not an Armstrong number."); } } } 8. Prime or Not: import java.util.Scanner; public class PrimeCheck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int number = sc.nextInt(); boolean isPrime = true; for (int i = 2; i <= number / 2; i++) { if (number % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(number + " is a prime number."); } else { System.out.println(number + " is not a prime number."); } } }
  • 4.
    4 9. Simple Calculator: importjava.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); double num1 = sc.nextDouble(); System.out.print("Enter second number: "); double num2 = sc.nextDouble(); System.out.print("Enter an operator (+, -, *, /): "); char operator = sc.next().charAt(0); double result; switch (operator) { case '+': result = num1 + num2; System.out.println("Addition is: " +result); break; case '-': result = num1 - num2; System.out.println("Subtraction is: " +result); break; case '*': result = num1 * num2; System.out.println("Multiplication is: " +result); break; case '/': result = num1 / num2; System.out.println("Devision is: " +result); break; default: System.out.println("Invalid operator!"); return; } // System.out.println("Result: " + result); } } 10. Swap two Number: import java.util.Scanner; public class SwapNumbers { 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(); // Display numbers before swapping System.out.println("Before swapping: "); System.out.println("First number = " + num1); System.out.println("Second number = " + num2); // Swap the numbers int temp = num1; num1 = num2; num2 = temp; // Display numbers after swapping
  • 5.
    5 System.out.println("After swapping: "); System.out.println("Firstnumber = " + num1); System.out.println("Second number = " + num2); sc.close(); } } 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--) { System.out.print(" "); } for (j=0; j<=i; j++ ) { System.out.print("* "); } System.out.println(); } } } 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++) { System.out.print(number+ " ");
  • 6.
    6 number++; } System.out.println(); } } } 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++) { System.out.print(k++ + " "); } System.out.println(); } } } 14. Pattern Program: import java.util.*; public class Pattern5 { public static void main(String[] args) { int i, j, rows; Scanner sc = new Scanner(System.in); System.out.print("Enter the number of rows you want to print: "); rows = sc.nextInt(); for (i = 1; i <= rows; i++)
  • 7.
    7 { for (j =1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } }} 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++) { System.out.print((char) (alphabet + j) + " "); } System.out.println(); }}} 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 < array.length; i++) { sum += array[i]; } // Print the sum of the elements System.out.println("Sum of the array elements is: " + sum); }
  • 8.
    8 } Output Sum of thearray 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 < array.length; i++) { if (array[i] > max) { max = array[i]; } } // Print the maximum element System.out.println("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 < array.length; i++) { for (int j = 0; j < array[i].length; j++) { sum += array[i][j]; } } // Print the sum of all elements System.out.println("Sum of all elements in the 2D array is: " + sum); } } Output Sum of all elements in the 2D array is: 45
  • 9.
    9 19. Transpose ofa 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][array.length]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { transpose[j][i] = array[i][j]; } } // Print the transposed array System.out.println("Transpose of the 2D array is:"); for (int i = 0; i < transpose.length; i++) { for (int j = 0; j < transpose[i].length; j++) { System.out.print(transpose[i][j] + " "); } System.out.println(); } } } Output Transpose of the 2D array is: 1 4 7 2 5 8 3 6 9 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[array1.length][array2[0].length]; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array2[0].length; j++) {
  • 10.
    10 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 System.out.println("Product of the two 2D arrays is:"); for (int i = 0; i < product.length; i++) { for (int j = 0; j < product[i].length; j++) { System.out.print(product[i][j] + " "); } System.out.println(); } } } Output Product of the two 2D arrays is: 58 64 139 154