Banknotes and coins C#

Asked

Viewed 609 times

1

I’m having an error in an algorithm I’m implementing in the c# language on the JUDGE website online, Banknotes and coins And I don’t really know what it is, but I believe it’s about the calculations, it gives a rounding error, I realized this by putting several writeln, after the calculation, just to keep up with the variations, with some inputs works perfectly. only does not work with this entry 576.43 and others. it works all right, but the last line of data output gives 2 1c coins, being that are 3 coins.. I don’t know how to explain it, but I hope you understand my logic

using System;

class MainClass {
  public static void Main (string[] args) {
    double n1,n2,n3,n4,n5,n6,m1,m2,m3,m4,m5,m6;
    double a = double.Parse(Console.ReadLine());

    n1 = (a - a % 100)/100;
    a = a % 100;

    n2 = (a - a % 50)/50;
    a = a % 50;

    n3 = (a - a % 20)/20;
    a = a % 20;

    n4 = (a - a % 10)/10;
    a = a % 10;

    n5 = (a - a % 5)/5;
    a = a % 5;

    n6 = (a - a % 2)/2;
    a = a % 2;

    m1 = (a - a % 1)/1;
    a = a % 1;

    m2 = (a - a % 0.50)/0.50;
    a = a % 0.50;

    m3 = (a - a % 0.25)/0.25;
    a = a % 0.25;

    m4 = (a - a % 0.10)/0.10;
    a = a % 0.10;

    m5 = (a - a % 0.05)/0.05;   
    a = a % 0.05;

    m6 = (a - a % 0.01)/0.01;

    Console.WriteLine("NOTAS:");
    Console.WriteLine($"{n1} nota(s) de R$ 100.00");
    Console.WriteLine($"{n2} nota(s) de R$ 50.00");
    Console.WriteLine($"{n3} nota(s) de R$ 20.00");
    Console.WriteLine($"{n4} nota(s) de R$ 10.00");
    Console.WriteLine($"{n5} nota(s) de R$ 5.00");
    Console.WriteLine($"{n6} nota(s) de R$ 2.00");
    Console.WriteLine("MOEDAS:");
    Console.WriteLine($"{m1} moeda(s) de R$ 1.00");
    Console.WriteLine($"{m2} moeda(s) de R$ 0.50");
    Console.WriteLine($"{m3} moeda(s) de R$ 0.25");
    Console.WriteLine($"{m4} moeda(s) de R$ 0.10");
    Console.WriteLine($"{m5} moeda(s) de R$ 0.05");
    Console.WriteLine($"{m6} moeda(s) de R$ 0.01");    
  }
}

2 answers

1


Let’s assume that the price of a product is R$ 4.99 and you want to know the total value of 17 items of that product.

Below we have the code used to make the calculation using the type float for the product price and of course you will expect the result of R $ 84,83 since 4.99 17 = 84.83. Right?

float preco = 4.99f;

int quantidade = 17;

float total = preco * quantidade;

Console.WriteLine(total);

Console.ReadLine();

You expected 84.83, but the result obtained was 84.82999. To get the right result, you must use the type decimal.

Completion

For any calculation involving money or finance, the type Decimal should always be used. Only this type has the appropriate precision to avoid critical rounding errors.

Source: Using floating point in calculations

Use the suffix M or m for the decimal type.

decimal valor = 0.01m;
  • Thank you friend(a) for your reply.. but how can I use this suffix in my case? (if you are available, I would appreciate an example) is because in my program I’m doing an assignment with calculus, and this example got a little distant, but I’ll take a better look at how it uses the decimal, anyway already gave a light!

  • m2 = (a - a % 0.50m) / 0.50m;
 a = a % 0.50m; etc.. But your code is too repetitive, you can use arrays and cycles to simplify / shorten..

  • Yeah, I just haven’t learned yet.. rsrs no c# no

  • I really liked your solution!! was much more practical, thank you very much!!!

-1

