Perimeter of rectangle

Write a program to find the perimeter of the rectangle.

Example 1:

Input: side1=4,side2=3
Output: 14

Approach: Given sides of rectangle. 

Perimeter is 2*(side1+side2)

Java

public class PerimeterofRectangle {
    public static void main(String[] args) {
        double side1 = 4;
        double side2 = 3;
        double perimeter = perimeterRectangle(side1, side2);
        System.out.println("Perimeter of Rectangle is " + perimeter);
    }

    private static double perimeterRectangle(double side1double side2
        {
        return 2 * (side1 + side2);
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
//function to find the  perimeter
//of the rectangle
double perimeterRectangle(double side1,double side2)
{
    double perimeter=2*(side1+side2);
    return perimeter;
}
int main()
{
    double side1=4;
    double side2=3;
    double perimeter=perimeterRectangle(side1,side2);
    cout<<"Perimeter of rectangle is ";
    cout<<perimeter<<"\n";
    return 0;
}


No comments:

Post a Comment