Java program of common difference

import java.io.*;

class commondifference
{
public static void main(String args[]) throws Exception

{
int f=1,d=0,d2=0,n,r,r2;


BufferedReader br= new BufferedReader(new InputStreamReader(System.in));


System.out.println("ENTER a number");
n=Integer.parseInt(br.readLine());

do
{

r=n%10;

r2=(n/10)%10;

d=r-r2;

if(d2==0||d2==d)
d2=d;

else if (d2!=d)
 {
f=2;
break;

  }
n=n/10;

}while(n>9);



if(f==2)

System.out.println("No Common Difference");
else
System.out.println("Common difference is" + d2);
}
}

Post a Comment