What is the error of this algorithm? [closed]

1

I would like to see if you could help me find the error in this algorithm, the exercise says to calculate the discount: if m> = 300 the discount is 25% - if 150

asked by Gustavo Torres 23.06.2017 в 18:34
source

2 answers

3

As I mentioned, the opening and closing keys of each block were missing, but you also had the second condition if ((monto<=150 && monto<300)) that I just corrected you wrong. Some comments.

  • C and C ++ do not initialize the values of the variables, so it is always a good practice to initialize them after declaring them: int monto=0,desc1=0,calc1=0,desc2=0,calc2=0;

  • I added a "cast" to int in the calculation of the discount: int (monto*0.25) to avoid warnings during the compilation and in passing I commented that you are losing the decimal part of the calculation when using int but I I imagine is what the exercise asks for.

  • As already commented @Trauma the final condition is unnecessary

  • The code:

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    int main () 
    {
        int monto=0,desc1=0,calc1=0,desc2=0,calc2=0;
    
        printf("Ingrese el monto: ");
        scanf("%d",&monto);
    
        if (monto>300) {
            calc1=int (monto*0.25);
            desc1=monto-calc1;
            printf("El descuento es de: %d $",calc1);
            printf("\nEL total a pagar es de: %d $",desc1);
        }
        else if ((monto>=150 && monto<300)) {
             calc2=int (monto*0.2);
             desc2=monto-calc2;
             printf("El descuento es de: %d $",calc2);
             printf("\nEl total a pagar es de: %d $",desc2);
        } else {
            printf("No hay descuento");
            printf("\nEl total a pagar es de: %d $",monto);
        }
    }
    
        
    answered by 23.06.2017 / 19:09
    source
    2

    It should be like this

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    int main () {
        int monto,desc1,calc1,desc2,calc2;
    
        printf("Ingrese el monto: ");
        scanf("%d",&monto);
        if (monto>300){
           calc1=monto*0.25;
           desc1=monto-calc1;
           printf("El descuento es de: %d $",calc1);
           printf("\nEL total a pagar es de: %d $",desc1);
        }
        else if ((monto<=150 && monto<300)){ 
           calc2=monto*0.2;
           desc2=monto-calc2;
           printf("El descuento es de: %d $",calc2);
           printf("\nEl total a pagar es de: %d $",desc2);
        }
        else (monto<150){
           printf("No hay descuento");
           printf("\nEl total a pagar es de: %d $",monto);
        }
    }
    
        
    answered by 23.06.2017 в 18:46