Count Digits of a Number in C Language

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

In C language, you can use a while loop to count the number of digits in a given integer. The basic idea is to repeatedly divide the number by 10 until it becomes zero, and count the number of divisions.

Here's an example of C code that counts the number of digits in a given number:

Copy code

#include <stdio.h> int main() {    int n, count = 0;    printf("Enter a positive integer: ");    scanf("%d", &n);    while (n != 0) {        n /= 10;        ++count;    }    printf("The number of digits in the entered integer is %d.\n", count);    return 0; }

This code prompts the user to enter a positive integer, and then uses a while loop to repeatedly divide the number by 10 until it becomes zero. It uses a variable 'count' to keep track of the number of divisions. Within the while loop, it uses the shorthand operator /= which divides the number by 10 and assigns the result to the same variable. And increments the count variable. After the while loop, it prints the number of digits in the entered integer which is stored in the count variable. It's also possible to use a for loop and modulus operator to get the number of digits of an integer.

7. Loops

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support

Sciaku (सियाकु)

Sciaku (सियाकु) provides you a technical and programming content like Java Programming, Python Programming, C Programming,Android Development, Web Development, etc. Learn how to make software, website, and applications here and also we have industrial internship for you.

Contact

G20, Gopal Vihar Colony, Noida Sector 2, Uttar Pradesh, India, 201301

info@sciaku.com

Copyright © 2022-2025 Created by ❤️ Sciaku

Privacy Policy | Terms & Conditions | Refunds Policy