Implement the function f(x) = x + sin(x).
Input. One real number x.
Output. Print the value of the function f(x) with 4 decimal digits.
Sample input 1 |
Sample output 1 |
1.1234 |
2.0250 |
|
|
Sample input 2 |
Sample output 2 |
-3.1415 |
-3.1416 |
functions
Implement the function f(x).
Algorithm
implementation
Define the function f.
double f(double
x)
{
return x + sin(x);
}
The main part of the program. Read the input value x.
scanf("%lf",&x);
Compute and print the value
of the function f(x).
y = f(x);
printf("%.4lf\n",y);
Java implementation
import java.util.*;
public class Main
{
static double
f(double x)
{
return x + Math.sin(x);
}
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
double x = con.nextDouble();
double y = f(x);
System.out.println(y);
con.close();
}
}
Python implementation
import math
Define the function f.
def f(x):
return x +
math.sin(x)
The main part of the program. Read the input value x.
x = float(input())
Compute and print the value
of the function f(x).
print(f(x))
Python implementation – lambda function
import math
Define the function f.
f = lambda x: x + math.sin(x)
The main part of the program. Read the input value x.
x = float(input())
Compute and print the value
of the function f(x).
print(f(x))