Breaking

Friday, 11 August 2017

Pattern Challange


You need to take an integer input and then draw the pattern according to it. Say for example if you enter 5 then, the pattern should be like this- 

            1 
         1 2 
      1 2 3 
   1 2 3 4 
1 2 3 4 5 


Input Format: 
You will take an integer input from stdin and the range of an integer can be from 1 to 1000. 

Output Format:
 
Your output should be the pattern according to the input which you had entered. 


Sample Test Case: 
Sample Input: 
5
Sample Output:
 
            1 
         1 2 
      1 2 3 
   1 2 3 4 
1 2 3 4 5


Code:
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i,j,k,n,x=1;
  6. cin>>n;
  7. int y=2;
  8. for(i=1;i<=n;i++)
  9. {
  10.     for(j=0;j<n*2-y;j++)
  11.         cout<<" ";
  12.    for(k=0;k<i;k++)
  13.    {
  14.        if(k==0)
  15.        cout<<x++;
  16.        else
  17.        cout<<" "<<x++;
  18.    }
  19.    x=1;
  20.    if(i!=0)
  21.    cout<<"\n";
  22.    y=y+2;
  23. }

  24. }




You need to take an integer input and then draw the pattern according to it. Say for example if you enter 5 then, the pattern should be like this- 

* * * * * 
   * * * * 
     * * * 
       * * 
         * 


Input Format: 
You will take an integer input from stdin and the range of an integer can be from 1 to 1000. 

Output Format:
 
Your output should be the pattern according to the input which you had entered. 


Sample Test Case:
 
Sample Input: 
5
Sample Output: 
* * * * * 
   * * * * 
     * * * 
       * * 
         *


#include <iostream>
using namespace std;
int main()
{

int i,j,n,k;
cin>>n;
int x=2;
for(i=n;i>0;i--)
{   
    if(i!=n)
   { for(j=0;j<x;j++)
        cout<<" ";
    x+=2;
   }
    for(k=0;k<i;k++)
    {
        if(k==0)
            cout<<"*";
        else
            cout<<" *";
    }
    if(i>0)
        cout<<"\n";
    
}
}


Pattern Problem - 13


This challenge will help you in getting familiarity with loops which will be helpful when you will solve further problems on Techgig. 

Task: 
You need to take an integer input and then draw the pattern according to it. Say for example if you enter 5 then, the pattern should be like this- 
           A 
        C B A 
     E D C B A 
  G F E D C B A 
I H G F E D C B A 


Input Format:
 
You will take an integer input from stdin and the range of an integer can be from 1 to 26. 

Output Format:
 
Your output should be the pattern according to the input which you had entered. 


Note: 
Please do not add space after the last character in each line. Failing to this will affect your submission and you will not get desired result. 


Sample Test Case 1:
 
Sample Input: 
5
Sample Output: 
           A 
        C B A 
     E D C B A 
  G F E D C B A 
I H G F E D C B A
Sample Test Case 2: 
Sample Input: 
3
Sample Output: 
      A 
   C B A 
E D C B A

Code:


#include <iostream>
using namespace std;
int main()
{
int i,n,j,k,x=2,ch='A',y=1;
cin>>n;
int h=n;
for(i=0;i<n;i++)
{
   for(j=0;j<n*2-x;j++)
       {
           
           cout<<" ";
       }
  x+=2;
  h=y-1;
   for(k=0;k<y;k++)
       {
          if(k==0)
               cout<<(char)(ch+h);
          else
               cout<<" "<<(char)(ch+h);
               h--;
       }
       h=n;
  y+=2;
  if(i<n)
  cout<<"\n";
}

}

1 comment:

  1. package com.mystring;
    import java.util.*;
    public class Apattern
    {
    public static void main(String arg[])
    {
    Scanner d=new Scanner(System.in);
    int h=d.nextInt();
    for(int i=1;i<=h;i++)
    {
    for(int j=1;j<=h-i;j++)
    {
    System.out.print(" ");
    }
    for(int k=(i*2)-1;k>0;k--)
    {
    char a=(char) (64+k);
    System.out.print(a);
    }
    System.out.println();
    }
    }
    }

    ReplyDelete

Like