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 = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

1 Answers   CSC,


respected sir, i did my MCA in 2013 when i am going to attend to an interview i was asked about my project how will i explain my project could please help me in this and my project title is "Social Networking Site For Social Responsibility"

1 Answers   Genpact, Ozdocs,


Write a program to print a square of size 5 by using the character S.

6 Answers   Microsoft,


write a program to Insert in a sorted list

4 Answers   Microsoft,


How to read a directory in a C program?

4 Answers  






Write a routine to implement the polymarker function

0 Answers   TCS,


main() { printf("%x",-1<<4); }

3 Answers   HCL, Sokrati, Zoho,


Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

13 Answers   Intel, Microsoft, TCS,


What is the hidden bug with the following statement? assert(val++ != 0);

1 Answers  


#include<stdio.h> void fun(int); int main() { int a; a=3; fun(a); printf("\n"); return 0; } void fun(int i) { if(n>0) { fun(--n); printf("%d",n); fun(--n); } } the answer is 0 1 2 0..someone explain how the code is executed..?

1 Answers   Wipro,


Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

2 Answers   Ace Info,


What is your nationality?

1 Answers   GoDB Tech,


Categories