Thursday 23 April 2015

Amcat Trapezium pattern Solution

To print the trapezium pattern.
 for example , we have num=4
the output should be like



1*2*3*4*17*18*19*20
- -5*6*7*14*15*16
- - - -8*9*12*13
- - - - - -10*11


the solution in c++ is:

#include<iostream>
using namespace std;
int main(){
    int n=4,num=1,i=1,space=0,k=1,number=n;
    for(i=0;i<n;i++)
    {
        for(int j=1;j<=space;j++)
        {
         
            cout<<"-";
         
        }
        for(int m=1;m<2*n-space;m++)
        {
            if(m%2==0)
                cout<<"*";
            else
                cout<<num++;
        }
        cout<<"*";
         for(int l=1;l<2*n-space;l++)
        {
            if(l%2==0)
                cout<<"*";
            else
            {
                cout<<k+number*number;
        k++;
            }
        }
        number--;
     
     
        space=space+2;
        cout<<endl;
    }
    return 0;
}

                    

11 comments:

  1. Replies
    1. yeah sure..here is the solution.

      #include
      int main(){
      int n=4,num=1,i=1,space=0,k=1,number=n;
      for(i=0;i<n;i++)
      {
      for(int j=1;j<=space;j++)
      {

      printf("-");

      }
      for(int m=1;m<2*n-space;m++)
      {
      if(m%2==0)
      printf("%s","*");
      else
      printf("%d",num++);
      }
      printf("%s","*");
      for(int l=1;l<2*n-space;l++)
      {
      if(l%2==0)
      printf("%s","*");
      else
      {
      printf("%d",k+number*number);
      k++;
      }
      }
      number--;


      space=space+2;
      printf("\n");
      }
      return 0;
      }

      Delete
    2. Can you please explain the logic?

      Delete
  2. Can you provide the same in java

    ReplyDelete
  3. public class TrapeziumPattern {
    public static void Trapezium(){
    int n=4, i=1,k=1,s=0,num=1,number=n;


    for(i=0;i<n;i++){
    System.out.println();
    for(int j=1;j<=s;j++){

    System.out.print("-");

    }
    for(int m=1;m<2*n-s;m++)
    {
    if(m%2==0)
    System.out.print("*");

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

    }
    System.out.print("*");

    for(int l=1;l<2*n-s;l++)
    {
    if(l%2==0)
    System.out.print("*");

    else
    {
    System.out.print(k+number*number);

    k++;
    }
    }
    number--;


    s=s+2;
    //System.out.println();
    }
    //return 0;
    }


    public static void main(String[] args) {
    TrapeziumPattern.Trapezium();

    }

    }

    ReplyDelete
  4. C++ code to print any Number trapezium

    #include
    using namespace std;
    int main()
    {
    int rows;
    cout<<"Enter the no of rows";
    cin>>rows;
    int number=0;
    int number1=0;
    int temp=rows;
    while(temp>0)
    {
    number1+=temp;
    temp--;
    }
    number1=number1*2;
    for(int i=1;i<=rows;i++)
    {
    for(int j=i;j>1;j--)
    cout<<" ";
    for( int k=rows;k>=i;k--)
    {
    cout<<++number;
    if(k!=0)
    cout<<"*";
    }

    int get=number1-(rows-i);
    for(int l=i;l<=rows;l++)
    {
    cout<<get++;
    if(l!=rows)
    {
    cout<<"*";
    }
    if(l==rows)
    {
    number1=number1-(rows-i);
    number1--;
    }
    }
    cout<<"\n";
    }
    return 0;
    }

    ReplyDelete
  5. pls give this by using function in c++

    ReplyDelete
  6. Can u plz provide the same in python

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

    ReplyDelete
  8. Can u provide for inverted trapezium in c
    Like
    ---10 11
    --8 9 12 13
    -5 6 7 14 15 16
    1 2 3 4 17 18 19 20

    ReplyDelete
  9. please any one give explanation about this program execution

    ReplyDelete