C Exercises: Print out the corresponding value of sin(1/x) using 4-decimal places
Calculate and display sin(1x)\sin(\frac{1}{x})sin(x1) for a real number xxx
Write a C program that accepts a real number x and prints out the corresponding value of sin(1/x) using 4-decimal places.
Note: Use 4-decimal places.
Test data and expected output:
Input value of x:
Value of sin(1/x) is 0.8415
Sample Solution:
C Code:
#include <stdio.h>
#include <math.h>
int main() {
double x, tval;
// Prompt user for input
printf("Input value of x: \n");
// Read the input value
scanf("%lf", &x);
if (x != 0.0) {
// Calculate sin(1/x) if x is not zero
tval = sin(1/x);
printf("Value of sin(1/x) is %0.4lf\n", tval);
} else {
// Display error message if x is zero
printf("Value of x should not be zero.");
}
return 0;
}
Sample Output:
Input value of x: Value of sin(1/x) is 0.9995
Flowchart:
For more Practice: Solve these Related Problems:
- Write a C program to compute sin(1/x) while handling the division-by-zero case.
- Write a C program to calculate sin(1/x) for a series of values and display the results in a table.
- Write a C program to compute sin(1/x) using a Taylor series expansion for increased precision.
- Write a C program to calculate both sin(1/x) and cos(1/x) for a given input and display both results.
Go to:
PREV :Enumerate and display integer values for the days of the week.
NEXT : Compute the sum of the digits of an integer less than 500.
C programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.