Entry Level C++ Problems: Find the Number of Digits in a Number

Here we have a program that’ll display the number of digits in a number. It’ll let you see what it does during the runtime too.

Ask the user to input a number and create variables for a counter (since we need to count) a number and a remainder. We’ll display numbers and their remainders as we go:

#include <iostream>

int main()
{

	int number;
	int remainder = 0;
	int counter=0;

	std::cout << "Type in a number: ";
	std::cin >> number;

	return 0;
}

We’re going to need a loop if we’re asked to do a repetitive action such as counting. The loop is going to run for as long as the variable ‘i’ is less or equal to the number typed in by the user:

#include <iostream>

int main()
{

	int number;
	int remainder = 0;
	int counter=0;

	std::cout << "Type in a number: ";
	std::cin >> number;

	for (int i = 0; i <= number; i++)
	{	
	}

	return 0;
}

We want to display a number and its remainder and increase the counter variable everytime the loop iterates:

#include <iostream>

int main()
{

	int number;
	int remainder = 0;
	int counter=0;

	std::cout << "Type in a number: ";
	std::cin >> number;

	for (int i = 0; i <= number; i++)
	{


		remainder = number % 10;
		number = number / 10;
                counter++;

		std::cout << number << " R" << remainder << 
	        std::endl;

	}



	return 0;
}

We need a conditional though in order to display the first number. Right now the program will automatically perform the operations we asked it to perform and then it’ll display the number. So in order to prevent that we could use an if statement to only displat the number and increase the counter if i=0 and then display the number, increase the counter and perform calculations if i!=0.

#include <iostream>

int main()
{

	int number;
	int remainder = 0;
	int counter=0;

	std::cout << "Type in a number: ";
	std::cin >> number;

	for (int i = 0; i <= number; i++)
	{

		if (i == 0)
		{
			std::cout << number << std::endl;
			counter++;

		}
		else
		{


			remainder = number % 10;
			number = number / 10;
			counter++;
			std::cout << number << " R" << remainder <<                
                        std::endl;
		}
	

	

	}

	std::cout << "Number of digits: " << counter << std::endl;

	return 0;
}

As it turns out our program will not display all the numbers. It’ll stop at a single digit number with its remainder and not display 0 with its remainder. It’ll also count one digit number as two digits. In order to fix that we need another set of conditionals that prevent it. This time we’ll nest them within the ‘else’ statement. We won’t touch the first if statement because it’s only supposed to run once when loop starts iterating.

#include <iostream>

int main()
{

	int number;
	int remainder = 0;
	int counter=0;

	std::cout << "Type in a number: ";
	std::cin >> number;

	for (int i = 0; i <= number; i++)
	{

		if (i == 0)
		{
			std::cout << number << std::endl;
			counter++;

		}
		else
		{


			if (number < 10)
			{
				remainder = number % 10;
				number = number / 10;
				std::cout << number << " R" << remainder <<  
                                std::endl;
			}
			else
			{
				remainder = number % 10;
				number = number / 10;
				counter++;
				std::cout << number << " R" << remainder << 
                                std::endl;
			}
		}

	

	}

	std::cout << "Number of digits: " << counter << std::endl;

	return 0;
}

First we’ll check if the number is less than 10. If it is we’re only going to run whatever is in this if statement and discard everything else. This will prevent 1 digit numbers to be counted as 2 digit numbers since this will not increase the counter and it’ll print any result that is 0 with a remainder.

Leave a Reply

Discover more from NMKN Studio

Subscribe now to keep reading and get access to the full archive.

Continue reading