C program to print rectangular star pattern

C program to print rectangular star pattern
  • Write a program in C to print rectangular star (*) pattern of n rows and m columns using for loop.
  • Write a C program to print a rectangle pattern of star (*) character using loops.

For a rectangular star pattern of 5 rows and 9 columns. Program’s output should be:

Rectangle_Star_Pattern_Program

Required Knowledge

Algorithm to print rectangular star pattern using loop

  • Take the number of rows(N) and columns(M) of rectangle as input from user using scanf function.
  • We will use two for loops to print rectangular star pattern.
  • Outer for loop will iterate N times. In each iteration, it will print one row of pattern.
  • Inner for loop will iterate M times.In one iteration, it will print one star (*) characters in a row.

Here is the matrix representation of the rectangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.

Rectangle_Star_Pattern_Matrix

C program to print rectangular star pattern

C program to print rectangular star pattern

#include<stdio.h>
 
int main(){
    int rows, cols , i, j;
     
    printf("Enter rows and columns of rectangle\n");
    scanf("%d %d", &rows, &cols);
     
    /* Row iterator for loop */
    for(i = 0; i < rows; i++){
     /* Column iterator for loop */
        for(j = 0; j < cols; j++){
           printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output

Enter rows and columns of rectangle
3 9
*********
*********
*********

C program to print hollow square star pattern

  • Write a program in C to print hollow square star (*) pattern of n rows using for loop.
  • Write a C program to print outline or boundary of a square pattern by star (*) character using loop.

For a hollow square star pattern of side 5 stars. Program’s output should be:

Hollow_Square_Star_Pattern

Required Knowledge

Algorithm to print hollow square star pattern using loop
The algorithm of this pattern is similar to square star pattern except here we have to print stars of first and last rows and columns. At non-boundary position, we will only print a space character.

  • Take the number of stars in each side of square as input from user using scanf function. Let it be N.
  • We will use two for loops to print square star pattern.
    • Outer for loop will iterate N times. In one iteration, it will print one row of pattern.
    • Inside inner for loop, we will add a if statement check to find the boundary positions of the pattern and print star (*) accordingly.

Here is the matrix representation of the hollow square star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Hollow_Square_Star_Pattern

C program to print hollow square star pattern

C program to print hollow square star pattern

#include<stdio.h>
 
int main(){
    int side, i, j;
     
    printf("Enter side of square\n");
    scanf("%d", &side);
     
    /* Row iterator for loop */
    for(i = 0; i < side; i++){
     /* Column iterator for loop */
        for(j = 0; j < side; j++){
            /* Check if currely position is a boundary position */
            if(i==0 || i==side-1 || j==0 || j==side-1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Output

Enter side of square
5
*****
*   *
*   *
*   *
*****

C program to print inverted right triangle star pattern

C program to print inverted right triangle star pattern
  • Write a program in C to print inverted right triangle star (*) pattern of n rows using for loop.
  • Write a C program to print a inverted right angles triangle pattern of star (*) character using loops.

For a inverted right triangle star pattern of 7 rows. Program’s output should be:

Inverted_Right_Triangle_Star_Pattern

Required Knowledge

Algorithm to print inverted right triangle star pattern using loop

If you look closely, this pattern is the vertically inverted pattern of right triangle star pattern. As the row number increases from top to bottom, number of stars in a row decreases.
NOTE: The index of rows and columns starts from 0.

  • Take the number of rows(N) of inverted right triangle as input from user using scanf function.
  • Number of stars in Kth row is equal to (N – K + 1). In the pattern given above, N = 7. Hence, 1st row contains 7 star, 2nd row contains 6 stars, 3rd row contains 5 stars.
  • We will use two for loops to print inverted right triangle star pattern.
    • For a inverted right triangle star pattern of N rows, outer for loop will iterate N time(from i = 0 to N-1). Each iteration of outer loop will print one row of the pattern.
    • For jth row of inverted right triangle pattern, inner loop will iterate N-j times(from j = 0 to N-j). Each iteration of inner loop will print one star character.

Here is the matrix representation of the inverted right triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Inverted_Right_Triangle_Star_Pattern_Matrix

C program to print inverted right triangle star pattern

C program to print inverted right triangle star pattern

#include<stdio.h>
 
int main() {
    int i,j,rows;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for(i = 0; i < rows; i++) {
        /* Prints one row of triangle */
        for(j = 0; j < rows - i; j++) {
            printf("* ");
        }
        /* move to next row */
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows
7
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*

C program to print mirrored right triangle star pattern

C program to print mirrored right triangle star pattern
  • Write a program in C to print mirrored right angled triangle star (*) pattern using for loop.
  • Write a C program to print a mirrored right angles triangle pattern of star (*) character of n rows.

For a mirrored right triangle star pattern of 7 rows. Program’s output should be:

Mirrored_Triangle_Star_Pattern

Required Knowledge

Algorithm to print mirrired right triangle star pattern using loop

C program to print mirrored right triangle star pattern is similar to the right triangle star pattern program. Once you understand how to print right triangle pattern then printing it’s mirrored pattern will be an easy task.

  • Take the number of rows(N) of mirrored right triangle as input from user using scanf function.
  • In any row, the sum of spaces and stars are equal to N. Number of stars increases by one and number of spaces before stars decreases by 1 in consecutive rows.
  • In any row R, we will first print N-R-1 space characters then R+1 star characters.

Here is the matrix representation of the inverted triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Inverted_triangle_Star_Pattern

C program to print mirrored right triangle star pattern

C program to print mirrored right triangle star pattern

#include <stdio.h>
 
int main() {
    int i, j, rows;
 
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for(i = 0; i < rows; i++){
        /* for j th row, first print rows-r spaces then stars */
        for(j = 0; j < rows; j++){
            if(j < rows-i-1){
                printf(" ");
            } else {
                printf("*");
            }
        }
        /* move to next row */
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows
6
     *
    **
   ***
  ****
 *****
******

Here is the C program to print inverted right triangle star pattern using one for loop

Here is the C program to print inverted right triangle star pattern using one for loop

#include<stdio.h>
  
int main(){
    char *str="*******************";
    int i,j, rows;
     
    printf("Enter the number of rows\n");
    scanf("%d", &rows); 
     
    for(i = 0; i < rows; i++){
       printf("%*.*s\n", rows, i+1, str);
    }
     
    return 0;
}
</stdio.h>

Output

Enter the number of rows
5
    *
   **
  ***
 ****
*****

C program to print reversed mirrored right triangle star pattern

C program to print reversed mirrored right triangle star pattern
  • Write a program in C to print reversed inverted right triangle star (*) pattern of n rows using for loop.

For a reversed mirrored right triangle star pattern of 7 rows. Program’s output should be:

Inverted_Star_Triangle_Pattern_2

Required Knowledge

Algorithm to print reversed mirrored right triangle star pattern using loop
If you look closely, this pattern is similar to inverted right triangle star pattern. For Rth row, we have to print R space characters before stars.

  • Take the number of rows(N) of reversed inverted right triangle as input from user using scanf function.
  • Each row(R) contains N characters, R space characters followed by N-R star (*) character.
  • We will use two for loops. Outer for loop will print one row in one iteration.
  • One iteration of inner loop will first print R space characters followed by N-R star (*) character

Here is the matrix representation of the mirrored triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Inverted_triangle_star_pattern2

C program to print reversed mirrired right triangle star pattern

C program to print reversed mirrored right triangle star pattern

#include <stdio.h>
 
int main() {
    int i, j, rows;
 
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for (i = 0; i < rows; i++) {
        for (j = 0; j < rows; j++) {
            if (j < i) {
                printf(" ");
            } else {
                printf("*");
            }
        }
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows
6
******
 *****
  ****
   ***
    **
     *

C program to print pyramid star pattern or equilateral triangle star pattern

C program to print pyramid star pattern or equilateral triangle star pattern
  • Write a program in C to print pyramid star pattern using for loop.
  • Write a C program to print an equilateral triangle star pattern of n rows using loops.

For an equilateral triangle pyramid star pattern of 5 rows. Program’s output should be:

Pyramid_Star_Pattern

Required Knowledge

Algorithm to print pyramid star pattern using loop
In this program, we are printing a pyramid star pattern in which ith row contains (2*i + 1) space separated stars. The index of rows and columns starts from 0.

  • We first take the number of rows(R) in the pattern as input from user using scanf function.
  • One iteration of outer for loop will print a row of pyramid(from i = 0 to R – 1).
  • For jth row of pyramid, the inner for loop first prints (R – i – 1) spaces for every line and then nested while loop prints (2*j + 1) stars.

Here is the matrix representation of the pyramid star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Pyramid_Star_Pattern

C program to print pyramid star pattern

C program to print pyramid star pattern or equilateral triangle star pattern

#include<stdio.h>
 
int main() {
    int i, j, rows, star = 0;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    /* printing one row in every iteration */
    for (i = 0; i < rows; i++) {
        /* Printing spaces */
        for (j = 0; j <= (rows - i - 1); j++) {
            printf(" ");
        }
        /* Printing stars */
        while (star != (2 * i + 1)) {
            printf("*");
            star++;;
        }
        star = 0;
        /* move to next row */
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows
6
     *
    ***
   *****
  *******
 *********
***********

C program to print rhombus star pattern

C program to print rhombus star pattern
  • Write a C program to print rhombus star pattern using for loop.

For a rhombus star pattern of 6 rows. Program’s output should be:

Rhombus_Star_Pattern

Required Knowledge

Algorithm to print rhombus star pattern using loop

  • We first take the number of rows(N) as input from user using scanf function.
  • The index of rows and columns will start from 0.
  • Each row of rhombus star pattern contains N star characters.
  • Each iteration of outer for loop(from i = 0 to N-1) will print one row of pattern at a time.
  • Each jth row, contains j space characters followed by N star characters.
  • First inner for loop prints space characters whereas second inner for loop prints star characters.

Here is the matrix representation of the rhombus star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Rhombus_Star_Pattern_Program

 program to print rhombus star pattern

C program to print rhombus star pattern

#include <stdio.h>
 
int main() {
    int i, j, rows;
 
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for (i = 0; i < rows; i++) {
        /* Print spaces before stars in a row */
        for (j = 0; j < i; j++) {
            printf(" ");
        }
        /* Print rows stars after spaces in a row */
        for (j = 0; j < rows; j++) {
            printf("*");
        }
        /* jump to next row */
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows
6
******
 ******
  ******
   ******
    ******
     ******

Mechanical Engineering Notes, Course Details, Duration, Eligibility, Admission Procedure, Fee Structure, and Career Prospect

Mechanical Engineering Notes

Mechanical Engineering Notes PDF Free Download: Looking for Btech ME Year-wise Notes & Study Materials that can support your preparation? There are numerous lecture notes & books of BTech Geeks which help you in better preparation. Mechanical Engineering Course structure & complete details are covered here along with the year-wise Bachelor of Technology ME Notes Pdf. Check out the entire article of Mechanical Engineering Notes & choose the appropriate study resource for effective preparation. Also, you can find other courses Btech Notes on our site. So, take a look at this page thoroughly and click on the ME Lecture Notes PDF links available over here to download freely.

Overview of BTech Mechanical Engineering Course Details & Study Materials

Here are the links to the ME Course details overview where you have to click on the respective link & directly jump into the matter that you required to know about Btech Mechanical Engineering(ME).

About Mechanical Engineering – BTech ME Course

Mechanical Engineering is an undergraduate programme that comes under the field of technology. The course is informative and broader related to the planning, designing, and implementation of technology that is used in industries. The course is divided into eight semesters. You can apply for this course after completing 10+2 from a recognized board with a minimum of 45 percent marks. The admission is provided based on the entrance examination. This is a popular field of technology that can highly-demanding jobs.

Mechanical engineering is a popular branch of engineering. It is a combination of the principles of mathematics, engineering physics, and materials science. It is related to the study of designing, analyzing, maintaining, and manufacturing mechanical systems.

In mechanical engineering courses, students get knowledge about electric motors, designing automobiles, aircraft, and other heavy vehicles. This article covers all the information about the mechanical engineering course. This course comes under one of the oldest and popular programmes named Bachelor of Technology.

If you have an interest in technology, science, and machines; this course is suitable for you. This course will help you to understand the working and development of machines. It is one of the traditional technology courses that has played a crucial role in the industrialization of the human race.

It is a full-time course consisting of four years that is offered in many institutions of India including the IITs and NITs. This course is related to the production and design of machines that help in reducing human labor.

Course Highlights

Course Name Mechanical Engineering
Course Duration Diploma- Three Years

UG- Fours Years

PG- Two Years

Eligibility Criteria Diploma- Class 10 with 45 percent aggregate

UG- Class 10+2 with 45 percent aggregate

PG-  B.Tech in Mechanical Engineering

Placement Opportunities Manufacturing, Industrial, Maintenance, and Automobile.
Top Recruiter Escorts, Tecumseh, Hindustan Aeronautics Limited (HAL), BHEL, NTPC, NHPC, JCB, Hero Honda, Railways, and Armed Forces.
Average Income 3 lacs to 15 lacs rupees per annum.

Mechanical Engineering Notes & Course Syllabus

The syllabus of mechanical engineering is divided into eight semesters. The complete syllabus of mechanical engineering is given below:

1st Semester 2nd Semester
Engineering Mathematics-I

Engineering Physics / Engineering Chemistry

Systems in Mechanical Engineering

Basic Electrical Engineering / Basic Electronics Engineering

Programming and Problem Solving / Engineering Mechanics

Workshop

Engineering Mathematics-II

Engineering Physics/ Engineering Chemistry

Basic Electrical Engineering / Basic Electronics Engineering

Programming and Problem Solving / Engineering Mechanics

Engineering Drawing (Engineering Graphics )

Project-Based Learning

3rd Semester 4th Semester
Solid Mechanics

Solid Modelling and Drafting

Engineering Thermodynamics

Engineering Materials and Metallurgy

Electrical and Electronics Engineering

Engineering Mathematics – III

Kinematics of Machinery (Theory of Machines)- I

Applied Thermodynamics

Fluid Mechanics

Manufacturing Processes

5th Semester 6th Semester
Design of Machine Elements-I

Heat Transfer

Theory of Machines-II

Turbo Machines

Metrology and Quality Control

Skill Development

Numerical Methods and Optimization

Design of Machine Elements-II

Refrigeration and Air Conditioning

Mechatronics%

Manufacturing – Process-II

7th Semester 8th Semester
CAD/ CAM Automation

Dynamics of Machinery

Elective – I

Elective – II

Project – I

Power Plant Engineering

Mechanical System Design

Elective -III

Elective – IV

Project – II

Avail subject-wise B.Tech Notes related to Engineering Departments like ECE, CSE, Mech, EEE, Civil, etc. all in one place and plan your preparation according to your requirements.

Skill Set Required

  • Basic technical knowledge
  • Capability to work under pressure
  • Creativity
  • Problem-solving skills
  • Interpersonal skills
  • Verbal and written communication skills
  • Team Working skills
  • Commercial awareness
  • Fair knowledge of mathematics
  • Analytical skills

Course Duration

The course duration for the mechanical engineering course programme is different for the three different levels of the course programme:

  • The course duration of mechanical engineering polytechnic diploma is three years.
  • The course duration of mechanical engineering undergraduate engineering course is four years.
  • The course duration of mechanical engineering postgraduate engineering course is two years.

Mechanical Engineering Eligibility Criteria

The eligibility criteria mechanical engineering course programme at the Undergraduate and Postgraduate level is as follows:

  • Diploma in Mechanical Engineering:

Candidates should have passed the class 10 examination from a recognised board with an overall average of 45 per cent marks.

  • Undergraduate Programme in Mechanical Engineering: 

Candidates should have completed class 12 examination with Physics, Chemistry and Mathematics as the core subjects from a recognised board with a minimum of 45 per cent marks.

  • Postgraduate Programme in Mechanical Engineering: 

Candidates should have completed B. Tech in mechanical engineering.

Admission Procedure

There are national-level, state-level, and institutional-level entrance examinations that offer admission into mechanical engineering.

Entrance Examinations

You have to appear and qualify for Institutional-level, State-level, or National-level entrance examinations to take admission in mechanical engineering courses. Some of the popular entrance examinations for the mechanical engineering course programme are as follows:-

  • GATE
  • JEE Main
  • JEE Advanced
  • SRMJEE
  • BITSAT
  • VITEEE
  • KIITEEE
  • KEAM
  • WBJEE

Fee Structure

The average course fee for Diploma in mechanical engineering course programme is around 15000 to 1.5 Lakh rupees per annum. The average course fee for the B.Tech in mechanical engineering course programme is around 4 lacs to 10 lacs rupees per annum. The average course fee for the M.Tech in mechanical engineering course programme is around 40000 to 1 lac rupees per annum. However, the fee structure of mechanical engineering courses varies depending on the location and type of institution. Government colleges offer comparatively lower fee courses than private colleges.

Top Institutions

Some of the leading institutions that offer mechanical engineering course are given below:

  1. Indian Institute of Technology, Madras, Chennai
  2. Indian Institute of Technology, Bombay, Mumbai
  3. Indian Institute of Technology, Kharagpur
  4. Indian Institute of Technology, Delhi, New Delhi
  5. Indian Institute of Technology, Kanpur
  6. Indian Institute of Technology, Roorkee
  7. Indian Institute of Technology, Hyderabad
  8. Indian Institute of Technology, Gandhinagar, Ahmedabad
  9. Indian Institute of Technology, Ropar-Rupnagar
  10. Indian Institute of Technology, Patna
  11. Indian Institute of Technology, North Guwahati
  12. National Institute of Technology, Tiruchirappalli
  13. Vellore Institute of Technology, Vellore (first private institute in the category)

Employment Prospects

Mechanical engineering is a traditional field of engineering which has recently reached new horizons with the popularity of artificial intelligence, robotics, and machine learning. So, this course is highly demanding as this course teaches product development using modern technologies. A few more information about this course is given below.

Areas of Employment

  • Mechanical engineers can work in the following areas:
  • Manufacturing
  • Industrial
  • Maintenance
  • Automobile

Top Recruiters

Some of the top-level companies that hire mechanical engineers are given below.

  •  Escorts
  •  Tecumseh
  •  Hindustan Aeronautics Limited (HAL)
  •  BHEL
  •  NTPC
  •  NHPC
  •  JCB
  •  Hero Honda
  •  Railways
  •  Armed Forces
  •  Software companies like Infosys, TCS, L&T, and other small scale industries.

Salary Package

The average pay package for mechanical engineers differs based on the type of institution and industry. The average salary package of mechanical engineers varies from 3 lacs to 15 lacs rupees per annum.

Frequently Asked Questions on Mechanical Engineering

Question 1.

What is the course duration of a mechanical engineering course?

Answer:

  • The course duration of the mechanical engineering polytechnic diploma is three years.
  • The course duration of mechanical engineering undergraduate engineering course is four years.
  • The course duration of the mechanical engineering postgraduate engineering course is two years.

Question 2.

Which companies hire mechanical engineers?

Answer:

Some of the top-level companies that hire mechanical engineers are given below:

  •  Escorts
  •  Tecumseh
  •  Hindustan Aeronautics Limited (HAL)
  •  BHEL
  •  NTPC
  •  NHPC
  •  JCB
  •  Hero Honda
  •  Railways

Question 3.

What are some popular entrance examinations for mechanical engineering courses?

Answer:

Some popular mechanical engineering courses are GATE, JEE Main, JEE Advanced, SRMJEE, BITSAT, VITEEE, KIITEEE, KEAM, and WBJEE.

Question 4.

What is the course fee of B.Tech in Mechanical Engineering?

Answer:

The average course fee for the B.Tech in mechanical engineering course programme is around 4 lacs to 10 lacs rupees per annum.

Summary – Btech 1st, 2nd, 3rd, 4th Mechanical Engineering Lecture Notes Pdf

Hopefully, the information mentioned above helped you to know more about mechanical engineering courses. If you have any other questions regarding BTech Mechanical Engineering Notes Pdf, please let us know in the comments section.

Java Program to Print an Identity Matrix

Java Program to Print an Identity Matrix

In the previous article, we have discussed Java Program to Check Whether the Matrix is a Magic Square or Not

In this article we are going to see how we can write a program to print an indentity matrix in JAVA language.

Java Program to Print an Identity Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to print identity matrix.

Method-1: Java Program to Print an Identity Matrix By Static Initialization of Array Elements

Approach:

  • Ask the user for size.
  • Declare and instantiate an 2D array with the specified size.
  • Set all primary diagonal elements to 1 and rest to 0.
  • Print the matrix.

Program:

import java.util.Scanner;
public class matrix
{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);
        
        // Taking matrix size as input
        System.out.println("Enter square matrix row/column size : ");
        int rowSize = scan.nextInt();
        int colSize = rowSize, row, col;

        // Declaring and instantiating  the 3X3 matrix i.e. 2D array
        int arr[][] = new int[rowSize][colSize];

        //Creating the identity matrix
        for(row=0;row<rowSize;row++)
            for(col=0;col<colSize;col++)
                if(row==col)
                    arr[row][col] = 1;
                else
                    arr[row][col] = 0;

        
        System.out.print("The identity matrix elements are : ");
        
        // Loop to print the elements
        for(row=0;row<rowSize;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<colSize;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
}

Output:

Enter square matrix row/column size : 3
The identity matrix elements are : 
1 0 0 
0 1 0 
0 0 1

Method-2: Java Program to Print an Identity Matrix By Using User Defined Methods

Approach:

  • Ask the user for size.
  • Declare and instantiate an 2D array with the specified size.
  • Call an user defined method to print identity matrix.
  • Within the user defined method, Set all primary diagonal elements to 1 and rest to 0.
  • Print the matrix.

Program:

import java.util.Scanner;
public class matrix
{
        public static void main(String args[])
        {
            //Scanner class to take input
            Scanner scan = new Scanner(System.in);
            
            // Taking matrix size as input
            System.out.println("Enter square matrix row/column size : ");
            int rowSize = scan.nextInt();
            int colSize = rowSize, row, col;
    
            // Declaring and instantiating  the 3X3 matrix i.e. 2D array
            int arr[][] = new int[rowSize][colSize];
            
            //calling the user defined method
            //to print identity matrix
            identityMatrix(arr,rowSize,colSize);
            
            System.out.print("The identity matrix elements are : ");
            //method called to print the matrix
            printMatrix(arr,rowSize,colSize);
        }
        
        //creating the identoity matrix
        public static void identityMatrix(int arr[][],int rowSize,int colSize)
        {
        //Creating the identity matrix
        for(int row=0;row<rowSize;row++)
            for(int col=0;col<colSize;col++)
                if(row==col)
                    arr[row][col] = 1;
                else
                    arr[row][col] = 0;
        }
        
        
        // Method to print the matrix
        static void printMatrix(int arr[][],int rowSize,int colSize)
        {
            int row, col;
            // Loop to print the elements
            for(row=0;row<rowSize;row++)
            {
                // Used for formatting
                System.out.print("\n");
                for(col=0;col<colSize;col++)
                {
                    System.out.print(arr[row][col]+" ");
                }
            }
             System.out.print("\n");
        }
    
}

Output:

Enter square matrix row/column size : 
The identity matrix elements are : 
1 0 0 0 0 
0 1 0 0 0 
0 0 1 0 0 
0 0 0 1 0 
0 0 0 0 1

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs:

Java Program to Print Multiplication Table of Number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Java Program to Print Multiplication Table of Number

  • Write a Java program to print multiplication table of a number.

Given a number N, we have to print the multiplication table of N till 10 terms using a loop. In this java program, we are using a for loop to print table but same logic can be implemented using while or do-while loop.
For loop will iterate 10 times and in every iteration it will print one line of multiplication table.

Java program to print multiplication table of a number

Java program to print multiplication table of a number

package com.tcc.java.programs;
 
import java.util.Scanner;
 
/**
 * Java Program to print multiplication table of a number
 */
public class PrintTable {
 
    public static void main(String[] args) {
        int i, num;
        Scanner scanner;
        scanner = new Scanner(System.in);
        // Take input from user
        System.out.println("Enter a Number");
        num = scanner.nextInt();
 
        System.out.format("Multiplication Table of \n", num);
 
        for (i = 1; i <= 10; i++) {
            System.out.format("%d X %d = %d\n", num, i, num * i);
        }
    }
}

Output

Enter a Number
5
Multiplication Table of 
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50