Expedia Interview Question
Software Engineer in TestsCountry: United States
Remove the last digit of the number, double that digit and subtract from the remainder. Continue this till left with one digit. If that digit is 7/-7/0, you have a winner.
e.g.
14
Remove 4 and double it i.e. 8
Subtract 8 from 1, we get -7 which is awesome.
35 => 3-10 = -7
154 => 15 - 8 = 7
147 => 14-14 = 0
A number of the form 10x + y is divisible by 7 if and only if x − 2y is divisible by 7. In other words, subtract twice the last digit from the number formed by the remaining digits. Continue to do this until a number known to be divisible by 7 is obtained. The original number is divisible by 7 if and only if the number obtained using this procedure is divisible by 7. For example, the number 371: 37 − (2×1) = 37 − 2 = 35; 3 − (2 × 5) = 3 − 10 = −7; thus, since −7 is divisible by 7, 371 is divisible by 7.
Reference : en.wikipedia.org/wiki/Divisibility_rule#Divisibility_by_7
/*The code is as follows*/
- RUSHIT PARIKH September 29, 2012int size(int n)
{
int c=1;
if(n/10!=0)
{
n=n/10;
c=c+1;
}
return c;
}
void main()
{
int n,i,l,k;
cin>>n;
m=size(n);
for(i=0;i<m;i++)
{
if(n%10!=0)
{
k=n%10;
n=n/10;
l=n-2k;
if(l%7==0)
{
cout<<"divisible";
break;
}
else
{
cout<<"Not Divisible";
break;
}
}
else
{
if(n>=0)
{
n=n/10;
}
else
{
cout<<"Not divisible";
break;
}
}