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
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 |
Answer / ramesh
int countDigits(number)
{
if (number==0)
return 0;
else
return 1 + countDigits(number%10);
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / devendra
Answer #2 is right we can also use atoi() and itoa() function.
Is This Answer Correct ? | 0 Yes | 0 No |
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 |
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 |
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 |
main() { int i=5; printf(ā%dā,i=++i ==6); }
Design an implement of the inputs functions for event mode
main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }
Is this code legal? int *ptr; ptr = (int *) 0x400;
void main() { int x,y=2,z; z=(z*=2)+(x=y=z); printf("%d",z); }
C statement to copy a string without using loop and library function..
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"); }
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.
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }
void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(ā%dā, i); }
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
Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];