-
-
Save rozer007/6c9d55a337a1d8aa6fc00b17e24c8510 to your computer and use it in GitHub Desktop.
Count Digits In A Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1) Why is the while loop condition (n>0) not (n>=0)? | |
ans: If n==0 then there is no more digit present so, if you keep (n>=0) then it will give one more count extra. | |
Therefore, we are using n>0. | |
Q2) Why while loop not for loop? | |
ans: We can use any of the loop,even if you use for loop it will give you same solution. | |
Q3) why are we doing n=n/10? | |
ans: When we divide a number by 10, the digit in that number is decrease by one so, by dividing the count of digit present in that number. | |
Q4) Difference between count++ and count=count+1? | |
ans: Both count++ and count = count+1 means the variable count is increase by one. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hint 1: Try to use division method | |
Hint 2: Remember to increase count | |
Hint 3: Beware of Overflow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1) Fill the blank in according to Counts digit problem ? | |
while(n>0) | |
{ | |
count++; | |
_______; | |
} | |
a) n=n%10 | |
b) n=n*10 | |
c) n=n/10 | |
d) n=n%10+1 | |
ans : d) | |
Q2) choose the correct one: | |
while(n>0) | |
{ | |
count++; | |
} | |
System.out.println(count); | |
a) 1 | |
b) compilation error | |
c) runtime error | |
d) 10 | |
ans: c) | |
Q3) choose the correct output: | |
int n=1234; | |
int count=0; | |
while(true) | |
{ | |
count++; | |
if(count>3) | |
break; | |
if(count>2) | |
break; | |
n=n/10; | |
} | |
System.out.print(count); | |
a) 2 | |
b) 4 | |
c) 3 | |
d) 0 | |
ans: c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment