Wednesday, 22 June 2016

Program to print a Number Pattern Type-2.

Explanation :
If n=6 then output will be
 111111
100001
100001
100001
100001
 111111.

Code:

import java.util.Scanner;
public class Pattern2
{
        public static void main( String args[])
        {
            System.out.print("Enter the number of rows:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int i,j;
            for (i=1;i<=n;i++)
            {
                for (j=1;j<=n;j++)
                {
                    if(j==n || j==1 || i==1 || i==n)
                        System.out.printf("1");
                    else
                        System.out.printf("0");
                }
                System.out.printf("\n");
            }
            sc.close();
        }
}

No comments:

Post a Comment