C Programming – HackerRank Solution | Sine Series |

(HackerRank) Write a Modular C Programming code to solve Sine Series, Sin x is a series of sin function of trigonometry; it can expand up to an infinite number of term

Sin x is a series of sin function of trigonometry; it can expand up to infinite number of term. Through this series, we can find out value of sin x at any radian value of sin x graph.

image

Sin(x) Series:

image

Steps:

First the computer reads the value of x and number of terms from the user.

Convert x to radian value x=x*(3.1415/180)

Then the value of sin(x) is calculated.

Finally the value of sin(x) is printed.

Input Format

Two input values seperated by white space.

first number is degree.

second numbe is number of terms in series.

Constraints

0 <= degree < 360

1<= terms < 100

Output Format

computed sine series answer.

Sample Input 0

45 5

Sample Output 0

Sine value of 45.000 is: 0.707

Sample Input 1

120 0

Sample Output 1

Invalid input.

Sample Input 2

270 35

Sample Output 2

Sine value of 270.000 is: -1.000

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

float factorial(float ter)
   {
       float i,f=1;
       for(i=1;i<=ter;i++)
       {
           f=f*i;
       }
       return f;
   }
void sine_x(float x,float ter)
{
    float copy,i,fact=1;
    double fin=0;
    copy=x;
    x=x*(3.1415/180);
    for(i=0;i<ter;i++)
 { 
fact=factorial((2*i)+1); 
fin=fin+((pow(-1,i))*(pow(x,(2*i)+1)))/fact; 
} 
printf("Sine value of %0.3f is: %0.3lf",copy,fin); 
} 
int main()
 { 
float x,ter; 
scanf("%f%f",&x,&ter); 
if((x>=0&&x<360)&&(ter>=1&&ter<100))
        sine_x(x,ter);
    else
    {
        printf("Invalid input.\n");
        exit(0);
    }
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

45 5
Your Output (stdout)

Sine value of 45.000 is: 0.707
Expected Output

Sine value of 45.000 is: 0.707

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!