staircase hackerrank solution in c ,c++,python,java

staircase hackerrank solution 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

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 :

Code in C :

#include <stdio.h>


int main() {
    int ni,j;
    scanf("%d", &n);
    for(i=0;i<n;i++)
        {
        for(j=0;j<n;j++)
            {
            if(j<(n-1-i)) printf(" ");
            else printf("#");
            }
        printf("\n");
    }
  
    return 0;
}

Code in C++ :

#include <iostream>
using namespace std;


int main() {
    
    int n,i,j,k;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n-i;j++)
        {
            cout<<" ";    
        }
        for(k=j;k<=n;k++)
        {
            cout<<"#";    
        }
        cout<<"\n";
    }
     
    return 0;
}



Code in python :
def plusminus(n): k=map(int,input().split()) p=0 ne=0 z=0 for i in k: if i>0: p+=1 elif i==0: z+=1 elif i<0: ne+=1 print (round((p/n),3)) print (round((ne/n),3)) print (round((z/n),3)) n=int(input()) plusminus(n)
sdf
fgs
Code in java :
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int pos = 0; int zero = 0; int neg = 0; for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x > 0) { pos++; } else if (x == 0) { zero++; } else { neg++; } } System.out.println(pos / (double) n); System.out.println(neg / (double) n); System.out.println(zero / (double) n); } }


Please comment if you have any doubt or for any other problem .
Thanks
sasda


Post a Comment

0 Comments