This blog strives to provide answers to various problem statements in HackeRank.The blog is used only for educational purposes. This blog does not endorse copying answers.
Thursday, May 5, 2022
Staircase
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
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:
#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=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j>=n-1)
printf("#");
else {
printf(" ");
}
}
printf("\n");
}
}
int main()
{
int n = parse_int(ltrim(rtrim(readline())));
staircase(n);
return0;
}
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);
No comments:
Post a Comment