Need help

Jan 24, 2012 at 11:47am
Hello everyone, i need some help with a task. It says: Type in integers A and B. Find in wich number,there are less digits 2. Then multiply them. Write a function for each number.

Ex: If A is 242 and B is 200 it should say: A is 4, because 2*2, and B is 2, because 2. If A is 278252 and B is 2424252 it should say: A is 8, because 2*2*2 and B is 16, because 2*2*2*2.

Anyone could help me? Could anyone write an algorythm for this task?
Thanks.

P.S Sorry for my bad english.
Jan 24, 2012 at 12:12pm

Turn number into string.
Check each element of string, keeping track of how many times "2" occurs.
Output 2 to the power of that number.

What's the correct output for 345? 0 or 1?
Jan 24, 2012 at 2:35pm
So, how to turn numbers into strings exactly?
Jan 24, 2012 at 2:40pm
Might be easier to check the digits directly, rather than taking the string route.

You can check the lowest digit by using "A%10" ("A mod 10", a.k.a. "the remainder of dividing A by 10", being the lowest digit in base10); you can go to the next digit by dropping the last one ("A/10") and checking the lowest digit again. Check each digit; if it's two, increment a counter. In pseudocode:

1
2
3
4
5
for each digit {
    get lowest digit;
    check if 2 -> increment counter.
    drop last digit;
}
Topic archived. No new replies allowed.