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++
#include <iostream>
using namespace std;
float focal_length( float image_distance, float object_distance)
{
return 1 / ((1 / image_distance) + (1 / object_distance));
}
int main()
{
float image_distance = 2;
float object_distance = 50;
cout << "Focal length of a lens is "
<< focal_length(image_distance, object_distance)
<< " units ." ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static float focal_length( float image_distance,
float object_distance)
{
return 1 / (( 1 / image_distance) +
( 1 / object_distance));
}
public static void main(String[] args)
{
float image_distance = 2 ;
float object_distance = 50 ;
System.out.println( "Focal length of a lens is "
+ focal_length(image_distance, object_distance)
+ " units." );
}
}
|
Python3
def focal_length(image_distance, object_distance)
: return 1 / (( 1 / image_distance) + ( 1 / object_distance))
image_distance = 2
object_distance = 50
result = focal_length(image_distance, object_distance)
print ( "Focal length of a lens is " , result, " units." )
|
C#
using System;
class GFG {
static float focal_length( float image_distance,
float object_distance)
{
return 1 / ((1 / image_distance) +
(1 / object_distance));
}
public static void Main()
{
float image_distance = 2;
float object_distance = 50;
Console.WriteLine( "Focal length of a lens is "
+ focal_length(image_distance, object_distance)
+ " units." );
}
}
|
PHP
<?php
function focal_length( $image_distance ,
$object_distance )
{
return 1 / ((1 / $image_distance ) +
(1 / $object_distance ));
}
$image_distance = 2;
$object_distance = 50;
echo "Focal length of a lens is "
, focal_length( $image_distance ,
$object_distance )
, " units ." ;
?>
|
Javascript
<script>
function focal_length(image_distance,
object_distance)
{
return 1 / ((1 / image_distance) +
(1 / object_distance));
}
let image_distance = 2;
let object_distance = 50;
document.write( "Focal length of a lens is "
+ focal_length(image_distance, object_distance)
+ " units." );
</script>
|
OutputFocal length of a lens is 1.92308 units .
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...