hello sir,is there any function in C that can calculate number
of digits in an int type variable,suppose:int a=123;
3 digits in a.what ll b answer?

Answers were Sorted based on User's Feedback



hello sir,is there any function in C that can calculate number of digits in an int type variable,s..

Answer / muni

I am sorry the previous answer is for different question.

For your question the answer is ver simple.

use sprintf and strlen.

char str[10];
sprintf(str,"%d",number)
number of digits = strlen(str);

Is This Answer Correct ?    10 Yes 1 No

hello sir,is there any function in C that can calculate number of digits in an int type variable,s..

Answer / ramesh

int countDigits(number)
{
if (number==0)
return 0;
else
return 1 + countDigits(number%10);

}

Is This Answer Correct ?    1 Yes 1 No

hello sir,is there any function in C that can calculate number of digits in an int type variable,s..

Answer / devendra

Answer #2 is right we can also use atoi() and itoa() function.

Is This Answer Correct ?    0 Yes 0 No

hello sir,is there any function in C that can calculate number of digits in an int type variable,s..

Answer / arti sharma

void main()
{ int num,i,count=0;
printf("enter a no");
scanf("%d",&num);
while(num!=0)
{ count=count+1;
i=num%10;
num=num/10;
}
printf("count=%d",count);
getch();
}

Is This Answer Correct ?    0 Yes 0 No

hello sir,is there any function in C that can calculate number of digits in an int type variable,s..

Answer / sreejesh1987

Small change to Ramesh's answer.
I got answer when i done like this.

int countDigits(number)
{
if (number==0)
return 0;
else
return 1 + countDigits(number/10);//change % to /

}

Is This Answer Correct ?    0 Yes 1 No

hello sir,is there any function in C that can calculate number of digits in an int type variable,s..

Answer / muni

There is no function to calculate the number of bits. But
there is a very simple logic for calculating it.

int count = 0;

While( num!=0 )
{
count++;
num = num & (num-1);
}

Is This Answer Correct ?    1 Yes 9 No

Post New Answer

More C Code Interview Questions

int a = 10 + 10 .... ,... A = A * A What would be the value of A? The answer is 120!! Could anyone explain this to me.

2 Answers   Bosch, eInfochips, HCL, IHCL,


void ( * abc( int, void ( *def) () ) ) ();

1 Answers  


#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }

1 Answers   TCS,


given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit

8 Answers  


What are the files which are automatically opened when a C file is executed?

1 Answers  






Is the following code legal? struct a { int x; struct a b; }

1 Answers  


void main() { int i=i++,j=j++,k=k++; printf(ā€œ%d%d%dā€,i,j,k); }

1 Answers  


main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }

1 Answers  


main() { int x=5; for(;x!=0;x--) { printf("x=%d\n", x--); } } a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above

2 Answers   HCL,


print numbers till we want without using loops or condition statements like specifically(for,do while, while swiches, if etc)!

11 Answers   Wipro,


How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.

1 Answers  


main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }

9 Answers   CSC, GoDB Tech, IBM,


Categories