Print The Following Pattern For Given Number Of Rows by using Java Program
Pattern:
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
Code:
import java.util.*;
public class Pattern
{
public static void main(String[] args)
{
int i,j,n;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Number Of Row : ");
n = sc.nextInt();
System.out.println();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
{
System.out.print("1 ");
}
else
{
System.out.print("0 ");
}
}
System.out.println();
}
System.out.println();
}
}
Output of program:
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
Code:
import java.util.*;
public class Pattern
{
public static void main(String[] args)
{
int i,j,n;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Number Of Row : ");
n = sc.nextInt();
System.out.println();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
{
System.out.print("1 ");
}
else
{
System.out.print("0 ");
}
}
System.out.println();
}
System.out.println();
}
}
Output of program:
No comments