QUICK

QUICK

IN YOUR FAVORITE PROGRAMMING LANGUAGE, WRITE A PROGRAM THAT DETERMINES THE NUMBER OF DIGITS OF AN ARBITRARY INPUT INTEGER

OR THIS BIRD IS GONNA STAB YOU

go fuck yourself

holy shit can someone please kill this fucking bird?

digitCounter(input);
/*Note from Pajeet
I got a pretty good start on the code but it needs some polishing*/

emacs lisp
(defun count-digits (x base)
(cond ((= 0 x) 0)
(t (+ 1 (count-digits (/ x base))))))

(count-digits 1005 10) ;; => 4
(count-digits 84 10) ;; => 2

Way too easy.
#include
#include
#include

int main(int argc, char *argv[])
{
if (argc != 2)
return 1;

char *endptr;
double n = fabs(strtod(argv[1], &endptr));

if (endptr == argv[1])
return 1;

if (n == 0.0)
printf("0\n");
else
printf("%.0f\n", log10(n) + 1.0);
}

Public numDigits(string input)

Int digits = str.length(input)

Println(digits)


Something like that.

How many times are you going to post this shitty thread?

It's fun to see how other people think.
Same solution but in C.

whoops forgot to accomodate only one digit
here...
(defun count-digits (x base)
(cond ((< x base) 1)
(t (+ 1 (count-digits (/ x base) base)))))

(count-digits 1000 10) ;; => 4
(count-digits 38 10) ;; => 2
(count-digits 4 10) ;; => 1

Chill out bird

Okay, took about ten seconds:
int CountDigitsInInteger(int integer) {
if(integer==0) return 1;
return 1+(int)(log10((double)integer));
}


Inb4 tons of code monkeys who can't into math produce ridiculous loop-based solutions.

I'm hiring an Indian contractor now

public int countDigits(int x)
return new String(x).length();

Insert (dick_ass, (true))

inp = raw_input("Enter an integer:")
try:
if '.' in inp:
raise Error
test = int(inp); print "Length:", len(inp)
except:
print "Not an integer."

dont stab me

#include

int main(void){
long long n;
int count = 0;
printf("Please input an integer value: ");
scanf("%lld",&n);

while(n!=0){
n/= 10;
++count;
}


printf("Digit count: %d",count);
}

CountDigitsInInteger(0)

i kekd

Fails in some cases due to floating point error.

#include

int main()
{
int num = 0;
std::cin >> num;
int digits = 1;
while ((num /= 10) != 0)
++digits;
std::cout num;
if (num < 0)
num *= -1;
int val = 10;
int digits = 1;
while (val

import java.lang.Math;

public int getDigits(int dubs)
return 1+log10(abs(dubs));

>if(integer==0) return 1;

Are you retarded?

Not him but, 0 is a digit. But he still is retarded because the next line returns the same.

Input is not an integer. U gon get stabd.

if it's not an integer it throws an error dont stab me pls

>0 is a digit
By that logic, 010 is 3 digits long. Obviously, all leading zeros should be ignored.
0 is the "empty" number, and should be considered 0 digits.

Shit, forgot to cast to int.

Yes. But since an int can have the value 0, but not 030, it should be seen as a digit.

You must not have looked very carefully at the code. Your test returns the correct result.
Maybe you didn't understand this cryptic looking line:
if(integer==0) return 1;

However, I do have to make this amendment:
int CountDigitsInInteger(int integer) {
if(integer==0) return 1;
return 1+(int)(log10((double)abs(integer)));
}


Can you provide such a case?

>because the next line returns the same
log(0) is undefined, goof.

Why is EVE capitalized like that

def dont_stab_me(inp):
try:
if '.' in inp:
raise Error
test = int(inp); return len(inp)
except:
return None

function

Depends on the language, but 1+ undef = 1 sometimes. Wouldnt trust it, but yeah you're right.

eeeeeevaaaaaaaaa!

>But since an int can have the value 0, but not 030
You don't seem to understand how numbers actually work. 03 and 3 are just different representations of the same number.

function digitCount(num){
return String(num).length
}


ez


inb4 error handling

program asd;
var
i:integer;
s:string;

begin
readln(i);
str(i,s);
writeln(length(s));
end.

You dont seem to understand how integers work. 0 is an integer, but 030 is not, as it will be made 30.

what programming language is this?

Not the floating point guy, but, never. Ints cut off the number, not round off. Which this solution relies on.

Floating point errors is when e.g. 0.1 is expected but 0.99999999... is returned. Which doesnt matter in this case.

>but 030 is not
Yes it is, idiot. You seem to be confusing numbers with their string representation.
"30" and "030" both represent the thing that is "thirty".

By your dumbass logic: in int x = 30;, x doesn't equal 30 either.
It equals 11110 (base 2), because "30" not how it's actually stored.

lambda x: len(str(x))

Have you benchmarked the code and determined that calling log10 is actually faster than (at most) 10 iterations of a loop dividing by 10 though? I'm actually curious, I never did it myself and don't know what log10 does internally.

Doesn't take into consideration a float

Yes, I was intentionally using the int cast as a floor function.
I honestly have no idea what floating point guy was talking about.

No, and I have to admit that there's a very good chance calling log10 would be slower. But since this is a programming puzzle (in the loosest sense of the word,) a more elegant and math-y solution like mine seems more appealing.

print('Your number has ' + str(len(str(int(input('Type a number> '))))) + ' digits')

Op specified that the input is an integer. Hence I assume that 030 will be seen as 30 in this case. And we're talking decimals, not binary..

If someone is asking the amount of digits of number 3, you wouldnt say 100000000 right? Because you could just add that many 0s.

Thats my POV on this shitty assignment.

im gay

>IN YOUR FAVORITE PROGRAMMING LANGUAGE, WRITE A PROGRAM THAT DETERMINES THE NUMBER OF DIGITS OF AN ARBITRARY INPUT INTEGER


>THE NUMBER OF DIGITS OF AN ARBITRARY INPUT INTEGER

>INPUT INTEGER

>INTEGER

He's probably just sperging out anything he knows to sound smart.

heheuhad haha lollolo :-)

:=====)))
;)))
;(
;(
;(
:))))
:)

