Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Count Number of Digits of an Integer

 C++ program to count number of digit in an integer. This program takes an integer from user and calculates the number of digits in that integer. For example: If user enters 2319, the output of program will be 4 because it contains 4 digits.This program takes an integer from user and stores that number in variable n. Suppose, user entered 34523. Then, while loop is executed because n!=0 will be true in first iteration.
The codes inside while loop will be executed. After first iteration, value of n will be 3452 and count will be 1. Similarly, in second iteration n will be equal to 345 and count will be equal to 2. This process goes on and after fourth iteration, n will be equal to 3 and count will be equal to 4. Then, in next iteration n will be equal to 0 and count will be equal to 5 and program will be terminated as n!=0 becomes false.


Source Code:-

#include<iostream.h>
#include<conio.h>

void main()
{   clrscr();
    long int num1,count=0;
    cout<<"count number of digits in a number  by-Tarun Rawat\n\n";
    cout<<"Enter number to count it's Digit : ";
    cin>>num1;
    while(num1!=0)
    {num1=num1/10;
     count++;
    }
    cout<<"Number of Digit is : "<<count;
getch();
}

No comments:

Post a Comment

Blogger Widgets

Find Us On Google, Just type - way2cplusplus -

If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)