Solution code in C, C++, Python, Java
Problem statement:-
Staircase detail
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 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 .
C Code:-
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char* ltrim(char*);
char* rtrim(char*);
int parse_int(char*);
/*
* Complete the 'staircase' function below.
*
* The function accepts INTEGER n as parameter.
*/
void staircase(int n) {
int i,j;
for(i=1;i<=n;i++)
{
for(j=n;j>i;j--)
printf(" ");
for(j=0;j<i;j++)
printf("#");
printf("\n");
}
}
int main()
{
int n = parse_int(ltrim(rtrim(readline())));
staircase(n);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}
C++ Code:-
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
/*
* Complete the 'staircase' function below.
*
* The function accepts INTEGER n as a parameter.
*/
void staircase(int n) {
int i,j;
for(i=1;i<=n;i++)
{
for(j=n;j>i;j--)
cout<<" ";
for(j=0;j<i;j++)
cout<<"#";
cout<<endl;
}
}
int main()
{
string n_temp;
getline(cin, n_temp);
int n = stoi(ltrim(rtrim(n_temp)));
staircase(n);
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
Python Code:-
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'staircase' function below.
#
# The function accepts INTEGER n as parameter.
#
def staircase(n):
for i in range(1,n+1):
for j in range(n-i):
print(" ",end="")
for j in range(i):
print("#",end="")
print()
# Write your code here
if __name__ == '__main__':
n = int(input().strip())
staircase(n)
Java Code:-
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;