⑪ C Program to Count Digits in a Number (With Program and Explanation)
๐ข C Program to Count Digits in a Number
๐ Problem Statement
Write a C program to count the total number of digits in a given integer number.
๐ง Understanding the Logic
To count digits in a number:
-
Take a number as input.
-
Repeatedly divide the number by 10.
-
Each division removes the last digit.
-
Count how many times the division happens until the number becomes 0.
๐ Each division means one digit.
✨ Special Case
-
If the number is 0, the digit count is 1.
๐งพ Algorithm (Step by Step)
-
Start
-
Read the number
n -
If
n == 0, setcount = 1 -
Else
-
While
n > 0-
count++ -
n = n / 10
-
-
-
Print
count -
End
๐ป C Program to Count Digits
๐ค Sample Output Examples
▶ Example 1
Input
Output
๐ Explanation of Code
-
num / 10removes the last digit-
Example:
123 / 10 = 12
-
-
Loop runs until
numbecomes0 -
Each loop increases
countby 1 -
Final
countis the number of digits
Comments
Post a Comment