Programming logic question C

2

I'm learning how to program in C using André Backes' Complete and uncomplicated C language book. You have an exercise that I can not get the answer to. Follow the Statement:

  

(The exercise has been slightly changed.) Make a program that reads a worker's salary and borrowed loan amount.   If the loan amount:

     
  • For 20% greater than the salary , print: "Loan not granted."
  •   
  • Otherwise , print: "Loan Granted."
  •   

below is my code

int n1; // salário
int n2; // valor empréstimo
int resultado;
scanf ("%d %d",&n1,&n2);

resultado = n2/n1;
if (resultado <= 1.2){
    printf("emprestimo concedido");
}else{
    printf("emprestimo nao concedido");
}
return 0;
    
asked by anonymous 29.04.2017 / 03:25

3 answers

1

You can not do this right simply for a beginner. And it's a shame that a book teaches this way.

But to make it work more or less (you have rounding problems) you can use a float type. The int can not be used because monetary values are not only integer, it has decimal part for the cents. Even though they would still generate a decimal after dividing and you are unlikely to want to discard the generated decimal part.

Just to understand the logic ok, but do not do this with real monetary value.

I took advantage of and simplified the code that had a lot of things unnecessarily. Also I put good names for the variables, so do not need to comment.

#include <stdio.h>

int main(void) {
    float salario;
    float emprestimo;
    scanf ("%f %f", &salario, &emprestimo);
    printf("emprestimo%s concedido", emprestimo /salario <= 1.2 ? "" : " nao");
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
29.04.2017 / 03:41
0

Excuse the stinginess of my response ...

The code submitted by @bigown

#include <stdio.h>
int main(void) {
    float salario; // salário
    float emprestimo; // valor empréstimo
    scanf ("%f %f", &salario, &emprestimo);
    printf("emprestimo%s concedido", emprestimo /salario <= 1.2 ? "" : " nao");
}

(usually perfect, as usual) has a problem for the if the ratio is exactly equal to 1.2, referred to in the OP comment:

  

and now it worked except when the value is exactly 20% more of the salary ("should accept the loan, but it is not").

$ cc x.c -o x
$ x
100 
120
emprestimo não concedido    --> 1.2

This results from number 1.2 not having an exact representation in floats / doubles in computer: connected to its representation in binary this number is an infinite periodic decimal (if it were 1.25 there would be no problem)

Overall comparisons of real involving equalities need to predict an error ...

#define ERRO 0.0000000001
#include <stdio.h>
int main(void) {
    float salario; // salário
    float emprestimo; // valor empréstimo
    scanf ("%f %f", &salario, &emprestimo);
    printf("emprestimo%s concedido", emprestimo /salario <= 1.2+ERRO ? "" : " nao");
}
    
29.04.2017 / 14:01
0

Well, there are two problems there, the first one is

if (resultado <= 1.2){

This is because if it is 20% higher than the salary value, it will print, in case you would have to use

if(resultado <1.2){

This way while the result is less than 1.2 it will print "granted loan". The second problem is that you used int, so you do not get decimal places. Just swap int for float.

float n1; // salário
float n2; // valor empréstimo
float resultado;
scanf ("%f %f",&n1,&n2);

resultado = n2/n1;
if (resultado < 1.2){
    printf("emprestimo concedido");
}else{
    printf("emprestimo nao concedido");
}
return 0;

Hugs

    
29.04.2017 / 15:22