Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

if statement doesn't work on a triangle type function

I’m a complete newbie, so please excuse me.
I tried using online compiler but they unresponsive, and I get no return value (or return 0 for whatever I enter)

I tried to write a function that check if triangle is right, isosceles or both, and return 1,2,3 respectively, all other cases should return 0.

int main() {
    int TriangleType(unsigned angle1, unsigned angle2) {
        unsigned angleSum = angle1 + angle2;
        if (angleSum >= 180) {
            return 0;
        } 
    
        /* if triangle is right ---> */ 
        if (angle1==90 || angle2==90 || angleSum==90) {
            /*if it is also  an isosceles --->*/
            if (angle2==45 || angle1==45) {
                return 3;
            }  
            return 1;
        }
           
        /*check if it only a isosceles*/
        if (angle1==(180-angle2)/2 || 
            angle2== (180-angle1)/2 || 
            angle1==angle2) {
            return 2;
        } 
                
        return 0;
    }
    
    TriangleType(110, 111);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First, don’t try to use nested functions in C. Pulling that function out of main.

int TriangleType(unsigned angle1, unsigned angle2) {
    unsigned angleSum = angle1 + angle2;
    if (angleSum >= 180) {
        return 0;
    } 

    /* if triangle is right ---> */ 
    if (angle1==90 || angle2==90 || angleSum==90) {
        /*if it is also  an isosceles --->*/
        if (angle2==45 || angle1==45) {
            return 3;
        }  
        return 1;
    }
       
    /*check if it only a isosceles*/
    if (angle1==(180-angle2)/2 || 
        angle2== (180-angle1)/2 || 
        angle1==angle2) {
        return 2;
    } 
            
    return 0;
}

int main() {  
    TriangleType(110, 111);
}

Second, this doesn’t do anything with the return value from the function, so of course you see no output.

int main(void) {  
    switch (TriangleType(110, 111)) {
        case 1:
        printf("Right triangle\n");
        break;
        case 2:
        printf("Isosceles triangle\n");
        break;
        case 3:
        printf("Both types\n");
        break;
        default:
        printf("None of the above\n");
    }

    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading