Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program for focal length of a lens
In optics, the focal length is a fundamental property of lenses that determines their converging or diverging power. Given the image distance and object distance from a lens, we can calculate the focal length using the lens equation.
What is Focal Length?
Focal length of an optical system is the distance between the center of lens or curved mirror and its focus. It represents the distance over which initially collimated rays are brought to a focus.
Syntax
1/F = 1/O + 1/I
Where:
- F is the focal length
- O is the object distance from the lens
- I is the image distance from the lens
Example 1: Basic Focal Length Calculation
Let's calculate the focal length when image distance is 5 units and object distance is 10 units −
#include <stdio.h>
/* Function to calculate focal length using lens equation */
float calculateFocalLength(float image_distance, float object_distance) {
return 1.0 / ((1.0 / image_distance) + (1.0 / object_distance));
}
int main() {
float image_distance = 5.0;
float object_distance = 10.0;
float focal_length;
focal_length = calculateFocalLength(image_distance, object_distance);
printf("Object distance: %.2f units<br>", object_distance);
printf("Image distance: %.2f units<br>", image_distance);
printf("Focal length: %.6f units<br>", focal_length);
return 0;
}
Object distance: 10.00 units Image distance: 5.00 units Focal length: 3.333333 units
Example 2: Multiple Test Cases
Here's a program that calculates focal length for different sets of distances −
#include <stdio.h>
float calculateFocalLength(float image_distance, float object_distance) {
if (image_distance == 0 || object_distance == 0) {
printf("Error: Distance cannot be zero<br>");
return -1;
}
return 1.0 / ((1.0 / image_distance) + (1.0 / object_distance));
}
int main() {
float test_cases[][2] = {
{5.0, 10.0},
{7.0, 10.0},
{15.0, 30.0},
{12.0, 8.0}
};
int num_cases = sizeof(test_cases) / sizeof(test_cases[0]);
printf("Lens Focal Length Calculator<br>");
printf("============================<br>");
for (int i = 0; i < num_cases; i++) {
float image_dist = test_cases[i][0];
float object_dist = test_cases[i][1];
float focal_length = calculateFocalLength(image_dist, object_dist);
printf("Case %d: Object=%.1f, Image=%.1f, Focal Length=%.6f<br>",
i+1, object_dist, image_dist, focal_length);
}
return 0;
}
Lens Focal Length Calculator ============================ Case 1: Object=10.0, Image=5.0, Focal Length=3.333333 Case 2: Object=10.0, Image=7.0, Focal Length=4.117647 Case 3: Object=30.0, Image=15.0, Focal Length=10.000000 Case 4: Object=8.0, Image=12.0, Focal Length=4.800000
Key Points
- The lens equation 1/F = 1/O + 1/I is fundamental in optics
- Focal length is positive for converging lenses and negative for diverging lenses
- Both object and image distances must be non-zero for valid calculations
- The formula applies to both real and virtual images
Conclusion
Calculating focal length using the lens equation is essential in optics. This C program demonstrates how to implement the mathematical relationship between object distance, image distance, and focal length for practical lens calculations.
