Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

12 Feb 2024

JAVA PROGRAM - ADDITION OF TWO MATRICES


 ## Write a Java Program to ADD TWO MATRICES





public class MatrixAddition { 
                public static void main(String[] args) { 

                                    int[][] firstMatrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; 
                                    int[][] secondMatrix = { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }; 

                                    // Creating a matrix to store the result 
                                    int[][] result = new int[firstMatrix.length][firstMatrix[0].length]; 

                                    // Adding corresponding elements of two matrices 
                                    for (int i = 0; i < firstMatrix.length; i++) { 
                                                    for (int j = 0; j < firstMatrix[i].length; j++) { 
                                                                    result[i][j] = firstMatrix[i][j] + secondMatrix[i][j]; 
                                                    
                                    

                                    // Displaying the result matrix 
                                    System.out.println("Result of Matrix Addition:"); 

                                    for (int i = 0; i < result.length; i++) { 
                                                            for (int j = 0; j < result[i].length; j++) { 
                                                                                System.out.print(result[i][j] + " "); 
                                                            
                                   System.out.println(); 
                                  
                
}

10 Feb 2024

JAVA PROGRAM - RIGHT TRIANGLE STAR PATTERN


 ## Write a Java Program to print the Right Triangle Star Pattern



                                                                        *
                                                                        *  *
                                                                        *  *  *
                                                                        *  *  *  *
                                                                        *  *  *  *  *



 public class RightTriangleStarPattern { 

                        public static void main(String[] args) { 
                                            int rows = 5; 

                                            for (int i = 1; i <= rows; i++) { 
                                                            for (int j = 1; j <= i; j++) { 
                                                                                System.out.print("* "); 
                                                            
                                            System.out.println(); 
                                            
                        } 
}

JAVA PROGRAM - INVERTED PYRAMID STAR PATTERN


 ## Write a Java Program to Print an INVERTED PYRAMID STAR PATTERN


                                                        *  *  *  *  *  *  *  *  * 
                                                            *  *  *  *  *  *  * 
                                                                *  *  *  *  *
                                                                    *  *  *
                                                                        *


public class InvertedPyramidStarPattern { 

                        public static void main(String[] args) { 
                                            int rows = 5; 
                
                                            for (int i = rows; i >= 1; i--) { 
                                                                for (int j = 1; j <= rows - i; j++) { 
                                                                                System.out.print("  "); 
                                                                
                                                                
                                                                for (int k = 1; k <= 2 * i - 1; k++) { 
                                                                                System.out.print("* "); 
                                                                
                                                                System.out.println(); 
                                            
                        
}

JAVA PROGRAM - PYRAMID STAR PATTERN


## Write a Java program to print the PYRAMID STAR PATTERN? 



                                                                    *
                                                                *  *  *
                                                            *  *  *  *  *
                                                        *  *  *  *  *  *  *
                                                    *  *  *  *  *  *  *  *  *


public class PyramidStarPattern { 

                        public static void main(String[] args) { 
                                        int rows = 5; 

                                        for (int i = 1; i <= rows; i++) { 
                                                            for (int j = 1; j <= rows - i; j++) { 
                                                                            System.out.print("  "); 
                                                            
                                                            for (int k = 1; k <= 2 * i - 1; k++) { 
                                                                            System.out.print("* "); 
                                                            
                                                            System.out.println(); 
                                        
                      
}

JAVA PROGRAM - LEAP YEAR OR NOT


 ## Write a Java Program to check if the year is LEAP YEAR or NOT



 public class CheckLeapYear { 

                    public static void main(String[] args) { 
                                        int year = 2024; 

                                        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                                                             System.out.println("The year " + year + " is a leap year."); 
                                        
                                        else { 
                                                             System.out.println("The year " year + " is not a leap year."); 
                                        
                    
}

JAVA PROGRAM - LARGEST VALUE AMONG THREE NUMBERS


 ## Write a Java program to find the largest value among three variables



public class LargestAmongThreeNumbers { 

            public static void main(String[] args) { 
                            int num1 = 20; 
                            int num2 = 50; 
                            int num3 = 55; 

                            int largest = num1; 

                            if (num2 > largest) { 
                                            largest = num2; 
                            
                            if (num3 > largest) { 
                                            largest = num3; 
                            
        
                            System.out.println("The largest number among " + num1 + ", " + num2 + ", and " + num3 + " is: " + largest); 
            
}

JAVA PROGRAM - LARGEST VALUE AMONG TWO NUMBERS


## Write a Java Program to find the largest value among two numbers 



public class LargestOfTwoNumbers { 

          public static void main(String[] args) { 
                    int num1 = 10; 
                    int num2 = 20; 

                    if (num1 > num2) { 
                                    System.out.println("The largest number is: " + num1); 
                    
                    else { 
                                    System.out.println("The largest number is: " + num2); 
                    }
        
}

8 Feb 2024

JAVA - SUBTRACT TWO NUMBERS


 Write a JAVA program to subtract two numbers?




public class SubtractTwoNumbers { 

                public static void main(String[] args) {
                                int num1 = 10; int num2 = 5; 
                                int difference = num1 - num2; 

                                System.out.println("Difference of " + num1 + " and " + num2 + " is: " + difference);
                 

}

7 Feb 2024

JAVA - ADD TWO NUMBERS


 ## Write a Java program to add two numbers




public class AddTwoNumbers { 

    public static void main(String[] args) {
                
                int num1 = 5; int num2 = 10; 
                int sum = num1 + num2; 

                System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum); 
                
}

16 Nov 2022

Java Program to print an integer entered by user

## Java program to print an integer ##

import java.util.Scanner;

public class PrintInteger {

    public static void main (String[] args){
    
        Scanner reader = new Scanner (System.in);    
        System.out.println ("Input a Number: ");

        int number = reader.nextInt ();
        System.out.println ("Number you entered: " + number);
    }
}


===OUTPUT===

Input a Number: 4
Number you entered: 4

7 Jul 2014

Java Program #2

Q) Write a Program to print STAR Pattern.
   *
   **
   ***
   ****
   *****

Sol)

class Stars {
  public static void main(String[] args) {
    int row, numberOfStars;

    for (row = 1; row <= 5; row++) {
      for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
        System.out.print("*");
      }
      System.out.println(); // Go to next line
    }
  }
}

Java Program #1

Q) Write a Program to print " Hello World ".


Sol)

   public class HelloWorld {

    public static void main(String[] args) {
        
        System.out.println("Hello World");
        
    }