Answer to Question #248027 in C# for Holik

Question #248027
A four-digit number is given. Find the number 3 in it.
1
Expert's answer
2021-10-08T00:32:52-0400
using System;


namespace App
{
    class Program
    {
        static void Main(string[] args)
        {


            int number;
            Console.Write("Enter a four-digit positive integer: ");
            number = int.Parse(Console.ReadLine());
            if (number < 1000 || number > 9999)
            {
                Console.WriteLine("Wrong number.\n");
            }
            else
            {
                int index = -1;
                int counter = 0;
                for (int i = 1000; i >= 10; i /= 10)
                {
                    int digit = (number / i) % 10;
                    if (digit == 3)
                    {
                        index = counter;
                        break;
                    }
                    counter++;
                }
                if (index != -1)
                {
                    Console.WriteLine("The number 3 exists at index {0}.", index);
                }
                else
                {
                    Console.WriteLine("The number 3 does not exist");
                }
            }


            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS