none
Can someone help me out to find an error in a c# code? RRS feed

  • Question

  • Next program asks for a number from keyboard and calculates its number of digits considering those which are bigger than MAX. 

    The code isthe following:

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

    namespace ex3
    {
        class cex3
        {
            const int MAX = 7;

            static void Main(string[] args)
            {
                int original, num, count = 0;

                Console.WriteLine("Write down a number:");
                num = Convert.ToInt32(Console.ReadLine());
                original = num;

                while (num / 10 != 0)
                {
                    if (num % 10 > MAX)
                    count++;
                    num = num / 10;
                }

                Console.WriteLine("The number " + original + " has " + count + " digits bigger than " + MAX);
                Console.ReadLine();
            }
        }
    }

    Thursday, October 3, 2019 8:33 AM

Answers

  • Next program asks for a number from keyboard and calculates its number of digits considering those which are bigger than MAX. 

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

    namespace ex3
    {
        class cex3
        {
            const int MAX = 7;

            static void Main(string[] args)
            {
                int original, num, count = 0;

                Console.WriteLine("Write down a number:");
                num = Convert.ToInt32(Console.ReadLine());
                original = num;

                while (num / 10 != 0)
                {
                    if (num % 10 > MAX)
                    count++;
                    num = num / 10;
                }

                Console.WriteLine("The number " + original + " has " + count + " digits bigger than " + MAX);
                Console.ReadLine();
            }
        }
    }

    What seems to be the trouble with it? Don't make us guess.

    At first glance, it looks like you may be skipping the first digit entered.

    Try changing

    while (num / 10 != 0)

    to

    while (num != 0)

    - Wayne

    • Marked as answer by a_paris_m Thursday, October 3, 2019 10:22 AM
    Thursday, October 3, 2019 9:21 AM

All replies

  • Next program asks for a number from keyboard and calculates its number of digits considering those which are bigger than MAX. 

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

    namespace ex3
    {
        class cex3
        {
            const int MAX = 7;

            static void Main(string[] args)
            {
                int original, num, count = 0;

                Console.WriteLine("Write down a number:");
                num = Convert.ToInt32(Console.ReadLine());
                original = num;

                while (num / 10 != 0)
                {
                    if (num % 10 > MAX)
                    count++;
                    num = num / 10;
                }

                Console.WriteLine("The number " + original + " has " + count + " digits bigger than " + MAX);
                Console.ReadLine();
            }
        }
    }

    What seems to be the trouble with it? Don't make us guess.

    At first glance, it looks like you may be skipping the first digit entered.

    Try changing

    while (num / 10 != 0)

    to

    while (num != 0)

    - Wayne

    • Marked as answer by a_paris_m Thursday, October 3, 2019 10:22 AM
    Thursday, October 3, 2019 9:21 AM
  • I'm really grateful for you to help me out finding an error.

    -Arnau

    Thursday, October 3, 2019 10:26 AM