0

So id like to create a tax calculator in c# that does the following:

Example 1:

Input:

1200.0 - amount loaned

10 - tax in percentage

3 - number of years in debt 

Output:

    Growth of debt throughout 3 years:

    1200.0

    1320.0

    1452.0

    1597.2

So far i have this:

static int Mostra(int n, int p, int q)
    {
        int res = n + ((n * (p / 100)) * q);

        Console.WriteLine("Crescimento da dívida ao longo de " + q.ToString() + " anos:");

    }


    static bool Validar(string s, out int i)
    {
        bool res = int.TryParse(s, out i);
        if (!res)
            i = 0;
        return res;

    }


    static void Main(string[] args)
    {
        Console.WriteLine("Insira o valor do seu empréstimo em euros:");
        string s1 = Console.ReadLine();
        Console.WriteLine("Insira o valor correspondente à taxa de juro acordada:");
        string s2 = Console.ReadLine();
        Console.WriteLine("Insira o número de anos em que a dívida não foi paga:");
        string s3 = Console.ReadLine();

        string[] valores = new string[p];


        int n;
        int p;
        int q;


        if (Validar(s1, out n) && Validar(s2, out p) && Validar(s3, out q))
        {
            if (n >= 0 && p >= 0 && q >= 0 && p <= 100)
            {
                //Mostra(n, p, q);
            }
            else
                Console.WriteLine("O valor introduzido é inválido.");

        }

        Console.ReadKey();
    }

Keep in mind that this is a Console Application. My issue here is how to display the debt over the years instead of the final result. And also how to display it in decimals as shown in the example. ifyou could help me id be very happy :D

Deny51853
  • 3
  • 3
  • 1
    looks like you are almost there. you will need to uncomment the call to the Mostra function, and output the value it returns. for number formatting, look into the various overloads of the `ToString` method in documentation. Use the debugger to get the sequence right, if you're not sure where to put your Console.WriteLine calls. – Cee McSharpface Nov 01 '18 at 17:28

2 Answers2

0

If you want to display the result as a decimal, you should use the decimal data type not the int data type.

in your initial example though you also are adding interest per year, though your calculation is flat interest over the entire time period, the function should look more like

static int Mostra(decimal n, decimal p, int q)
{
    Console.WriteLine("Crescimento da dívida ao longo de " + q.ToString() + " anos:");
    decimal res = n;
    for (int i = 1; i <= q; i++)
    {
        res+= res * (p / 100);
        Console.WriteLine(res);
    }

}

you would also need to edit the Validar method to handle converting decimals using decimal.TryParse as the interest rate and amount should be decimals to handle decimal based calculations - the Year can still be an integer though

Gibbon
  • 2,633
  • 1
  • 13
  • 19
  • I'm having an issue that says not all code paths return a value; how do i fix that? – Deny51853 Nov 01 '18 at 17:45
  • Ahh yeah, sorry. Technically since the function is doing everything in line `static int Mostra` that could be changed to `static void Mostra` as it doesnt return anything :) – Gibbon Nov 01 '18 at 17:49
  • Check with debug if the input are formating to decimal. I guess the another anwers is better ... when you use a double value your output have decimals unity. In function Mostra signature you also need to change to double to returns a correct value. and use the output of that to show in output after method – Guilherme Marques Nov 01 '18 at 17:54
  • 1
    @GuilhermeMarques When doing things currency / money related you should generally always use decimals and not doubles - also the other answer on this post has truncation errors and also calculates the interest incorrectly, so the other answer is definitely not better as a solution :) – Gibbon Nov 01 '18 at 17:59
0

Here ya go, there were a couple things I noticed that were a little wonky. You'll want to change some of the variables in Validar from ints to doubles... otherwise if you want your input to be x.0, your code won't handle the .0 since you're looking for an int in your Validar method. I've fixed some of the code so that any ints you put in will work and you'll get the output in the format that you're looking for.

using System;

    public class Program
    {
     static void Mostra(double n, double p, double q)
     {
    //double res = n + ((n * (p / 100)) * q);

    Console.WriteLine("Crescimento da dívida ao longo de " + q.ToString() + " anos: \n");

    //Loop that increments each year for q number of years
    for (int x = 0; x < q + 1; x++)
    {
        double res = 0;
        res = n + ((n * (p / 100)) * x);
        Console.WriteLine(res);
    }

}


static double Validar(string s)
{
    int x = int.Parse(s);
    double res = (double)(x);

    return res;

}


public static void Main(string[] args)
{
    Console.WriteLine("Insira o valor do seu empréstimo em euros:");
    string s1 = Console.ReadLine();
    Console.WriteLine("Insira o valor correspondente à taxa de juro acordada:");
    string s2 = Console.ReadLine();
    Console.WriteLine("Insira o número de anos em que a dívida não foi paga:");
    string s3 = Console.ReadLine();

    double n = Validar(s1);
    double p = Validar(s2);
    double q = Validar(s3);



    if (n >= 0 && p >= 0 && q >= 0 && p <= 100)
    {
        Mostra(n, p, q);
    }
    else
        Console.WriteLine("O valor introduzido é inválido.");

}
}
SEMiller
  • 19
  • 3
  • This has some issues, the `int x = int.Parse(s)` will truncate any initial decimal based values for example, if the user enters in 5.5 for interest. it also has a slight issue where the interest is calculated as its calculating flat interest and not compound interest like depicted in the initial example. also, you should use decimal for anything potentially money related - reasons explained here - https://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when (thought I doubt it would matter in this case, but just for future :D) – Gibbon Nov 01 '18 at 18:04
  • @Gibbon thanks for your response, I agree with all of the above lol. That’s why I recommended OP handle doubles in methods rather than ints because the decimals will not be handled. To be honest I just didn’t want to rewrite the entire code for OP, just got it to a working point so they can proceed. Thanks again though, definitely all very valid points! – SEMiller Nov 01 '18 at 18:24