Variables hold the numbers a program calculates with.
What to build
A program that calculates the area and perimeter of a rectangle.
Requirements
- Read the width and the height.
- Print
Area: Xon the first line andPerimeter: Yon the second.
Input
One line with two whole numbers: width and height.
Output
Two lines.
Constraints
1 ≤ width, height ≤ 10000.
Example
Input:
3 4
Output:
Area: 12
Perimeter: 14
Why it works
Both values are ints, so the products and sums are ints too. Printing with Printf puts each result after its label.
Examples
Example 1 · Input
3 4
Expected output
Area: 12 Perimeter: 14
Example 2 · Input
10 10
Expected output
Area: 100 Perimeter: 40
Hints
Hint 1
Declare the two numbers as ints and read them with fmt.Scan(&w, &h).
Hint 2
Area is w * h and perimeter is 2 * (w + h).
Solution
Show one correct solution (try it yourself first)
package main
import "fmt"
func main() {
var w, h int
fmt.Scan(&w, &h)
fmt.Printf("Area: %d\n", w*h)
fmt.Printf("Perimeter: %d\n", 2*(w+h))
}