8240. Function 2

 

Implement a function f(x) =  + 2 * x + sin(x).

 

Input. Each line contains one real number x.

 

Output. For each value x print in a separate line the value of the function f(x) with four digits after the decimal point.

 

Sample input

Sample output

2.234

10.23

56.1

23.2651

6.7507

22.9375

119.2562

50.3973

 

 

SOLUTION

functions

 

Algorithm analysis

Implement a function f(x). Read the input data till the end of file.

 

Algorithm realization

Implement a function f.

 

double f(double x)

{

  return sqrt(x) + 2 * x + sin(x);

}

 

The main part of the program. Read the input data till the end of file.

 

while (scanf("%lf", &x) == 1)

  printf("%.4lf\n", f(x));

 

Java realization

 

import java.util.*;

 

public class Main

{

  static double f(double x)

  {

    return Math.sqrt(x) + 2 * x  + Math.sin(x);

  }

 

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    while(con.hasNext())

    {

      double x = con.nextDouble();

      System.out.printf("%.4f\n",f(x));

    }

    con.close();

  }

}  

 

Python realization

 

import sys

import math

 

def f(x):

  return math.sqrt(x) + 2 * x + math.sin(x)

 

for x in sys.stdin:

  x = float(x)

  print(f(x))