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 |
Declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }
union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0
write a program to find out roots of quadratic equation "x=-b+-(b^2-4ac0^-1/2/2a"
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.
How to swap two variables, without using third variable ?
104 Answers AB, ADP, BirlaSoft, Cisco, Cygnet Infotech, HCL, Hewitt, Honeywell, HP, IBM, Infosys, Manhattan, Microsoft, Mobius, Percept, Satyam, SofTMware, TCS, Wipro, Yamaha,
write a c program to Create a registration form application by taking the details like username, address, phone number, email along with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 5 users and display the details. In place of password display “****”. (Use Structures).
0 Answers CDAC, College School Exams Tests,
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }
main() { int i=5; printf(“%d”,i=++i ==6); }
func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); }
main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10
main() { extern int i; i=20; printf("%d",sizeof(i)); }