Digits In A Number - C# Programming Exercise

This C# exercise aims to teach how to develop a program that calculates the number of digits in a positive integer. The program uses an efficient technique by performing repeated division by 10, which allows counting the number of digits that make up a number. If the user enters a negative number, the program will display a warning message indicating that the number is negative, and automatically proceed to calculate the number of digits for the absolute value of that number. This exercise is crucial for understanding how to handle numbers in programming and how to perform operations such as division and converting negative numbers to positive using the absolute value function. It also teaches how to improve input validation in programs, an essential aspect for preventing errors in real-world applications. Exercises like this are fundamental for reinforcing knowledge of basic arithmetic operations and their implementation in programming languages like C#.

 Category

Flow Control

 Exercise

Digits In A Number

 Objective

Write a C# program to calculate the number of digits in a positive integer (hint: this can be done by repeatedly dividing by 10). If the user enters a negative integer, the program should display a warning message and proceed to calculate the number of digits for the equivalent positive integer.

For example:
Number = 32
2 digits
Number = -4000
(Warning: it is a negative number) 4 digits

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Prompt the user to enter a number
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());  // Reading the number entered by the user

        // Check if the number is negative
        if (number < 0)  // If the number is negative
        {
            Console.WriteLine("(Warning: it is a negative number)");  // Display the warning message
            number = Math.Abs(number);  // Convert the number to positive using the Math.Abs() method
        }

        // Variable to count the digits
        int digits = 0;

        // Calculate the number of digits by repeatedly dividing the number by 10
        while (number > 0)  // Loop continues as long as the number is greater than 0
        {
            number /= 10;  // Divide the number by 10
            digits++;  // Increment the digit count
        }

        // Display the number of digits
        Console.WriteLine(digits + " digits");  // Output the total number of digits
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Hollow square

    This C# exercise aims to develop a program that asks the user for a symbol and a width, and then displays a hollow square of that width using the provid...

  •  Product

    This C# exercise aims to develop a program that asks the user for two integer numbers and shows their multiplication, but without using the "*" operator. Inste...

  •  Absolute value

    This C# exercise aims to develop a program that calculates (and displays) the absolute value of a number x. The absolute value of a number is its distan...

  •  Hollow rectangle

    This C# exercise aims to develop a program that prompts the user for a symbol, a width, and a height, then displays a hollow rectangle of the spe...

  •  Statistics

    This C# exercise aims to develop a program that allows the user to input several numbers and calculate basic statistical operations such as sum, average, mi...

  •  Switch

    This C# exercise aims to develop a program that, given a numerical grade, displays the corresponding text grade according to the following equivalence: 9 and ...