Find the perimeter of a square
In this post, you will learn how to find the perimeter of a square using different programming language.
The perimeter of a square is defined as the total length that its boundary covers. The perimeter of a square is the total length of all its sides. All sides of a square are equal, so the perimeter of the square is equivalent to 4 times the side of the square. The perimeter of a square is the distance covered by its four sides. As, all the sides of a square are equal, therefore, the perimeter of square will be 4 times its side, i.e. 4 × Side.
Formula to calculate the perimeter of a square
Perimeter of square, (P) = 4 × Side
C program to find the perimeter of a square
The given C program, we compute the perimeter of a square based on the user input.
// Find perimeter of a square
#include<stdio.h>
void main()
{
int length, perimeter=0;
printf("\nEnter Length of a square : ");
scanf("%d", &length);
perimeter = 4 * length;
printf("Perimeter of a square : %d", perimeter);
}
Output of the above code:
Enter Length of a square : 32
Perimeter of a square : 128
Python program to find the perimeter of a square
In the given program, you will learn how to calculate the perimeter of a square using Python programming language.
# Find perimeter of a square
side = int(input("Enter the side of a square: " ))
#Formula for Perimeter of square
perimeter = 4*side
print("Perimeter of a square : ",perimeter)
Output of the above code:
Enter the side of a square: 51
Perimeter of a square : 204
Java program to find the perimeter of a square
In the given program, you will learn how to calculate the perimeter of a square using the Java programming language.
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int length, perimeter;
Scanner scan = new Scanner(System.in);
System.out.print("Enter length of sqaure : ");
length = scan.nextInt();
perimeter = (4*length);
System.out.print("\nPerimeter of square : " +perimeter);
}
}
Output of the above code:
Enter length of square : 31
Perimeter of square : 124
C++ program to find the perimeter of a square
The given C++ program, we compute the perimeter of a square based on the user input.
#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int side, area, perimeter;
cout<<"Side : ";
cin>>side;
perimeter = side*side;
cout<<"Perimeter of Square : "<<perimeter;
}
Output of the above code:
Side : 6
Perimeter of Square : 36
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number