Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- int isBaseFour(int num)
- {
- int a;
- a = num % 10;
- if (num == 0)
- return 1;
- if (a>3 || a<0)
- return 0;
- return isBaseFour(num/10);
- }
- int toDecimalIterate(int num)
- {
- int sum = 0;
- if (isBaseFour(num) == 0)
- return -1;
- for (int i=0; num > 0; i++) {
- sum = sum + (num % 10) * pow(4.0, i);
- num = num/10 ; }
- return sum;
- }
- int four (int num , int i=0)
- {
- int a ;
- if (num == 0)
- return 0;
- a = (num % 10) * pow(4.0, i);
- return a + four(num/10, i++);
- }
- int toDecimal(int num)
- {
- if (isBaseFour(num) == 0)
- return -1;
- return four(num);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement