Computer Science, asked by rebecasunny90, 1 month ago

write a program to input side of a square. calculate and display area and perimeter

Answers

Answered by raisanjeet8896
3

Explanation:

Using python you can easily determine Area as well as perimeter of square

Attachments:
Answered by Anonymous
10

Area and Perimeter of Square - Python

Assuming that the question needs Python program to input side of a square and to calculate and display area and perimeter of square. As we are not mentioned in the Question only by which programming language we write the required program, therefore here we use python program.

Let us take a look at what we need to do in this program.

First we would simply take in the user input of the side of square and store them as float value. Since we not familiar that what type of value will the user accept.

We use float() function since user can also enter the decimal value, values could always have the possibility of being in decimals. If we use int() function as the data type and a user enters a decimal value or integers, the program would result in an error. To be on the safer side, we use float() as both integers and decimals are accepted.

As we know that, the formula of area of square and the formula of perimeter of square.

The area of square : A = a²

The perimeter of square : P = 4a

[Here, A is the area of square, a is the side of the square and P is the perimeter of square respectively.]

But here the side of square is in square on area of square, so here we can use pow() function to raise square. You can also use Exponentiation operator or by simply multiply side value by two times.

Finally, by simply using these formulas we can calculate the area of square and the perimeter of square respectively. And we will print the area of square and the perimeter of square respectively by using f strings.

\rule{300}{1}

The Python Program

Here's the Python program to calculate the area and perimeter of square respectively.

# Python program to calculate the area and perimeter of square

# Take input of side of square

a = float(input("Enter the side: "))

# Area of square = a²

area = pow(a,2)

# Perimeter of square = 4a

perimeter = 4*a

# Print out the area of square

print(f"The area of square is {area} sq units.")

# Print out the perimeter of square

print(f"The perimeter of square is {perimeter} units.")

We can shorten the above program even more. Let's shorten the program.

# Python program to calculate the area and perimeter of square

# Take input of side of square

a = float(input("Enter the side: "))

# Print out the area of square by using area of square formula, area = a²

print(f"The area of square is {pow(a,2)} sq units.")

# Print out the perimeter of square by using perimeter of square formula, perimeter = 4a

print(f"The perimeter of square is {4*a} units.")

[Hashtag (#) is only a single line comment in Python, It's not printed in the program, I have used them here for understanding the program step-by-step. If you want then you can remove those comments from the program.]

I'll be pasting here the same program by removing all the comments.

a = float(input("Enter the side: "))

area of square formula, area = a²

print(f"The area of square is {pow(a,2)} sq units.")

print(f"The perimeter of square is {4*a} units.")

\rule{300}{1}

Sample run

If you run the given program then the output will be as follows:

Enter the side: 10

The area of square is 100.0 sq units.

The perimeter of square is 40.0 units.

[Program finished]

Similar questions