In this tutorial you will learn about the C Program to Count number of digits in number without using mod operator and its application with practical example.
C Program to Count the number of digits in number without using the mod operator.
In this tutorial, we will learn to create a C program that will Count the number of digits in number without using the mod operator in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- String functions in c programming.
Count the number of digits in number without using the mod operator.
As we all know the c programming is a very powerful language. In the c programming language, we have many pre-defined functions for arithmetic operations. but today we will count the number of digits in a number without using the mod operator.
With the help of this program, we can count the number of digits in numbers without using the mod operator.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables. 2. Take the input number from the user. 3. Pass that number to a string type variable. 4. Calculate the size of string. 5. Print the size of that string. 6. End the program. |
Program to count the number of digits in numbers without using the mod operator:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<stdio.h> //importing the header file #include<string.h> int main() { //declaring local variable for the program. int no, digits; char ch[10]; //Taking input from the user for calculating the number digits in number printf("\nEnter the Number : "); scanf("%d", &no); //passing that number to a string variable sprintf(ch, "%d", no); //storing the sting size by using pre defined function digits = strlen(ch); //Printing output of the program printf("\nNumber of Digits in given number : %d", digits); return(0); } |
Output:-
Including header files.
In the above program, we have first initialized the required variable.
- ch[10] = it will hold the string value.
- digits = it will hold the integer value for numbers.
- no = it will hold the integer value of number input.
Taking input number from the user.
Converting that number to a string.
Storing the string size by using a predefined function.
Printing output of the program.