Posted on by Kalkicode
Code Number

# Find number of digits in a number

The given problem requires finding the number of digits in a given integer. We need to design a C program that takes an integer as input and calculates the number of digits in it, considering both positive and negative numbers.

## Problem Statement

Write a C program to find the number of digits in a given integer. The program should handle positive and negative integers and return the count of digits in each case.

## Example

Let's consider an example to understand the problem and how the solution works:

## Input

• Integer: 84211
• Integer: -1111
• Integer: 100
• Integer: 123451

## Output

• Number : 84211 Digit : 5
• Number : -1111 Digit : 4
• Number : 100 Digit : 3
• Number : 123451 Digit : 6

## Pseudocode

The pseudocode for the given problem can be written as follows:

``````function digits_length(number)
result = 0
print("Number : ", number)
if number == 0
result = 1
if number < 0
number = absolute value of number

while number != 0
result = result + 1
number = number / 10

print("Digit : ", result)
``````

## Algorithm Explanation

1. Start the function `digits_length(number)` with an input parameter 'number.'
2. Initialize a variable `result` to store the number of digits and set it to 0.
3. Print the input number to show the current test case.
4. Check if the number is 0. If it is, set `result` to 1, as the number of digits in 0 is 1.
5. If the number is negative, convert it to its absolute value so that we can ignore the negative sign when counting digits.
6. Use a while loop to count the digits in the number: while the number is not equal to 0, do the following:
• Increment the `result` by 1.
• Divide the number by 10 to remove the last digit.
7. After the loop ends, print the `result`, which represents the number of digits in the input number.
8. Repeat the process for other test cases.

## Code Solution

Here given code implementation process.

``````// C Program
// Find number of digits in a number
#include <stdio.h>

//Find the length of number
void digits_length(int number)
{
int result = 0;
printf("Number : %d  ",number);
if(number==0)
{
//When number is zero, then digit length is one
result = 1;
}
if(number < 0)
{
//When number is negative number
number = -number;
}
while(number!=0)
{
result++;
number/=10;
}
printf("Digit : %d  \n",result);
}
int main()
{
//Test Case
digits_length(84211);
digits_length(-1111);
digits_length(100);
digits_length(123451);
return 0;
}``````

#### Output

``````Number : 84211  Digit : 5
Number : -1111  Digit : 4
Number : 100  Digit : 3
Number : 123451  Digit : 6``````
``````/*
C++ Program
Find number of digits in a number
*/
#include<iostream>

using namespace std;
class MyNumber
{
public:
//Find the length of number
void digits_length(int number)
{
int result = 0;
cout << "Number : " << number << " ";
if (number == 0)
{
//When number is zero, then digit length is one
result = 1;
}
if (number < 0)
{
//When number is negative number
number = -number;
}
while (number != 0)
{
result++;
number /= 10;
}
cout << "Digit : " << result << " \n";
}
};
int main()
{
MyNumber obj =  MyNumber();
//Test Case
obj.digits_length(84211);
obj.digits_length(-1111);
obj.digits_length(100);
obj.digits_length(123451);
return 0;
}``````

#### Output

