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

main() { int i=5; printf(ā€œ%dā€,i=++i ==6); }

1 Answers  


Design an implement of the inputs functions for event mode

0 Answers   Wipro,


main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

1 Answers  


Is this code legal? int *ptr; ptr = (int *) 0x400;

1 Answers  


void main() { int x,y=2,z; z=(z*=2)+(x=y=z); printf("%d",z); }

4 Answers  






C statement to copy a string without using loop and library function..

2 Answers   Persistent, TCS,


main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }

1 Answers  


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; for(;i++;printf("%d",i)) ; printf("%d",i); }

1 Answers   Zoho,


void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(ā€œ%dā€, i); }

2 Answers  


main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000

3 Answers   HCL,


Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];

1 Answers  


Categories