Anik Shahriar
Anik Shahriar

Reputation: 151

Number doesn't print in expected way

This is my program on 'Sin Calculator'

#include <stdio.h>
#include <math.h>
#define PI 3.1416

void fraction(double x, int y);
void print(double sx, int x, int y, int s);
void scal();

int main(){

    scal();
    return 0;
}

void fraction(double x, int y){

    int b = 100, a;
    a = x * 100;

    while ((a % 2 != 0) || (a % 5 != 0)){

        if (a % 2 == 0){

            a /= 2;
            b /= 2;
        }

        else if (a % 5 == 0){

            a /= 5;
            b /= 5;
        }

    }

    print(x, a, b, y);

}

void print(double sx, int x, int y, int s){

    printf (" Sin(%d) = %d/%d || %.2lf\n",s,x,y,sx);
    scal();
}

void scal(){

    double sine, sinx;
    int x, a, b;

    printf ("\n Sin(X) : ");
    scanf ("%d",&x);

    sinx = x * (PI / 180);
    sine = sin(sinx);

    fraction(sine, x);

}

I don't get any errors. But when i run it, though the variable 'a' of fraction function can be divided by 5 or 2, it doesn't do it. As a result I get the whole value of 'a'. For example, the value of Sin30 degrees is 0.50, so multiplying it with 100 makes 'a' 50. 50 is dividable by 2 and 5. But in fraction function, it seems that 'a' doesn't get divided there. As a result in 'print' function, i get '50/100' instead of '1/2'. Why is this happening? And also when i enter somethin like Sin23, the program doesn't finish. It stops. What's the problem?

Upvotes: 1

Views: 26

Answers (1)

A.S.H
A.S.H

Reputation: 29352

while ((a % 2 != 0) || (a % 5 != 0))

this test fails for the value of 50 or any integer that is multiple of both 2 an 5. Therefore the while loop will not be entered and no division will occur, so a conserves its value.

maybe your intent was the inverse of the condition:

while ((a % 2 == 0) || (a % 5 == 0))

Upvotes: 1

Related Questions