Sunday, October 17, 2021

Rectangle area Hackerrank

 In this challenge, you are required to compute the area of a rectangle using classes.

Create two classes:

Rectangle

The Rectangle class should have two data fields-width and height of int types. The class should have display() method, to print the width and height of the rectangle separated by space.

RectangleArea

The RectangleArea class is derived from Rectangle class, i.e., it is the sub-class of Rectangle class. The class should have read_input() method, to read the values of width and height of the rectangle. The RectangleArea class should also overload the display() method to print the area  of the rectangle.

Input Format

The first and only line of input contains two space separated integers denoting the width and height of the rectangle.

Constraints

Output Format

The output should consist of exactly two lines:
In the first line, print the width and height of the rectangle separated by space.
In the second line, print the area of the rectangle.

Sample Input

10 5

Sample Output

10 5
50
class Rectangle
{
    protected:
       int width;
       int height;
    
    public:
    void display() {
        
        cout<<width<<" "<<height;
        
         }
};

class RectangleArea : public Rectangle {
        
        public:
        void read_input(){
            cin>>width>>height;
        }
        
        void display() {
            cout<<endl<<width*height;
        }
        
    };

Wednesday, October 13, 2021

Time conversion java


Given a time in -hour AM/PM format, convert it to military (24-hour) time.

Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

Example

  • Return '12:01:00'.

  • Return '00:01:00'.

Function Description

Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.

timeConversion has the following parameter(s):

  • string s: a time in  hour format

Returns

  • string: the time in  hour format

Input Format

A single string  that represents a time in -hour clock format (i.e.:  or ).

Constraints

  • All input times are valid

Sample Input 0

07:05:45PM

Sample Output 0

19:05:45



Answer:

  public static String timeConversion(String s) {

    // Write your code here

        StringBuilder sb=new StringBuilder(); sb.append(s);


    if(sb.charAt(8)=='A'){


        if(sb.substring(0,2).equals("12")){

            sb.delete(8,10);

            sb.replace(0,2,"00");

        }

        else{

            sb.delete(8,10);

        }


    }

    else{

        if(sb.substring(0,2).equals("12")){

            sb.delete(8,10);

        }

        else{

            sb.delete(8,10);

            int value=Integer.parseInt(sb.substring(0,2))+12;

            sb.replace(0,2,String.valueOf(value));

        }


    }

    s=sb.toString();

    return s;


    }


}

Staircase Problem - C language


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 .

Answer::



void staircase(int n) {

int i,j;

    if(0<n && n<100)

        for(i=0;i<n;i++)

        {

            for(j=1;j<n-i;j++)

                printf(" ");

            for(j=0;j<=i;j++)

                printf("#");

            printf("\n");

        }

    else

    {

        printf("Please Enter valid Number");

    }

}

 

Java Regex

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered v...