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

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

  • Write a program to count number of digits in given number using For loop in C
  • Write a program to count number of digits in given number using For loop in C++
  • Write a program to count number of digits in given number using For loop in Python
  • Write a program to count number of digits in given number using For loop in PHP
  • Write a program to count number of digits in given number using For loop in Java
  • Write a program to count number of digits in given number using For loop in Java Script
  • Write a program to count number of digits in given number using For 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 for 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 For loop

#include <stdio.h>

int main() {
    int num, count = 0;
    printf("Enter a number: ");
    scanf("%d", &num);

    for (count = 0; num != 0; count++) {
        num /= 10;  // Remove the last digit
    }

    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;

    for (count = 0; num != 0; count++) {
        num /= 10;  // Remove the last digit
    }

    cout << "Number of digits: " << count << endl;
    return 0;
}

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

for count in range(0, num+1):
    if num == 0:
        break
    num //= 10  # Remove the last digit

print("Number of digits:", count)

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

for ($count = 0; $num != 0; $count++) {
    $num = (int)($num / 10);  // Remove the last digit
}

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

import java.util.Scanner;

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

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

        for (count = 0; num != 0; count++) {
            num /= 10;  // Remove the last digit
        }

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

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

for (count = 0; num !== 0; count++) {
    num = Math.floor(num / 10);  // Remove the last digit
}

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;

        for (count = 0; num != 0; count++) {
            num /= 10;  // Remove the last digit
        }

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

List of All Programs