if you do int() on a float it will convert it, but that is not the number that was input.

math.stackexchange has spoken.

Damn you messed up on ;(, it can cause ()'; errors.

...

Im the guy thats on-par with stackexchange, but what about values like 03.

That is the number 3, and it has one digit.

I steal his knife every thread but eh keeps coming back.

5 print "type an integer"
10 input a%
15 if a

Hasty solution.
EXTERN scanf
EXTERN printf

SECTION .data
input: DB "%d", `\0`
output: DB "Has %d digits", `\n`, `\0`
number: DD 0
SECTION .text
GLOBAL _start

_start:
mov rdi, input
mov rsi, number
call scanf

mov eax, [number]
mov ebx, 10
xor ecx, ecx
find_digits:
cdq
idiv ebx
add ecx, 1
test eax, eax
jnz find_digits

mov rdi, output
mov esi, ecx
call printf

mov eax, 1
mov ebx, 0
int 80H

>tfw just realized that line 15 is entirely useless since when displayed as a string, the first character of an integer is a space if positive and a - if negative, so I don't need to futz around with adjusting the sign to match

also, "a" (float) is a different variable from "a%" (int)

That's stupid.
Thinking in terms of natural numbers and thinking of them as "number of things", 0 is the empty amount, and should be 0 digits.

Another way of thinking about it to using a formula.
To find the amount of natural numbers (including zero) that have some number of digits (n), the formula would be
digits(n) = 10^n - 10^(n-1)
Since we're dealing with integers here, in the 0 case 10^(-1) = 0 because of truncation.
So digits(0) = 10^0 - 0 = 1
So there is 1 natural number (incl 0), there is 1 number with 0 digits, which is 0.

function hereIsYourHomeworkAnon (x) {
x = parseInt(x);

if (isNaN(x)) {
return 0;
}

return x.toString().length;
}


All too easy.

var countDigits = require('count-digits');
console.log(countDigits(process.argv[2]))

This is how i would do as well, except the loop condition being n > 9

...

>tfw coding in C#
>plan was to using fucking modular division to see the length of the string
>in C# of all things

holy shit I am retarded

>0 is the empty amount, and should be 0 digits.
Are you trolling? You're advocating a system where it's impossible to describe 0 using digits? Sounds like a step backwards.

The representation of 0 in the common, universally used decimal number system, requires exactly one digit, and that's 0.
Leading 0s are not part of the standard number system.
Of course there are an infinite number of other ways to represent numbers, using whatever leading digits you want. But no one does that, and that's clearly not what OP wanted.
>So there is 1 natural number (incl 0), there is 1 number with 0 digits, which is 0.
Sounds like your formula is wrong then.

#include

int main(void){
long long n;
int count = 0;
printf("Please input an integer value: ");
scanf("%lld",&n);

while(n!=0){
n/= 10;
++count;
}


printf("Digit count: %d",count);
}

You're wrong though, 0 has one digit. Let us consider an integer in base 10, for example 123. We can express that integer (and any other finite integer) as a set containing the digits used to represent that integer, in this case the set {1,2,3}. Each element of the set corresponds to a single digit, so the cardinality of the set used to represent the integer is equivalent to the number of digits in the integer. In base 10 the number 0 is represented as 0, we would represent it as the set {0}. The cardinality of the set {0} is one because the set contains one element. Therefore the number 0 has one digit.

digitLength

auto byDigit(BigInt n)
{
static struct Result
{
BigInt n;
bool empty = false;

@property int front() { return n % 10; }
void popFront() { n /= 10; empty = n == 0; }
}

return Result(n);
}

import std.algorithm;
import std.stdio;
import std.exception;
import std.bigint;

void main(string[] args)
{
enforce(args.length > 0, "No number passed");
auto num = BigInt(args[1]);
writeln(num.byDigit.count);
}

function getNumDigits(val) {
var numDigits = 0;
while (val % 1 == 0) {
numDigits++;
val /= 10;
}
return numDigits;
}

Wrote it in Python first but then rewrote it in Javascript when I remembered that Python is shit.

class CountDigitsCommandlet extends Commandlet;

event int Main( string Parms )
{
if(string(int(Parms))!=Parms)
{
Log("Invalid input. Only integers are accepted.");
return 0;
}
Log(Len(Parms));
return 0;
}

Hey bud I don't know if you're trolling at this point

buttttttttttt

decimals(floats) are not integers(-3,-2,-1...5,6,7)

Sorry I made an error, it should be:
function getNumDigits(val) {
if (val == 0)
return 1;

var numDigits = 0;
while (val % 1 == 0) {
numDigits++;
val /= 10;
}
return numDigits;
}

>You're advocating a system where it's impossible to describe 0 using digits?
It can describe zero perfectly. Zero has zero digits.
>The representation of 0 in the common, universally used decimal number system, requires exactly one digit, and that's 0.
Zero is just a place holder for blank. Having a number which is entirely blank isn't particularly helpful though, so we represent it with '0'.

In the cases when zero isn't the leading number, it does count as a digit though.
In "10", the zero in the ones column does have a value. There are 10 ones, but we write it down as '0'.
>Sounds like your formula is wrong then.
Explain how. It works in the general case, so why doesn't it work in the zero case?

see

Why can't I just do len(str(input()))?

#include
#include
int main(int argc, char* argv[]) {
return 1 + (int)log10(llabs(strtoll(argv[1],NULL,10)))
}

Again, '0' is a placeholder for blank.
The set for zero is {}, but for the sake of convenience, we write this empty set as '0'.
I'm arguing this from a mathematical point, not a "what humans write down" point.

What happens if I input 000000015? It'll say 9 digits.

Pascal

not sure what language, but my guess is negatives and leading zeroes. And detecting non-integers.

import java.util.Scanner;
public class DigitFinder {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int given, check = 10, count = 1;
System.out.print(">");
given = sc.nextInt();

while(check

print(len(input()))

numberdigits =: (((1&+)@(

That's not solving the problem described in the OP. You're supposed to accept an arbitrary input integer, not an arbitrary input string.

import re
string = input( "enter an integer: " )
digits = len( re.findall( '[0-9]', string ) )
print( 'not a number' if len( string ) - digits else digits )

If you're going to use command line arguments couldn't you just do this
]#include
#include
int main(int argc, char **argv){
printf ("%d\n", strlen(argv[1]));
}

>It can describe zero perfectly. Zero has zero digits.
Okay, that's an interesting idea. But that's not the way our commonly used number system works. The digit 0 refers to an empty quantity. It's a symbol with a well established meaning.
I feel like /sci/ needs to settle the question once and for all, though.

Again, I'm not talking about "how we write shit down".

Okay, but that's what the OP was asking for. That's the whole point of digits.

len(str(abs(int(input()))))

#include

int main(){
int n,count=0;
scanf("%d",&n);
while(n > 0){
n /= 10;
count++;
}
printf("%d\n",count);
}

see

ought to do it. Although if int coerces all numeric input to integer then is still processes invalid input. Btw I don't know any "real" programming languages so sorry if I'm retarded.

int num_digits(int n)
{
return snprintf(NULL, 0, "%d", n);
}