Good evening, the syntax with passage of arguments to Console.WriteLine() is Console.WriteLine("{0}{1}{2}", SUA_VAR1, SUA_VAR2, SUA_VAR3) .... because the method contains a parameter that accepts args[] or several variables.

Console.WriteLine($"{m6} moeda(s) de R$ 0.01");

Remove the $, already the {m6} must be your index variable 0 {0}. Put a comma at the end and pass the arguments to function.

When you want to do mathematical operations with two decimal places as for example in coins you should make a arrendomento because the MOD or in the case % using double has very large 64 bit values that we don’t need in the case, use the Math.Round() to round a value to the nearest integer or to the specified number of decimals. There is and you don’t need to calculate the M6 because it is already the rest of the value itself.

Example:

    int n1,n2,n3,n4,n5,n6,m1,m2,m3,m4,m5,m6;
    double a = 576.43f;
    a = Math.Round(a, 2);

    n1 = (int)(a - a % 100)/100;
    a = Math.Round(a % 100, 2);
    n2 = (int)(a - a % 50)/50;
    a = Math.Round(a % 50, 2);
    n3 = (int)(a - a % 20)/20;
    a = Math.Round(a % 20, 2);
    n4 = (int)(a - a % 10)/10;
    a = Math.Round(a % 10, 2);
    n5 = (int)(a - a % 5)/5;
    a = Math.Round(a % 5, 2);
    n6 = (int)(a - a % 2)/2;
    a = Math.Round(a % 2, 2);
    m1 = (int)(a - a % 1)/1;
    a = Math.Round(a % 1, 2);
    m2 = Convert.ToInt32((a - a % 0.50)/0.50);
    a = Math.Round(a % 0.50, 2);
    m3 = Convert.ToInt32((a - a % 0.25)/0.25);
    a = Math.Round(a % 0.25, 2);
    m4 = Convert.ToInt32((a - a % 0.10)/0.10);
    a = Math.Round(a % 0.10, 2);
    m5 = Convert.ToInt32((a - a % 0.05)/0.05);   
    a = Math.Round(a % 0.05, 2);
    m6 = Convert.ToInt32(a/0.01);

    Console.WriteLine("NOTAS:");
    Console.WriteLine("{0} nota(s) de R$ 100.00", n1);
    Console.WriteLine("{0} nota(s) de R$ 50.00", n2);
    Console.WriteLine("{0} nota(s) de R$ 20.00", n3);
    Console.WriteLine("{0} nota(s) de R$ 10.00", n4);
    Console.WriteLine("{0} nota(s) de R$ 5.00", n5);
    Console.WriteLine("{0} nota(s) de R$ 2.00", n6);
    Console.WriteLine("MOEDAS:");
    Console.WriteLine("{0} moeda(s) de R$ 1.00", m1);
    Console.WriteLine("{0} moeda(s) de R$ 0.50", m2);
    Console.WriteLine("{0} moeda(s) de R$ 0.25", m3);
    Console.WriteLine("{0} moeda(s) de R$ 0.10", m4);
    Console.WriteLine("{0} moeda(s) de R$ 0.05", m5);
    Console.WriteLine("{0} moeda(s) de R$ 0.01", m6);
  • Thanks friend! however, it hasn’t solved my problem yet. : / continues with the same problem;

  • I think it’s a problem with rounding in the third variable (N3)he assigns a number full of numbers after the comma, an example is if I put 576.43 in the input, it should get there 6.43, But when you get there, something miraculous happens that I don’t understand and attribute 6,499,999 to something like that, you know? that’s why it makes a mistake, I think;

  • Had not seen this detail, you should use the Math.Round() for rounding, take a look at my answer again.

  • It looks like it worked! I don’t know if Uri online will accept rsrs yet I had tried to use the Math round command, but I hadn’t succeeded.. thank you very much!

  • It worked, beast!(in reference the site also accepted) ksks

Browser other questions tagged

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