How to align the output as shown in screenshot - C#?

Rajoli Hari Krishna 801 Reputation points
2022-04-30T13:09:44.44+00:00

As a Newbie to C#, I have some code in C#.

The Program is a kind of Daily Money Saving Algorithm!

I'm struggling to produce the output in some alignment way as shown in below image:

gqjZ5.png

Code I have written:

CSharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RemainingDaysCalculation
{
    internal class DailyMoneySavingMultiplier
    {
        public static void Main()
        {
            DateTime currentDate = DateTime.Now;
            int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;
            int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.
            int finisheddaysCount = daysInYear - daysLeftInYear;
            Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);
            Console.WriteLine("finishedDaysCount is {0}",finisheddaysCount);
            int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount -1));
            Console.WriteLine(savings);

            //Case 2:
            Console.WriteLine("_________________________________");
            Console.WriteLine(" Day No || Daily Saving || Total Saved");
            for (int i=1; i <= daysInYear; i++)
            {


                Console.WriteLine("{0,-10} || {1,-10} || {2,5}", i, (i * 2), (i * (i + 1)));
                //Console.Write(i);
                //Console.Write(i * 2);
                //Console.Write(i * (i + 1));

            }
            Console.WriteLine("_________________________________");

        }
    }
}

Output:

L6VId.png

Update:

I have tried the 2 ways from the given workarounds in this reference, but I'm getting the exception like:

Mn8Q0.png

mLiXG46.png

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-05-02T02:00:37.807+00:00

    @Rajoli Hari Krishna , based on my test, I reproduced your problem.

    First, We need to set the column before we want to use ConsoleTable.

     var table = new ConsoleTable("Day No", "Daily Saving", "Total Saved");  
    

    Second, Please add rows in the loop.

      for (int i = 1; i <= daysInYear; i++)  
                {  
                    table.AddRow(i, (i * 2), (i * (i + 1)));  
                }  
    

    Finally, you could use the following code to show the table in the console:

    table.Write();  
    

    Completed code example:

    DateTime currentDate = DateTime.Now;  
                int daysInYear = DateTime.IsLeapYear(currentDate.Year) ? 366 : 365;  
                int daysLeftInYear = daysInYear - currentDate.DayOfYear; // Result is in range 0-365.  
                int finisheddaysCount = daysInYear - daysLeftInYear;  
                Console.WriteLine("daysLeftInYear is {0}", daysLeftInYear);  
                Console.WriteLine("finishedDaysCount is {0}", finisheddaysCount);  
                int savings = (daysInYear * (daysInYear + 1)) - (finisheddaysCount * (finisheddaysCount - 1));  
                Console.WriteLine(savings);  
                //Case 2:  
                Console.WriteLine("_________________________________");  
                var table = new ConsoleTable("Day No", "Daily Saving", "Total Saved");  
                for (int i = 1; i <= daysInYear; i++)  
                {  
                    table.AddRow(i, (i * 2), (i * (i + 1)));  
                }  
      
                table.Write();  
      
                Console.ReadKey();  
    

    Result:

    198049-image.png

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.