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

Modulo Operator don't work in my calculator app in C

So I tried to program a little calculator in C to do addition, subtraction , multiplication , division and modulus everything works perfectly except the modulus operator and I don’t know why so here is my code and modulus is inside case 5 in the switch statement :

#include <stdio.h>

void main(){

    float n1=0.0;
    float n2=0.0;
    float n3=0.0;
    int choix=0;
        printf("\nRentrer le Premier Chiffre:");
        scanf("%f" , &n1);
        
        printf("\nRentrer le Deuxieme Chiffre:");
        scanf("%f" , &n2);

    printf("\n\tEcrire 1 pour une addition\n\tEcrire 2 pour une soustraction\n\tEcrire 3 pour une multiplication\n\tEcrire 4 pour une division\n\tEcrire 5 pour modulus:");
    scanf("%d" , &choix);

    switch (choix)
    {
        case 1: 
        n3=n1+n2;
        printf("L'addition de %.2f+%.2f=%.2f" , n1 , n2, n3);
        break;

        case 2: 
        n3=n1-n2;
        printf("La soustraction %.2f-%.2f=%.2f" , n1 , n2, n3);
        break;

        case 3:
        n3=n1*n2;
        printf("La multiplication %.2f*%.2f=%.2f" , n1 , n2, n3);
        break;

        case 4:
        n3=n1/n2;
        printf("La division %.2f/%.2f=%.2f" , n1 , n2, n3);
        break;

        case 5:
        n3=n1%n2;
        printf("Le modulus de %.2f et %.2f=%.2f" , n1 , n2, n3);
        break;

        default: 
        printf("Entrer un choix valide!");
        break;

    }

}

It give me this error :

In function 'main':
calculatrice.c:41:8: error: invalid operands to binary % (have 'float' and 'float')
   n3=n1%n2;
    ^

So if someone could help me or lead me where the problem is it would be gladly appreciated 🙂

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 :

Check this out:

https://www.geeksforgeeks.org/modulus-two-float-double-numbers/

Also I think you have it as =% instead of %=

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