编写程序以确定镜头的焦距。
焦距是镜片中心到主要焦点之间的距离。为了确定透镜的焦距,我们应该知道透镜与图像之间的距离(I)以及透镜与物体之间的距离(O)。在这里,我们将使用镜头方程式,也称为焦距方程式。
透镜方程为:
= +
这里,
F是焦距
我是镜头和图像之间的距离
O是镜头与物体之间的距离
例子 :
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
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;
}
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.
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.")
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.
PHP
Javascript
输出 :
Focal length of a lens is 1.92308 units .