Thursday 23 April 2015

AMCAT Programming Pattern to print 2*n number of rows for input n

For input N print 2*N number of rows in following Pattern
3
44
555
6666
555
44
3



The Solution is:


#include <iostream>

using namespace std;

int main()
{
   int n=4,num=n-1;
  for(int i=1;i<=n;i++)
  {
      for(int j=1;j<=i;j++)
      cout<<num;
      num++;
      cout<<endl;
  }
  num--;
   for(int i=n;i>=1;i--)
  {
      for(int j=1;j<=i;j++)
      cout<<num;
      num--;
      cout<<endl;
  }
 
 
   return 0;

}

11 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. The above code will not print the same pattern as in the question!
    One more line 6666 will be there.

    ReplyDelete
    Replies
    1. Just change ----
      Line 13 : num-=2;
      Line 14 : for(int i=n-1;i>=1;i--)

      Delete
  4. Can u write this cide by using functions

    ReplyDelete

  5. #include

    int i,j,k,start,n;

    void PrintMatchPattern(start,n)
    {
    for(i=1; i<=2*n; i++)
    {
    if(i<=n)
    {
    for (j=1; j<=i; j++)
    printf("%d", start);

    start++;
    printf("\n");
    k=j;
    }
    else
    {
    start--;
    for (j=1; j<k; j++)
    printf("%d", start);

    printf("\n");
    k--;
    }
    }
    }
    int main()
    {

    PrintMatchPattern(2,5);
    return 0;
    }

    ReplyDelete
  6. Java:-
    public class Testing {

    public static void main(String[] args) {

    int n = 4, num = n - 1;
    for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++)

    System.out.print(num);
    num++;
    System.out.println();

    }

    num = num - 2;
    for (int i = n - 1; i >= 1; i--) {
    for (int j = 1; j <= i; j++)
    System.out.print(num);
    num--;
    System.out.println();
    }
    }

    }

    ReplyDelete
    Replies
    1. Is this correct?
      I wrote the same but we showing error

      Delete
  7. public class xxx
    {
    public static void main(String[] args)
    {
    int n=4,start=3;

    for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=i;j++)
    System.out.print(i+(start-1));

    System.out.println();
    }

    for(int i=n;i>=1;i--)
    {
    for(int j=1;j<=i;j++)
    System.out.print(i+(start-1));

    System.out.println();
    }
    }
    }

    ReplyDelete
  8. import java.util.*;
    class pattern{
    public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    int num=sc.nextInt(); int val=num;
    for(int i=1;i<=(num+1);i++){
    for(int j=1;j<=i;j++){
    System.out.print(val+" ");

    }
    val++;
    System.out.println();
    }
    val=val-2;
    for(int i=num;i>=1;i--){
    for(int j=1;j<=i;j++){
    System.out.print(val+" ");
    }
    val--;
    System.out.println();
    }
    }
    }

    ReplyDelete