- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
C Program – Area and Perimeter of Rectangle - Direct Calculation & Functions
Rectangle Area and Perimeter (Direct)
#include <stdio.h>
int main() {
float length, breadth, area, perimeter;
printf("Enter length and breadth of the rectangle: ");
scanf("%f %f", &length, &breadth);
area = length * breadth;
perimeter = 2 * (length + breadth);
printf("Area = %.2f\n", area);
printf("Perimeter = %.2f\n", perimeter);
return 0;
}
Enter length and breadth of the rectangle: 5 3
Area = 15.00
Perimeter = 16.00
This straightforward method calculates the area by multiplying length and breadth and computes the perimeter as twice the sum of the sides, printing both values for the user.
Rectangle Area and Perimeter Using Functions
#include <stdio.h>
float area(float l, float b) {
return l * b;
}
float perimeter(float l, float b) {
return 2 * (l + b);
}
int main() {
float length, breadth;
printf("Enter length and breadth of the rectangle: ");
scanf("%f %f", &length, &breadth);
printf("Area = %.2f\n", area(length, breadth));
printf("Perimeter = %.2f\n", perimeter(length, breadth));
return 0;
}
Enter length and breadth of the rectangle: 7.5 2.0
Area = 15.00
Perimeter = 19.00
Encapsulating the area and perimeter computations in separate functions makes the code modular and reusable. Each function takes length and breadth as parameters, returns the computed value, and is called from the main function.
⬅ Previous TopicC Program - Find Compound Interest
Next Topic ⮕C Program - Check Whether a Number is Positive, Negative, or Zero
Next Topic ⮕C Program - Check Whether a Number is Positive, Negative, or Zero
Comments
Loading comments...