Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Monday, June 15, 2020

Program to find perimeter of a rectangle in C


C Program to find perimeter of a rectangle:
By Ahmad Irshad | Programming Exercises and Solutions > find perimeter of a rectangle

Write a C program to input length and width of a rectangle and find its perimeter. See the below logic how to find perimeter of rectangle.

Perimeter : Sum of all sides is called perimeter. For example: A rectangle has four sides, since opposite sides of rectangle  are always equal. like length l and width so its perimeter would be p = l + w + l + w. 

Farmula of perimeter of a rectangle

  1. /* since opposite sides of a rectangle are equal */ p = l + w + l + w // sum of all sides
  2. p = 2l + 2w p = 2(l + w)

Required knowledge for this exercise is


Before writing any program you just imagine output screen first. For example: what input to be given, and output to be displayed. Let's have a look at the below example: 

INPUT:

Enter length of the rectangle: 20
Enter width of the rectangle: 40

OUTPUT:

Perimeter of a rectangle is = 120.00 units


Logic to calculate perimeter of a rectangle:

Step by step logic to calculate perimeter of a rectangle.

1) Print a massage to the user like "Enter length and width:" using printf();
2) Input marks using scanf(); and store it in a variables: length, and width
3) Now calculate perimeter of a rectangle using farmula: perimeter = 2 * (length + width)
6) At last print the values of perimeter. that's it!

Program to find perimeter of a rectangle

  1. /* C program to find perimeter of a rectangle */ #include <stdio.h> int main() { float length, width, perimeter; /* * Input length and width of rectangle from user */ printf("Enter length of the rectangle: "); scanf("%f", &length); printf("Enter width of the rectangle: "); scanf("%f", &width); /* Calculate perimeter of rectangle */ perimeter = 2 * (length + width); /* Print perimeter of rectangle */ printf("Perimeter of rectangle = %.2f units ", perimeter); return 0; }

Output:

  1. Enter length of the rectangle: 20
  2. Enter width of the rectangle: 40
  3. Perimeter of rectangle is = 120.00 units


Note: Never forget to prioritize the order of operations using a pair of braces ( ). Since, statements perimeter = 2 * length + width and perimeter = 2 * (length + width) will generate different results.

In addition, never write statement like 2 * (length + width) as 2(length + width). It will generate a compilation error.




Next Topic to be updated soon..>>




Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us