Determining Integer Digit Count in C++

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to determine the number of digits present in a given integer number using C++. We will be using a simple logic of dividing the number by 10 and counting the number of divisions until the quotient becomes 0 with each division.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/while_loop("`While Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/comments -.-> lab-96127{{"`Determining Integer Digit Count in C++`"}} cpp/variables -.-> lab-96127{{"`Determining Integer Digit Count in C++`"}} cpp/data_types -.-> lab-96127{{"`Determining Integer Digit Count in C++`"}} cpp/operators -.-> lab-96127{{"`Determining Integer Digit Count in C++`"}} cpp/while_loop -.-> lab-96127{{"`Determining Integer Digit Count in C++`"}} cpp/function_parameters -.-> lab-96127{{"`Determining Integer Digit Count in C++`"}} end

Creating a new C++ file

First, we need to create a new C++ file in our project directory. Open the terminal and navigate to your project directory using cd ~/project. Then, create a new C++ file called main.cpp using the touch command:

touch main.cpp

Writing the program

In this step, we will write the C++ program to determine the number of digits present in a given number. Copy and paste the following code into your main.cpp file:

// C++ program to count number of digits in a given number
#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " =====  Program to count the number of digits in a given number ===== \n\n";

    //variable declaration
    int n, n1, num = 0;

    //taking input from the command line (user)
    cout << " Enter a positive integer :  ";
    cin >> n;

    n1 = n; //storing the original number

    //Logic to count the number of digits in a given number
    while (n != 0)
    {
        n /= 10; //to get the number except the last digit.
        num++; //when divided by 10, updated the count of the digits
    }

    cout << "\n\nThe number of digits in the entered number: " << n1 << " is " << num;
    cout << "\n\n\n";

    return 0;
}

In this program, we take input a positive integer from the user and store it in variables n and n1. We initialize num to 0 which will store the count of the digits in the number. We then use a while loop to count the digits. In each iteration, we divide the number by 10, and increase the count of digit by 1. We continue this loop until the quotient becomes zero.

After the loop is finished, we display the value of num which holds the count of the digits present in the entered number.

Compiling and running the program

Now, we need to compile and run our program. In the terminal, type the following command to compile the main.cpp file:

g++ main.cpp -o main

After compiling successfully, run the program using the following command:

./main

The program will display a prompt asking for the input number. Enter a positive integer number and press Enter. The program will process the input and print the number of digits present in the entered number.

Summary

In this lab, we learned how to use C++ to determine the number of digits present in a given integer number. We used a simple logic of dividing the number by 10 and counting the number of divisions until the quotient becomes 0.

We created a C++ program that takes a positive integer number from the user and computes the number of digits present in the number. We compiled and ran the program using the terminal and tested it with different input integers.

Other C++ Tutorials you may like