Staircase detail
This is a staircase of size :
#
##
###
####
Its base and height are both equal to n. 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 n.
Example:
Input: 4
Output:
# ## ### ####
Approach
Java
public class Staircase {public static void main(String[] args) {int n = 4;staircase(n);}static void staircase(int n) {int i, j;for (i = 1; i <= n; i++) {for (j = 1; j <= n; j++) {if (j >= n + 1 - i) {System.out.print("#");} else {System.out.print(" ");}}System.out.println();}}}
C++
#include <bits/stdc++.h>using namespace std;int main(){int n = 4;int i, j;for (i = 1; i <= n; i++){for (j = 1; j <= n; j++){if (j >= n + 1 - i){cout << "#";}else{cout << " ";}}cout << "\n";}return 0;}
No comments:
Post a Comment