Answer to Question #272747 in C# for Holik

Question #272747

9. A four-digit number is given. Determine if there are 3 digits in this number.

1
Expert's answer
2021-11-28T11:44:35-0500
using System;
using System.Collections.Generic;


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;
                        Console.WriteLine("The number 3 exists at index {0}.", index);
                    }
                    counter++;
                }
                if (index == -1)
                {
                    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