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() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }
void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(ā%dā,k); }
how to print 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 using any loop(for or while) only once(only 1 loop) and maximum 2 variables using C.
19 Answers Cap Gemini, Infosys,
Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ā¦ā¦..a[49][49]). Finally, print out how many zeros and ones in the diagonal.
void ( * abc( int, void ( *def) () ) ) ();
source code for delete data in array for c
#define a 10 void foo() { #undef a #define a 50 } int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } explain the answer
#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }
How we print the table of 3 using for loop in c programing?
Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.
Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }