Logic doubt of programming C

Asked

Viewed 378 times

2

I’m learning to code in C using the book Linguagem C - Completa e mplicada by André Backes. There is an exercise that I cannot get the answer right. Follow the Statement:

(The exercise has been slightly modified) Make a program that reads a worker’s salary and loan amount requested. If the loan value:

  • For 20 % higher than salary, print: "Loan not granted."
  • Otherwise, print: "Loan granted."

follows below 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;

3 answers

2


There’s no way to make this right for a beginner. And it’s a shame that a book teaches this way.

But to make it work more or less (has rounding problems) can use a type float. The int cannot be used because monetary values are not only integers, have decimal part for pennies. Even if they were still would generate a decimal after dividing and it is unlikely that you 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 and simplified the code that had a lot to do without need. In addition I put good names for variables, so do not need to put 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");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Wow, I forgot this detail: decimal part :/ and use float or double for variable result. Anyway the program does not run correctly. You can make it work just by using if or switch?

  • I had to declare all variables as float and now it worked except when the amount is exactly 20% more than the salary ("should accept the loan but is not").

  • 1

    It is there showing that it works. I see no reason to use if in this case, even less with switch. Like I said, use float for monetary value does not work even.

  • Yeah, I tried using if because the exercise is in the chapter of the book that teaches Conditional Control Command. Anyway, thanks for the help!

0

I’m sorry for the nit of my answer...

The code presented by @Maniero

#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");
}

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

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

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

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

Overall real comparisons 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");
}

0

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

if (resultado <= 1.2){

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

if(resultado <1.2){

So as long as the result is less than 1.2 it will print "loan granted". The second problem is that you used int, so you don’t get decimals. 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.