Loops – Physics – Problem #4
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
Write a C Program to evaluate the series f(x) = 1 - 2x/1! + 3x 2 /2! - 4x 3 /3! + 5x 4 /4! - …. for 0 < x ≤ 1 (x is a floating point number) up to a given integer number n (n > 0) of terms. Save the file as A05_<Roll Number>.c (example A05_21AG10002.c). Build, Run for and test it for the given data as well as your own data. Then upload the .c file for the Assignment. [20 Marks: 5 marks for intermediate submission, 15 marks for final submission] In particular, you will do the following: 1. Read in the value of x and an integer n and print them in a single line depicting which is x and which is n 2. If any one of the values entered is invalid, that is, either x does not satisfy 0 < x ≤ 1, or n is ≤ 0, print a message saying invalid input and ask the user to enter BOTH the values again, and read the values again (must use a while loop) and print them 3. Continue the above two steps until values entered satisfy both 0 < x ≤ 1 and n is positive 4. Now compute the value of f(x) up to n terms. Print the interim values of sum and term in every iteration inside the loop and then the final values at the end. Do not use any mathematical function like pow, etc. Test Data: a) 0.6 10 b) 0.97 25 c) 1.3 12 followed by 0.5 0 followed by 1.0 20