Q: Petar's Game - C# Task

+5 votes

Petar likes to play with numbers. He is very good with strings too. One day he decided to invent a new game of summing numbers.

He will get one number and will try to divide it by 5, if the number can be divided without remainder (for example 15 can be divided by 5 without remainder, but 17 divided by 5 is 3 with remainder 2) Petar will add this number to the sum. However if the number cannot be divided without remainder, only the remainder will be added to the sum.

After he is done with the numbers, Petar likes to replace some of the sum's digits with strings. If the sum is odd he will replace the last digit and all others that are the same as it with a given string. If the sum is even he will do the same, but with the first digit. For example, if the sum is 2434, and the string is "a" - the result will be a434.

You will be given a start number, an end number and a string. You have to check all numbers between the start number and the end number (without the end number), do Petar's algorithm and finally replace the digits with the string as described above.


The input data should be read from the console. It consists of three lines:

  • The first line will hold the starting number;
  • The second  line will hold the end number;
  • The third like will hold the replacement string

The input data will always be valid and in the format described. There is no need to check it explicitly.


  • The output data must be printed on the console.
  • On the only output line you must print the result of the game.


  • Start number and end number will be integers in the range [0 … 18 446 744 073 709 551 615].
  • The string will contain letters and numbers.
  • Allowed memory: 16 MB.


Petar's game screen 1

Petar's game screen 2

screen 3 - Petar game

asked in C# category by user sam
edited by user golearnweb

1 Answer

+2 votes
Best answer

Interesting C# task! Here is my solution - you can read the comments in green to understand better:

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

class PetarsGame
    static void Main()
        ulong startNumber = ulong.Parse(Console.ReadLine());//use ulong instead of decimal to speed up the program
        ulong endNumber = ulong.Parse(Console.ReadLine());
        string letter = Console.ReadLine();

        decimal sum = 0;//or you can use BigInteger here - because the number could be big
        for (ulong i = startNumber; i < endNumber; i++)
            if (i % 5 == 0)
                sum += i;

            else if (i % 5 != 0)
                sum += (i % 5);

        string sumAsString = sum.ToString();//convert the sum (which is integer so far) to string so we can replace the numbers with a given replacement char/string

        string digitToReplace;
        if (sum % 2 == 0)//check if the sumAsString is EVEN
            digitToReplace = sumAsString[0].ToString();//convert to string as it is represented as char now
        else//check if the sumAsString is ODD (sum % 2 != 0)
            digitToReplace = sumAsString[sumAsString.Length - 1].ToString();//convert to string as it is represented as char now

        sumAsString = sumAsString.Replace(digitToReplace, letter);//using the .Replce() method over the string

Also - you can read more about the .Replace() method over the string here:

answered by user eiorgert
selected by user sam