Algorithm and Flowchart to Count Number of Digits in an Integer

[449 views]


Introduction

In this program we will be counting the no. of digits in the input integer. For Eg. lets say we input 342567 so the no. of digits is 6.
There are two different method for counting the digits.

  1. Using a loop whether it be a while or recursive loop.
  2. Using a log but that is applicable only for positive numbers.
In this article, we will be demonstrating it through while loop.

What should be the approach for this program?

Let's consider we have a number 342567 so how can we count its digit? First thing is clear that we have to use a loop as we are counting the digits, as long as the loop will run we will add the 1 to the variable. But we don't know the length of integer so what we can do is divide it by 10.We divide until the number becomes zero so loop will run until the length of digits. Though this we can count no. of digits in a given integer.

Flowchart for counting no. of digits in the number:

Flowchart for counting no. of digits in the number
Remove WaterMark from Above Flowchart

Algorithm for counting no. of digits in the number:

Declare variable a and n; //n variable is used for counting Read number a; while(a not equal 0) a=a/10; n=n+1; end Print n;

Here in this algorithm we declare a variable "a" for storing the number whose digits we have to count and a variable n for counting the digits. We read the variable a. Let's take a number 456235 we have to count it's digit, so we store it in the variable a. As our number is not zero it will enter the loop, this will be the 1st iteration.

  • In the 1st iteration, comes a=a/10 so we divide 456235/10 ie. a becomes 45623 and we add a number 1 in n, n becomes 1.
  • In the 2nd iteration 45623 enters the loop a becomes 4562 and n becomes 2
  • In the 3rd iteration 4562 enters the loop a becomes 456 and n becomes 3.
  • In the 4th iteration 456 enters the loop a becomes 45 and n becomes 4.
  • In the 5th iteration 45 enters the loop a becomes 4 n becomes 5.
  • In the 6th iteration a becomes 0 and n becomes 6
Now the a will not enter the loop so we print the value of n which is 6.

Count Number of Digits in an Integer Implementation in C:

#include<stdio.h> int main() { int a, n; printf("Enter a number:"); scanf("%d", & a); while (a != 0) { a /= 10; n++; } printf("No. of digits is %d", n); }

Note: The value stored in the variable is int hence the digit should be in between -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 depending on whether you have 32 bit or 64 bit compiler. At max a variable can store 9223372036854775807 this much large number using long datatype.

Output of the program:

        

Algorithm Logic Test

Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once. 



Comments



Recommended Book Deals Ends in

Download 101 Ready to Use Excel Formulas ebook
Download Python All in One for Dummies ebook



Search
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Flowchart for Number of Digits in a number

    Algorithm for Number of Digits in a number

    C Program to find number of digits in number