We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int beautifulDays(int i, int j, int k) {
int num,count=0,gn;
int quo, newNum = 0, x = 0;
for (int x = i; x <= j ; x++)
{
num = x; //make sure to assign this
newNum = 0;
//For reversing no
while (num != 0)
{
quo = num % 10;
newNum = 10 * newNum + quo;
num = num / 10;
}
gn = abs(x- newNum);
if (gn%k==0)
{
count++;
}
}
return count;
}
the correct code for reversing (from above commented code)
num=x //assignment of the temp number
while(num!=0)
{
//code
}
if u dont assign a new variable(num)
that leads to changing the value of for loop initiailizer (x) to 0 at end of the loop ,which makes the loop infinite.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Days at the Movies
You are viewing a single comment's thread. Return to all comments →
int beautifulDays(int i, int j, int k) { int num,count=0,gn; int quo, newNum = 0, x = 0; for (int x = i; x <= j ; x++) { num = x; //make sure to assign this newNum = 0; //For reversing no while (num != 0) { quo = num % 10; newNum = 10 * newNum + quo; num = num / 10; } gn = abs(x- newNum); if (gn%k==0) { count++; } } return count; }
the correct code for reversing (from above commented code)
num=x //assignment of the temp number while(num!=0) { //code } if u dont assign a new variable(num) that leads to changing the value of for loop initiailizer (x) to 0 at end of the loop ,which makes the loop infinite.