Program to count number of digits in given number using While loop

Program to count number of digits in given number using While loop

  • Write a program to count number of digits in given number using While loop in C
  • Write a program to count number of digits in given number using While loop in C++
  • Write a program to count number of digits in given number using While loop in Python
  • Write a program to count number of digits in given number using While loop in PHP
  • Write a program to count number of digits in given number using While loop in Java
  • Write a program to count number of digits in given number using While loop in Java Script
  • Write a program to count number of digits in given number using While loop in C#

Explanation:

A for loop can be used to count the number of digits in a given number by continually dividing it by 10 and counting the number of iterations needed to get the number down to 0.

Logic

  1. Take input for the number (n).
  2. Initialize a counter variable to 0.
  3. Use a While loop to repeatedly divide nnn by 10.
    • Each time you divide, increment the counter.
    • Exit the loop when nnn becomes 0.
  4. The counter will contain the number of digits in the given number.

Program to count number of digits in given number using While loop

#include <stdio.h>

int main() {
    int num, count = 0;
    
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Handle negative numbers
    if (num < 0) {
        num = -num;
    }
    
    // Count digits
    while (num != 0) {
        num /= 10;
        count++;
    }
    
    printf("Number of digits: %d\n", count);
    
    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int num, count = 0;

    cout << "Enter a number: ";
    cin >> num;
    
    // Handle negative numbers
    if (num < 0) {
        num = -num;
    }
    
    // Count digits
    while (num != 0) {
        num /= 10;
        count++;
    }
    
    cout << "Number of digits: " << count << endl;

    return 0;
}

num = int(input("Enter a number: "))
count = 0

# Handle negative numbers
if num < 0:
    num = -num

# Count digits
while num != 0:
    num //= 10
    count += 1

print(f"Number of digits: {count}")

<?php
$num = (int)readline("Enter a number: ");
$count = 0;

// Handle negative numbers
if ($num < 0) {
    $num = -$num;
}

// Count digits
while ($num != 0) {
    $num = (int)($num / 10);
    $count++;
}

echo "Number of digits: $count\n";
?>

import java.util.Scanner;

public class CountDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        int count = 0;

        // Handle negative numbers
        if (num < 0) {
            num = -num;
        }

        // Count digits
        while (num != 0) {
            num /= 10;
            count++;
        }

        System.out.println("Number of digits: " + count);
        sc.close();
    }
}

let num = parseInt(prompt("Enter a number: "));
let count = 0;

// Handle negative numbers
if (num < 0) {
    num = -num;
}

// Count digits
while (num != 0) {
    num = Math.floor(num / 10);
    count++;
}

console.log(`Number of digits: ${count}`);

using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int num = int.Parse(Console.ReadLine());
        int count = 0;

        // Handle negative numbers
        if (num < 0) {
            num = -num;
        }

        // Count digits
        while (num != 0) {
            num /= 10;
            count++;
        }

        Console.WriteLine($"Number of digits: {count}");
    }
}

List of All Programs