Sunday, December 15, 2013

Programming Challenges Question 1

Programming Challenges Question 

Write a program which will print the below structures according to the input provided to the program.
The program should accept 3 inputs in the form of numbers between 1 and 9, both inclusive (one number per line) and then generate the corresponding structures based on the input.
Suppose the following sequence of numbers is supplied to the program:
3
2
4

Then the output should be:
  1
 2 2
3 3 3
 1
2 2
   1
  2 2
 3 3 3
4 4 4 4

/**********************Code1 in JAVA******************************/
import java.io.*;
public class Main {
public static void main(String[] args) {
    try
    {
        int a[]= new int[3];
    InputStreamReader sr= new InputStreamReader(System.in);
    BufferedReader br= new BufferedReader(sr) ;
     System.out.print("Enter 3 Number:\n");
     for(int i=0;i<3;i++)
     {
      a[i] = Integer.parseInt(br.readLine());
     }
     for(int count=0;count<3;count++)
     {
        for(int i=1;i<=a[count];i++)
          {
             for(int j=a[count];j>i;j--)
             {
                 System.out.print(" ");
             }
             for(int k=1;k<=i;k++)
             {
              System.out.print(k+" ");
             }
             System.out.println();
          }
     }
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
    }

    }


/******************************** Code2 in JAVA*********************************/
 import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
   int a,b,c;
 Scanner in = new Scanner(System.in);
 a = in.nextInt();
 b = in.nextInt();
 c = in.nextInt();
 int[] array={a,b,c};
    int k;
  for(int x=0; x<3; x++){
  int y = array[x];
  int z= y+1;
   for(int i=1;i<=y;i++){
   System.out.println();
   System.out.format("%"+z+"s", "");

    for(int j=1;j<=i;j++)
    {
    k=i;
    System.out.format("%"+1+"s", "");
    System.out.print(k);
    }
    z--;
   }
  }
   }
}



No comments:

Post a Comment