Thursday 26 March 2015

PATTERN PROGRAM 1 : ENTER THE NUMBER OF TERMS SO THAT A PATTERN OF NUMBERS IS PRODUCED.

Example : you have entered the number '5' ; then the following output comes:



PROGRAM :

import java.util.Scanner;
public class assi
{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of terms to be printed:");
        int n = sc.nextInt();
        int c = n - 1;
        int f = 1;
        for(int i = 1 ; i<=n ; i++){
            for(int s = 1 ; s<=c ; s++){
                System.out.print(" ");
            }
            c = c - 1;
            for(int j = 1 ; j<=f ; j++){
                System.out.print(j);
            }
            f = f + 2;
            System.out.println();
        }
        int d = 1;
       
        for(int i = 1 ; i<= n ; i++){
            for(int s = 1 ; s<=d ; s++){
                System.out.print(" ");
            }
            for(int a = 1 ; a<=f - 4 ; a++){
                System.out.print(a);
            }
            f = f - 2;
            d = d + 1;
            System.out.println();
        }
    }
}

No comments:

Post a Comment