settingsAccountsettings
Menusettings

Q: Check if the third digit is 7?

+3 votes

Please help me write an expression that checks for given integer if its third digit from right-to-left is 7.

Examples:

check if the third number is 7 (seven)

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

1 Answer

+1 vote
 
Best answer

Here is my solution:

using System;

class Program
{
    static void Main()
    {
        long n = long.Parse(Console.ReadLine());

        long check100 = n / 100;//Dividing the number by 100 >> and the 3rd number is the FIRST NOW
        long checkRest = check100 % 10;//using module division and getting the leftover number 

        bool result = (checkRest == 7);//checking whether the leftover number is 7 or not with bool expression

        Console.WriteLine("Third number 7? {0}", result);//printing the result
    }
}
answered by user andrew
selected by user golearnweb
...