Java Code 3 _ Two - digit " Reduced Subtracted Form " [Wipro Interview Question]
Question:
Given a number , you are expected to find its two digit "Reduced Subtracted Form ( RSF) "
The RSF of a number can be found by concatenating the difference between its adjacent digits.
To find two - digit "Reduced subtracted form ( RSF )" , we need to continue this process till the resultant RSF is not a two digit number .
example :
input number : 6928
(6 - 9) is 3
(9 - 2) is 7
(2 - 8) is 6
so the reduced RSF of 6928 is 376
(3 - 7) is 4
(7 - 6) is 1
so the reduced RSF of 376 is 41
so the output :
376
41
Note:
input number >= 100 .
while concatenating the differences use the absolute value(non negative value).
Program:
package numsub;
import java.util.Scanner;
public class NumSub
{
static int subtractor(int n)
{
int result=0,temp,temp1,diff,pow=1;
while(n>9)
{
temp=n%10;
temp1=(n/10)%10;
diff=(temp<temp1)?(temp1-temp):(temp-temp1);//Math.abs(temp1-temp)
result=result+diff*pow;
pow*=10;
n/=10;
}
return result;
}
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
if(n>99)
{
while(n>99)
{
n=subtractor(n);
System.out.println(n);
}
}
else
System.out.println(n);
}
}
Output:
Thank You ...
********* The above code is contributed by AjayRam KV *********
0 comments: