c# Math/Formula Calculation

2k views Asked by At

I have an interesting problem i am trying to solve in my c# project, my head is hurting so any advice would be greatly appreciated

Basically i have an employee that works on a 3 week cycle

wk 1 - they work mon

wk 2 - they work wed

wk 3 - they work fri

wk 4 - they work mon

wk 5 - they work wed .. and so on

I need a formula that will show me what day they worked on any given week, ie what day would they work on week 49 of employment?

Any thoughts on how i may solve this equation?

Cheers Anthony

2

There are 2 answers

2
Prasad Telkikar On BEST ANSWER

As discussed in comment we can use modulo (%) operator in your formula.

int dayInt = (nthWeek % 3);

This will give you result in 0, 1, 2. 0 - friday, 1 - Monday, 2- wednesday

Here is the code:

int nthWeek = 49;

    int result = (nthWeek % 3);

    switch(result)
    {
        case 0:
            Console.WriteLine("Worker is working on Friday");
        break;

        case 1:
            Console.WriteLine("Worker is working on Monday");
        break;

        case 2:
            Console.WriteLine("Worker is working on Wednesday");
        break;

        default:
            Console.WriteLine("Worker is working on other day");
        break;
    }

Implementation: DotnetFiddler

0
josemartindev On

This will give the desired output:

using System;
using System.Collections.Generic;

public static void WhichDaysAreTheyWorking()
{
   List<String> daysOfTheWeek = new List<String>();
   daysOfTheWeek.Add("Monday");
   daysOfTheWeek.Add("Tuesday");
   daysOfTheWeek.Add("Wednesday");
   daysOfTheWeek.Add("Thursday");
   daysOfTheWeek.Add("Friday");
   daysOfTheWeek.Add("Saturday");
   daysOfTheWeek.Add("Sunday");
   int i = 0;
   for (int week = 1; week < 50; week++)
   {
      if (daysOfTheWeek[i].Equals("Friday"))
      {
          Console.WriteLine("Week # " + week + " They work " + daysOfTheWeek[i]);
          Console.WriteLine("Week # " + (week + 1) + " They work " + daysOfTheWeek[i - 4]);
          i = 2;
          week = week + 1;
          continue;
      }
      else
      {
          Console.WriteLine("Week # " + week + "They work " + daysOfTheWeek[i]);
          i = i + 2;
      }
   }
   Console.ReadKey();
}