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

what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);

2 Answers  


main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }

2 Answers  


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 main() { int x,y=2,z; z=(z*=2)+(x=y=z); printf("%d",z); }

4 Answers  


prog. to produce 1 2 3 4 5 6 7 8 9 10

4 Answers   TCS,






#include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }

1 Answers  


main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }

1 Answers  


how to swap 3 nos without using temporary variable

4 Answers   Satyam,


void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above

1 Answers   HCL,


Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

2 Answers   Wipro,


main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above

5 Answers   HCL,


Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a { int x; aType *b; }; }

1 Answers  


Categories