Saturday, December 10, 2016

Twist Number Program Using BlueJ

BlueJ Program on Twist Number checking. In this BlueJ program, the consecutive digits in a number will exchange their positions. If the number has odd number of digits then the last digit would remain the same

Example: Enter the Number: 123456
Original : 123456
Modified : 214365
(The above number has 06 Digits)


Enter the Number: 123
Original : 123
Modified : 213
(The above number has 03 Digits)


 import java.io.*;
class Twist
{
int n,n1;
int i,len;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
System.out.print("\nEnter the Number:");
n=Integer.parseInt(br.readLine());
}
int countDigits(int m)
{
 int c=0;
for(int i=m;i> 0;i=i/10)
c++;
return c;
}
public void show()
{
 System.out.println("Original Number:"+n);
 System.out.println("Modified Number:"+n1);
}
public void exchange()
{
 len=countDigits(n);
System.out.println("Length="+len);
if(len%2!=0)
 n1=n;
else
{
for(int i=n;i> 0;i=i/100)
 n1=n1*100+i%100;
n1=rev(n1);
}
}
private int rev(int x)
{
 int r=0;
for(int i=x;i> 0;i=i/10)
r=r*10+i%10;
return r;
}
 public static void main(String args[]) throws Exception
{
Twist ob=new Twist();
ob.take();
ob.exchange();
ob.show();
 }
}

Related PostBlueJ Programs on Number

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner