FYBCom(CA) C practical Slip 2 Q 1.A

C Program to Calculate Area and Perimeter of a Rectangle

1.Introduction

In this blog,we will learn how to write a simple C program to calculate the area and perimeter of a rectangle.This program is perfect for beginners who are just starting with C programming.

2.Problem Statement

Question:
Write a C program to accept the length and breadth of a rectangle from the user and calculate its area and perimeter.

3.Logic

To solve this problem,we use the basic mathematical formulas of a rectangle:

Area of Rectangle=length×breadth
Perimeter of Rectangle=2×(length+breadth)

Steps:
1.Declare variables for length,breadth,area,and perimeter.
2.Ask the user to enter the length and breadth.
3.Calculate the area using the formula.
4.Calculate the perimeter using the formula.
5.Display the results.

4.Source Code

#include<stdio.h>

int main()
{
float length,breadth,area,perimeter;

printf("Enter the length of rectangle:");
scanf("%f",&length);

printf("Enter the breadth of rectangle:");
scanf("%f",&breadth);

area=length*breadth;
perimeter=2*(length+breadth);

printf("\nArea of rectangle=%.2f",area);
printf("\nPerimeter of rectangle=%.2f",perimeter);

return 0;
}

5.Sample Output

Enter the length of rectangle:10
Enter the breadth of rectangle:5

Area of rectangle=50.00
Perimeter of rectangle=30.00

6.Viva Questions

1.What is the formula for the area of a rectangle?
Answer:Area=length×breadth.

2.What is the formula for the perimeter of a rectangle?
Answer:Perimeter=2×(length+breadth).

3.Why do we use float data type in this program?
Answer:To allow decimal values for length and breadth.

4.What is the purpose of scanf() in C?
Answer:It is used to take input from the user.

5.What does return 0; indicate in a C program?
Answer:It indicates that the program executed successfully.

Conclusion

This simple C program helps beginners understand user input, arithmetic operations, and basic output formatting. Practice modifying the program by using integers or adding validation checks to improve your programming skills.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top