Thatonepeep
Thatonepeep

Reputation: 17

How to count down large payments with a loop with c#

I am trying to make my own countdown of the national debt using loop statements. I am still very new to programming so I am having a hard time figuring out how to make this loop. I want to display how long it will take to pay off the debt if $100,000,000 was paid each day. I would like to display the numbers of days and years, but my priority right now is the loop. So far I have this

namespace Count_Down_The_National_Debt
{
    class Program
    {
        static void Main(string[] args)
        {
            //step 1
            // use 19.9 trillion dollars 
            long national_debt = 19900000000000;
            // keep track of how many days and it years it takes to pay off the US National Debt
            int day = 0, years = 0;

I looked back at a simpler program I made that used loops, but it was for counting up and not down. This is what I made based on my past program;

int j = 0;
            Console.WriteLine("By 100 Millions ");
            while (national_debt > 0)
            {
                Console.WriteLine(j.ToString());
                j = j - 100000000;
            }

However, it just runs in an infinite loop until I close my program. Will I need a break of some kind, or do I need to completely rewrite my loop?

Upvotes: 1

Views: 734

Answers (3)

maccettura
maccettura

Reputation: 10818

The accepted answer isn't very good in my opinion. A while loop that is dependent on incrementing/decrementing numbers usually means you should not use a while loop. A better way to handle this is to use a for loop (and to put it in a method so its resuable)

private static void CalculatePayDownDays(long payPerDay, long nationalDebt)
{
    long days = (long)Math.Ceiling((decimal)nationalDebt / payPerDay);

    for (long i = 1; i < days + 1; i++)
    {
        nationalDebt -= payPerDay;
        Console.WriteLine("Day {0}, New Balance: {1:c}", i, nationalDebt);
    }
}

Fiddle here

EDIT

For the pedantic among us, I slightly modified the method.

Upvotes: 0

kriskalish
kriskalish

Reputation: 145

In general any type of loop is not a computationally efficient way to solve this problem because it can be solved with arithmetic as demonstrated later. I assume that you trying to solve it with a while loop as you suggested in the question purely as a learning exercise in while loops. With that said, you will need your loop condition to be dependent on the value you are modifying. In your case you could make it dependent on j, and initialize j to the start value of national_debt. Here is a more complete example using more verbose variable names:

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            long initial_national_debt = 19900000000000;
            long national_debt = initial_national_debt;
            long payment_size = 100000000;

            while (national_debt > 0)
            {
                Console.WriteLine(national_debt);
                national_debt -= payment_size;
            }
        }
    }
}

The answer proposed by @maccettura fails to properly calculate the number of days it will take to pay off the debt. For example if the debt is $100 and the payment increment is $200, it takes one day to pay off the debt. However truncating integer division says that 100 / 200 is equal to zero. It clearly does not take zero days to pay off the debt in this scenario.

As I hinted to earlier, this problem can be solved directly. See the following snippet:

using System;
namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            long national_debt = 19900000000000;
            long payment_size = 100000000;

            long total_days_to_pay = national_debt / payment_size;

            // if the debt is not evenly divisible by the payment size, then there is one partial day of debt
            if (national_debt % payment_size != 0)
                total_days_to_pay++;

            Console.WriteLine("It takes " + total_days_to_pay + " total days to pay off the national debt.");
            Console.WriteLine("That's " + total_days_to_pay / 365 + " years and " + total_days_to_pay % 365 + " days !");
        }
    }
}

Upvotes: 1

frcnav10
frcnav10

Reputation: 13

For starters, I would actually use a for loop which can be able to both increment and decrement. For the incrementing for loop, the C# syntax would like this:

for (int i = 0; i < N; i++)
{
    // Put your logic here
}

However, the decrementing loop is a little different. Using your example:

for(long d = national_debt; d > 0; d--)
{
    // Put your logic here
}

Hope that this can help you to understand, and potentially lead you to solve your problem.

Upvotes: 1

Related Questions