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 |
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
What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }
how to delete an element in an array
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.
#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }
How we print the table of 3 using for loop in c programing?
main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }
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().
Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }
main() { char *p = “ayqm”; char c; c = ++*p++; printf(“%c”,c); }