Posted by Unknown on 8:17 PM
Posted by Unknown on 6:42 AM
Question : A fancy number is a number in the
sequence 1, 1, 5, 17, 61, … .Note
that first two fancy numbers are 1 and any fancy number other than the first two is sum
of thethree times previous one and two times the one before that.
See below:
1,
1,
3*1 +2*1 = 5
3*5 +2*1 = 17
3*17 + 2*5 = 61
Write a function named isFancy that returns 1 if its integer
argument is a Fancy number, otherwise it returns 0.
The signature of the function is
int
isFancy(int n)
Solution :
public static int
isFancy(int n)
{
int a = 1,
b = 1, c = 0;
if (n ==
1)
return 1;
while(c<n)
{
c = 3 * b + 2 * a;
a =b;
b =c;
}
if(c==n)
{
return 1;
}
else
{
return 0;
}
}
Posted by Unknown on 12:55 AM
Question :
Write a function named maxOccurDigit that returns the digit that occur the most. If there is no such digit, it will return -1. For example maxOccurDigit(327277) would return 7 because 7 occurs three times in the number and all other digits occur less than three times. Other examples:
maxOccurDigit(33331) returns 3
maxOccurDigit(3232, 6) returns -1
maxOccurDigit(5) returns 5
maxOccurDigit(-9895) returns 9
The function signature is
maxOccurDigit(int n)
Solution:
public static int maxOccurDigit(int n)
{
int rem1 = 0, count = 0, rem2 = 0,
temp = 0,maxCount = 0, maxNumber = 0;
if (n < 0)
n = -1 * n;
while (n != 0)
{
rem1 = n % 10;
count = 1;
temp = n = n / 10;
while (temp != 0)
{
rem2 = temp % 10;
temp = temp / 10;
if (rem1 == rem2)
{
count++;
}
}
if (maxCount < count)
{
maxCount = count;
maxNumber = rem1;
}
else if (maxCount == count)
{
return -1;
}
}
return maxNumber;
}