``````Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6``````
``````/*
Java Program
Find number of digits in a number
*/
public class MyNumber
{
//Find the length of number
public void digits_length(int number)
{
int result = 0;
System.out.print("Number : " + number + " ");
if (number == 0)
{
//When number is zero, then digit length is one
result = 1;
}
if (number < 0)
{
//When number is negative number
number = -number;
}
while (number != 0)
{
result++;
number /= 10;
}
System.out.print("Digit : " + result + " \n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.digits_length(84211);
obj.digits_length(-1111);
obj.digits_length(100);
obj.digits_length(123451);
}
}``````

#### Output

``````Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6``````
``````/*
C# Program
Find number of digits in a number
*/
using System;
public class MyNumber
{
//Find the length of number
public void digits_length(int number)
{
int result = 0;
Console.Write("Number : " + number + " ");
if (number == 0)
{
//When number is zero, then digit length is one
result = 1;
}
if (number < 0)
{
//When number is negative number
number = -number;
}
while (number != 0)
{
result++;
number /= 10;
}
Console.Write("Digit : " + result + " \n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.digits_length(84211);
obj.digits_length(-1111);
obj.digits_length(100);
obj.digits_length(123451);
}
}``````

#### Output

``````Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6``````
``````<?php
/*
Php Program
Find number of digits in a number
*/
class MyNumber
{
//Find the length of number
public 	function digits_length(\$number)
{
\$result = 0;
echo("Number : ". \$number ." ");
if (\$number == 0)
{
//When number is zero, then digit length is one
\$result = 1;
}
if (\$number < 0)
{
//When number is negative number
\$number = -\$number;
}
while (\$number != 0)
{
\$result++;
\$number = intval(\$number / 10);
}
echo("Digit : ". \$result ." \n");
}
}

function main()
{
\$obj = new MyNumber();
//Test Case
\$obj->digits_length(84211);
\$obj->digits_length(-1111);
\$obj->digits_length(100);
\$obj->digits_length(123451);
}
main();``````

#### Output

``````Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6``````
``````/*
Node Js Program
Find number of digits in a number
*/
class MyNumber
{
//Find the length of number
digits_length(number)
{
var result = 0;
process.stdout.write("Number : " + number + " ");
if (number == 0)
{
//When number is zero, then digit length is one
result = 1;
}
if (number < 0)
{
//When number is negative number
number = -number;
}
while (number != 0)
{
result++;
number = parseInt(number / 10);
}
process.stdout.write("Digit : " + result + " \n");
}
}

function main(args)
{
var obj = new MyNumber();
//Test Case
obj.digits_length(84211);
obj.digits_length(-1111);
obj.digits_length(100);
obj.digits_length(123451);
}
main();``````

#### Output

``````Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6``````
``````#   Python 3 Program
#   Find number of digits in a number

class MyNumber :
# Find the length of number
def digits_length(self, number) :
result = 0
print("Number : ", number ," ", end = "")
if (number == 0) :
# When number is zero, then digit length is one
result = 1

if (number < 0) :
# When number is negative number
number = -number

while (number != 0) :
result += 1
number = int(number / 10)

print("Digit : ", result ," \n", end = "")

def main() :
obj = MyNumber()
# Test Case
obj.digits_length(84211)
obj.digits_length(-1111)
obj.digits_length(100)
obj.digits_length(123451)

if __name__ == "__main__": main()``````

#### Output

``````Number :  84211  Digit :  5
Number :  -1111  Digit :  4
Number :  100  Digit :  3
Number :  123451  Digit :  6``````
``````#   Ruby Program
#   Find number of digits in a number

class MyNumber

# Find the length of number
def digits_length(number)

result = 0
print("Number  :", number ," ")
if (number == 0)

# When number is zero, then digit length is one
result = 1
end
if (number < 0)

# When number is negative number
number = -number
end
while (number != 0)

result += 1
number /= 10
end
print("Digit  :", result ," \n")
end
end
def main()

obj = MyNumber.new()
# Test Case
obj.digits_length(84211)
obj.digits_length(-1111)
obj.digits_length(100)
obj.digits_length(123451)
end
main()``````

#### Output

``````Number  :84211 Digit  :5
Number  :-1111 Digit  :4
Number  :100 Digit  :3
Number  :123451 Digit  :6
``````
``````/*
Scala Program
Find number of digits in a number
*/
class MyNumber
{
//Find the length of number
def digits_length(num: Int): Unit = {
var result: Int = 0;
var number: Int = num;
print("Number : " + number + " ");
if (number == 0)
{
//When number is zero, then digit length is one
result = 1;
}
if (number < 0)
{
//When number is negative number
number = -number;
}
while (number != 0)
{
result += 1;
number = (number / 10).toInt;
}
print("Digit : " + result + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Case
obj.digits_length(84211);
obj.digits_length(-1111);
obj.digits_length(100);
obj.digits_length(123451);
}
}``````

#### Output

``````Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6``````
``````/*
Swift Program
Find number of digits in a number
*/
class MyNumber
{
//Find the length of number
func digits_length(_ num:  Int)
{
var result: Int = 0;
var number: Int = num;
print("Number : ", number ," ", terminator: "");
if (number == 0)
{
//When number is zero, then digit length is one
result = 1;
}
if (number < 0)
{
//When number is negative number
number = -number;
}
while (number != 0)
{
result += 1;
number /= 10;
}
print("Digit : ", result ," \n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Case
obj.digits_length(84211);
obj.digits_length(-1111);
obj.digits_length(100);
obj.digits_length(123451);
}
main();``````

#### Output

``````Number :  84211  Digit :  5
Number :  -1111  Digit :  4
Number :  100  Digit :  3
Number :  123451  Digit :  6``````

## Time Complexity

The time complexity of the provided code is O(d), where 'd' is the number of digits in the input number. This is because the while loop iterates 'd' times, where 'd' is the number of digits in the number.

## Result Explanation

The output of the given c program is as follows:

``````Number : 84211  Digit : 5
Number : -1111  Digit : 4
Number : 100    Digit : 3
Number : 123451 Digit : 6
``````

The program provides the correct output for each test case. It correctly counts the number of digits in positive and negative integers. For example, in the first test case, the number is 84211, which has 5 digits, and the program outputs "Digit: 5." Similarly, for other test cases, it provides the correct digit count.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post