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

posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come

2 Answers   GATE,


What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }

1 Answers  


how to delete an element in an array

2 Answers   IBM,


print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!!

35 Answers   Tata Elxsi, TCS, VI eTrans,


1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.

3 Answers  






#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

2 Answers  


How we print the table of 3 using for loop in c programing?

7 Answers  


main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }

3 Answers  


int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().

1 Answers  


Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

1 Answers  


int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }

3 Answers   Cisco, HCL,


main() { char *p = “ayqm”; char c; c = ++*p++; printf(“%c”,c); }

1 Answers  


Categories