# Program for focal length of a lens

Write a program to determine the focal length of a lens.

Focal length is the distance between the center of the lens to the principal foci. In order to determine the focal length of a lens we should know the distance between the lens and the image ( I ) and the distance between the lens and the object ( O ). Here, we will use the lens equation also called the focal length equation.

The lens equation is :

= +

here,

F is the focal length

I is the distance between the lens and the image

O is the distance between the lens and the object

Examples :

Input : O = 50, I = 2 Output : F = 1.92308 Input : O = 25, I = 5 Output : F = 4.16667

## C++

`// C++ program to determine ` `// the focal length of a lens ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to determine the focal length of a lens ` `float` `focal_length(` `float` `image_distance, ` `float` `object_distance) ` `{ ` ` ` `return` `1 / ((1 / image_distance) + (1 / object_distance)); ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `// variable to store the distance ` ` ` `// between the lens and the image ` ` ` `float` `image_distance = 2; ` ` ` ` ` `// variable to store the distance ` ` ` `// between the lens and the object ` ` ` `float` `object_distance = 50; ` ` ` ` ` `cout << ` `"Focal length of a lens is "` ` ` `<< focal_length(image_distance, object_distance) ` ` ` `<< ` `" units ."` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to determine ` `// the focal length of a lens ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to determine the focal ` ` ` `// length of a lens ` ` ` `static` `float` `focal_length(` `float` `image_distance, ` ` ` `float` `object_distance) ` ` ` `{ ` ` ` `return` `1` `/ ((` `1` `/ image_distance) + ` ` ` `(` `1` `/ object_distance)); ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `// variable to store the distance ` ` ` `// between the lens and the image ` ` ` `float` `image_distance = ` `2` `; ` ` ` ` ` `// variable to store the distance ` ` ` `// between the lens and the object ` ` ` `float` `object_distance = ` `50` `; ` ` ` ` ` `System.out.println(` `"Focal length of a lens is "` ` ` `+ focal_length(image_distance, object_distance) ` ` ` `+ ` `" units."` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to determine ` `# the focal length of a lens ` ` ` `# Function to determine the focal length of a lens ` `def` `focal_length(image_distance, object_distance) ` ` ` `: ` `return` `1` `/` `((` `1` `/` `image_distance) ` `+` `(` `1` `/` `object_distance)) ` ` ` `# Driver Code ` `# Variable to store the distance ` `# between the lens and the image ` `image_distance ` `=` `2` ` ` `# Variable to store the distance ` `# between the lens and the object ` `object_distance ` `=` `50` ` ` `result ` `=` `focal_length(image_distance, object_distance) ` `print` `(` `"Focal length of a lens is "` `, result, ` `" units."` `) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to determine ` `// the focal length of a lens ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to determine the focal ` ` ` `// length of a lens ` ` ` `static` `float` `focal_length(` `float` `image_distance, ` ` ` `float` `object_distance) ` ` ` `{ ` ` ` `return` `1 / ((1 / image_distance) + ` ` ` `(1 / object_distance)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `// variable to store the distance ` ` ` `// between the lens and the image ` ` ` `float` `image_distance = 2; ` ` ` ` ` `// variable to store the distance ` ` ` `// between the lens and the object ` ` ` `float` `object_distance = 50; ` ` ` ` ` `Console.WriteLine(` `"Focal length of a lens is "` ` ` `+ focal_length(image_distance, object_distance) ` ` ` `+ ` `" units."` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to determine ` `// the focal length of a lens ` `// Function to determine the ` `// focal length of a lens ` `function` `focal_length(` `$image_distance` `, ` ` ` `$object_distance` `) ` `{ ` ` ` `return` `1 / ((1 / ` `$image_distance` `) + ` ` ` `(1 / ` `$object_distance` `)); ` `} ` ` ` ` ` `// Driver Code ` ` ` ` ` `// variable to store the distance ` ` ` `// between the lens and the image ` ` ` `$image_distance` `= 2; ` ` ` ` ` `// variable to store the distance ` ` ` `// between the lens and the object ` ` ` `$object_distance` `= 50; ` ` ` ` ` `echo` `"Focal length of a lens is "` ` ` `, focal_length(` `$image_distance` `, ` ` ` `$object_distance` `) ` ` ` `, ` `" units ."` `; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output :

Focal length of a lens is 1.92308 units .

## Recommended Posts:

- Program to determine focal length of a spherical mirror
- Program to find Length of Bridge using Speed and Length of Train
- Power of a lens
- C program to find the length of a string
- Python program to print even length words in a string
- Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given
- Arc length from given Angle
- Sum of XOR of all sub-arrays of length K
- Length of longest rod that can fit into a cuboid
- Length of the Diagonal of the Octagon
- Print all sequences of given length
- Find length of period in decimal value of 1/n
- Number of K length subsequences with minimum sum
- Number of N length sequences whose product is M
- Area of hexagon with given diagonal length

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.