0

Below is my current output following my current source code. According to my assignment, after it writes the calculation for when nCount=4, it should go to a new line like in the picture of the sample program. I'm not sure how to do that kind of formatting, also I need to show just commas. when I use {0:n} it shows .00 at the end of the number, and I don't want that.

Code:

static void CalculateK(uint nCount)
{
  uint kSum = 0;
  double kCalc = 0;
  double k = 1;
  for(int j=1;j<=nCount;j++)
  {
    kCalc = Convert.ToUInt32(Math.Ceiling((1000) * Math.Pow(Math.E, -k / 20)));
    fileOut.Write("{0,2}. {1,3} ", j, kCalc);
    kSum += Convert.ToUInt32(kCalc);
    k++;
  }
  fileOut.WriteLine();
  fileOut.WriteLine("Sum = {0,5:g}",kSum);
}

Output:

Current output

Final Output wanted

Huy Nguyen
  • 2,025
  • 3
  • 25
  • 37
  • 1
    Props to you for actually trying to solve your homework unlike others that have asked here. :) – jegtugado Aug 26 '16 at 03:05
  • thanks lol. this is the last part i need, was never taught it in class. we learned the formatting codes, currency, float, number, etc. but I've never seen how to make it return to a new line like he wants. – Jonathan Butler Aug 26 '16 at 03:08
  • Just do `string.Format("{0:n0}", 9876);` it will leave no digits after the decimal point. Gotten from [.NET String.Format() to add commas in thousands place for a number](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number). In your case, change `fileOut.WriteLine("Sum = {0,5:g}",kSum);` to `fileOut.WriteLine("Sum = " + string.Format("{0:n0}",kSum));` – Keyur PATEL Aug 26 '16 at 03:26

1 Answers1

0

Okay here's the solution.. You're so close! I modified this to a console project. Change as necessary.

    static void Main(string[] args)
    {
        do
        {
            Console.Write("Input a number: ");
            uint input = uint.Parse(Console.ReadLine());
            CalculateK(input);
            Console.ReadLine();
            Console.Clear();
        } while (true);
    }

    static void CalculateK(uint nCount)
    {
        uint kSum = 0;
        double kCalc = 0;
        double k = 1;
        for (int j = 1; j <= nCount; j++)
        {
            kCalc = Convert.ToUInt32(Math.Ceiling((1000) * Math.Pow(Math.E, -k / 20)));
            Console.Write("{0,2}. {1,3} ", j, kCalc);
            if(j % 4 == 0)
            {
                Console.WriteLine(); // If the counter is divisible by 4 then add a new line.
            }
            kSum += Convert.ToUInt32(kCalc);
            k++;
        }
        Console.WriteLine();
        Console.WriteLine("Sum = {0}", kSum.ToString("0,0")); // Use the ToString method and add your desired format instead.
    }
jegtugado
  • 5,081
  • 1
  • 12
  • 35