Showing posts with label pattern. Show all posts
Showing posts with label pattern. Show all posts

Sunday, April 17, 2022

Staircase | HackerRank | C#

 This is a staircase of size :

             #
          ##
      ###
  ####

Its base and height are both equal to . It is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Function Description

Complete the staircase function in the editor below.

staircase has the following parameter(s):

  • int n: an integer

Print

Print a staircase as described above.

Input Format

A single integer, , denoting the size of the staircase.

Constraints

 .

Output Format

Print a staircase of size  using # symbols and spaces.

Note: The last line must have  spaces in it.

Sample Input

6 

Sample Output

                    #
                 ##
             ###
         ####
     #####
######

Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of .


Solution

    public static void staircase(int n)
    {
        for(int i=0; i < n; i++)
        {
            Console.WriteLine(new String('#', i+1).PadLeft(n));
        }      
    }

Thursday, June 18, 2020

Write a program to print the given below pattern 4

Sample Input:

3

Sample Output:

112
322
334

Sample Input:

5

Sample Output:

11112
32222
33334
54444
55556

SOLUTION:

#include<iostream> 
using namespace std; 
void printPattern(int n) 
{ 
	int j, k = 1; 
	// loop to decide the row number 
	for (int i = 1; i <= n; i++) 
	{ 
		// if row number is odd 
		if (i%2 != 0) 
		{ 
			for (j = 0; j < n-1; j++) 
				cout << k; 
			cout << ++k << endl;  
		} 
		
		// if row number is even 
		else
		{ 
          	cout << ++k;
          	k--;
			for (j = 0; j < n-1; j++ ) 
				cout << k ; 	
          	cout<<endl;
          	k++;
		} 
	} 
} 
int main() 
{ 
	int n ;
  	cin>>n;
	printPattern(n); 
	return 0; 
} 

Write a program to print the following pattern 3

Sample Input:

5

Sample Output:

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

Sample Input:

4

Sample Output:

1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1

SOLUTION:

#include<iostream>
using namespace std;
void printPattern(int n){
int i,j,c=1;
  for(i = 1; i <= n; i++){
    for(j = 0; j < i-1; j++)
      cout<<c<<"*";
    cout<<c++<<endl;
  }
  c--;
    for(i = n; i >= 1; i--){
    for(j = i-1; j > 0; j--)
      cout<<c<<"*";
    cout<<c--<<endl;
  }
}
int main(){
  int n;
  cin>>n;
  printPattern(n);
}


Wednesday, June 17, 2020

Write a program to print the following pattern.

Sample Input:

3

Sample Output:

3 
44 
555 
6666 
6666 
555 
44 
3

Sample Input:

4

Sample Output:

4
55
666
7777
7777
666
55
4

Sample Input:

5

Sample Output:

5
66
777
8888
8888
777
66
5


SOLUTION:

#include<iostream>
using namespace std;
int main(){
  int n, i, j, temp;
  cin>>n;
  temp = n;
    for(i = 1; i < 5; i++){
      for(j = 0; j < i; j++){
        cout<<temp;
      }
      temp++;
      cout<<"\n";
    }
  temp--;
    for(i = 4; i > 0; i--){
      for(j = i; j > 0; j--){
        cout<<temp;
      }
      temp--;
      cout<<"\n";
    }
  return 0;
}




















Write a program to print the trapezium pattern

Sample Input:

4

Sample Output:

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

Sample Input:

5

Sample Output:

1*2*3*4*5*26*27*28*29*30
--6*7*8*9*22*23*24*25
----10*11*12*19*20*21
------13*14*17*18
--------15*16

SOLUTION :

#include<iostream>
using namespace std;
void printPattern(int n){
int i, j, c = 1, temp = n, c2 = n * n + 1, k = 0, m = 0;
  for(i = 0; i < n; i++){
	for(j = 0; j < m; j++)
	cout<<"-";
	m = i*2+2;
	for(j = temp; j > 0; j--)
	cout<<c++<<"*";
	for(j=temp-1;j>0;j--){
	k++;
	cout<<c2++<<"*";}
	cout<<c2++;
	c2 = c2 - temp -k;
	k= 0;
	temp--;
	
	cout<<endl;  
  }
}
int main(){
  int n ;
  cin>>n;
  printPattern(n);
}

Write a program to print the following pattern 2

Sample Input:

5

Sample Output:

1
3*2
4*5*6
10*9*8*7
11*12*13*14*15

Sample Input:

6

Sample Output:

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

solution:

#include <iostream> 
using namespace std; 
void printPattern(int n) 
{ 
	int j, k = 0; 
	for (int i=1; i<=n; i++) 
	{ 
	
		if (i%2 != 0) 
		{ 

			for (j=k+1; j<k+i; j++) 
				cout << j << "*"; 
			cout << j++ << endl; 

			k = j;	 
		} 

		else
		{ 

			k = k+i-1; 
			for (j=k; j>k-i+1; j--) 
				cout << j << "*"; 
			cout << j << endl;	 
		} 
	} 
} 

int main() 
{ 
	int n;
 	cin>>n;
	printPattern(n); 
	return 0; 
} 

horizontal ads