RSF Program in Java

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

---------------------------------------------------------------------

Write a java program to print single digit �Reduced Subtracted


form(RSF)� of given input number.

Hint: The Reduced Subtracted form (RSF) of a given number can


be found by concatenating the differences between its adjacent
digits. To find the single digit �Reduced Subtracted form(RSF) �,
we need to continue this process till the resultant RSF is not a
single digit.

Example: if the Input number is 6928, its RSF can be found by


concatenating the difference between (6 and 9) (9 and 2) and (2 and
8) as shown below:

Difference between 6 and 9 is 3;


Difference between 9 and 2 is 7;
Difference between 2 and 8 is 6;
So the Reduced Subtracted form(RSF) is 376.
The resultant RSF is not a single digit so we must continue finding
its RSF.

Difference between 3 and 7 is 4;


Difference between 7 and 6 is 1;
The Reduced Subtracted form(RSF) is 41.
Again the resultant RSF is not a single digit so we must continue
finding its RSF.

--------------------------------------------------------------------

Solution:

import java.util.Scanner;

import org.omg.Messaging.SyncScopeHelper;

public class RSF1 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("Enter the Input Number");
int n=s.nextInt();
/*System.out.println("Enter the second Number");
int b=s.nextInt();
System.out.println(a+b);*/
// input=6789
int total=0, input1=0,r1=0,r2=0,rev=0,rsf=0;
input1=n;
while(n>0)
{
//rem=(n%10);
rev=(rev*10)+(n%10);
n/=10;
}
//System.out.println("RFeverse is:"+rev);
String s1="";
while(rev>0 ||(rsf>=10))
{
//System.out.println("rev="+rev);
if(rev==0)
{
rev=rsf;
rsf=0;
r1=0; r2=0;
}
if(r1==0)
{
r1=rev%10;
rev/=10;
//System.out.println("rev="+rev);
}
r2=rev%10;
//System.out.println("r1="+r1);
rsf=(rsf*10)+Math.abs(r1-r2);
// System.out.println("r2="+r2);
rev/=10;
r1=r2;
// System.out.println("rsf="+rsf);
}
System.out.println("RSF is:"+rsf);
}
}

You might also like