Perimeter of rectangle#7943

Perimeter of rectangle (Editorial)

The perimeter of a rectangle with sides and is 2 \cdot (a + b).

Algorithm implementation

Read the sides of the rectangle.

scanf("%d %d",&a,&b);

Compute and print the perimeter of the rectangle.

p = (a + b) * 2;
printf("%d\n",p);

Algorithm implementation — class

#include <stdio.h>

class Rectangle
{
public:
  int x, y;
  Rectangle(int x, int y) : x(x), y(y) {}

  int Perimeter(void)
  {
    return 2 * (x + y);
  }
};

int x, y;

int main(void)
{
  scanf("%d %d", &x, &y);
  Rectangle a(x, y);
  printf("%d\n", a.Perimeter());
  return 0;
}

Java implementation

import java.util.*;

public class Main
{
  public static void main(String[] args) 
  {
    Scanner con = new Scanner(System.in);
    int x = con.nextInt();
    int y = con.nextInt();
    int p = 2 * (x + y);
    System.out.println(p);
    con.close();
  }
}

Java implementation — class

import java.util.*;

class Rectangle
{
  int a, b;

  Rectangle(int a, int b)
  {
    this.a = a;
    this.b = b;
  }
	
  public int Perimeter()
  {
    return 2 * (a + b);
  }
};

public class Main
{
  public static void main(String[] args) 
  {
    Scanner con = new Scanner(System.in);
    int a = con.nextInt();
    int b = con.nextInt();
    
    Rectangle r = new Rectangle(a,b);
    System.out.println(r.Perimeter());
    
    con.close();
  }
}

Java implementation — class + MyInteger

import java.util.*;

class MyInteger
{
  private int a;
  
  MyInteger(int a)
  {
    this.a = a;
  }

  MyInteger Add(MyInteger b)
  {
    return new MyInteger(a + b.a); 
  }
  
  MyInteger Mult(MyInteger b)
  {
    return new MyInteger(a * b.a); 
  }

  MyInteger Mult(int b)
  {
    return new MyInteger(a * b); 
  }
  
  public String toString()
  {
    return String.valueOf(a);
  }
}

class Rectangle
{
  MyInteger a, b;

  Rectangle(MyInteger a, MyInteger b)
  {
    this.a = a;
    this.b = b;
  }
	
  public MyInteger Perimeter()
  {
    return a.Add(b).Mult(2);
  }
};

public class Main
{
  public static void main(String[] args) 
  {
    Scanner con = new Scanner(System.in);
    MyInteger a = new MyInteger(con.nextInt());
    MyInteger b = new MyInteger(con.nextInt());
    
    Rectangle Rect = new Rectangle(a,b);
    System.out.println(Rect.Perimeter());
    
    con.close();
  }
}

Python implementation

Read the sides of the rectangle.

a, b = map(int, input().split())

Compute and print the perimeter of the rectangle.

p = (a + b) * 2
print(p)