🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Functions in C programming provide modularity, which makes the code organized, manageable, and reusable. This tutorial will demonstrate how to use functions to calculate the area of a rectangle.
2. Program Overview
1. Define a function named calculateArea that will take the length and width of the rectangle as its parameters and will return the calculated area.
2. Inside the main function, declare variables for length, width, and area.
3. Prompt the user to enter the length and width of the rectangle.
4. Call the calculateArea function by passing the entered length and width, and store the result in the area variable.
5. Display the computed area of the rectangle.
3. Code Program
#include <stdio.h>
// Function to calculate the area of a rectangle
float calculateArea(float length, float width) {
return length * width; // return the product of length and width
}
int main() {
float length, width, area;
// Input rectangle details
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
// Calculate the area
area = calculateArea(length, width);
// Display the area
printf("The area of the rectangle is: %.2f\n", area);
return 0;
}
Output:
Enter the length of the rectangle: 5 Enter the width of the rectangle: 3 The area of the rectangle is: 15.00
4. Step By Step Explanation
1. We have a function named calculateArea that accepts two parameters: length and width. This function computes the area of the rectangle by multiplying the length by the width and returns the result.
2. Inside the main function, we declare three float variables: length, width, and area.
3. We then prompt the user to input the length and width of the rectangle and store these values in the length and width variables respectively.
4. The function calculateArea is called by passing the entered length and width as arguments. The returned result (area of the rectangle) is stored in the area variable.
5. Finally, we display the computed area using printf.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment