Monday 30 September 2013

Program to check the difference b/w the highest number and lowest number(formed by digits in givenNumber) is equal to given number or not ? if not, choose the difference result as the new input and repeat the process untill the difference equals given number

//java code -- to understand this you need to have little knowledge on Collections interface and ArrayList class, can be found in java.util package
Here am giving two solutions with recursion and without recursion

//with recursion

import java.util.*;
class Calculation
{

static int findHighestOrLeast(int num,boolean highOrLow)   //true - highest and false - least
{
List<Integer> l = new ArrayList<Integer>();
while( num > 0 )
{
l.add(num%10);
num /= 10;
}
Collections.sort(l);
if(highOrLow)
Collections.reverse(l);
num = 0;
for(int i : l )
num = num*10 + i;
return num;

}
static int findNumber(int num)
{
int num2=0;

num2 = findHighestOrLeast(num,true) - findHighestOrLeast(num,false);
if(num == num2)
{
return num;
}
else
{
return findNumber(num2);
}
}
public static void main(String[] args)
{
int num1 = 7624;
System.out.println(findNumber(num1));
}
}

//without recursion

import java.util.*;
class Calculation
{
static int leastPossibleNumber(int num)
{
List<Integer> l = new ArrayList<Integer>();
while( num > 0 )
{
l.add(num%10);
num /= 10;
}
Collections.sort(l);
num = 0;
for(int i : l )
num = num*10 + i;
return num;
}
static int highestPossibleNumber(int num)
{
List<Integer> l = new ArrayList<Integer>();
while( num > 0 )
{
l.add(num%10);
num /= 10;
}
Collections.sort(l);
Collections.reverse(l);
num = 0;
for(int i : l )
num = num*10 + i;
return num;
}
public static void main(String[] args)
{
int num1 = 7624;
int num2=0;
while( true )
{
num2 = highestPossibleNumber(num1) - leastPossibleNumber(num1);
if(num1 == num2)
{
System.out.println(num1 + "  " + num2);
break;
}
else
{
num1 = num2;
}
}
}
}

instead of having two methods leastPossibleNumber and highestPossibleNumber, we can have one method findLeastOrHighest with an extra boolean argument. if you are keen about reusability then you can plan that way.

Program to print all the twin prime combinations b/w the given inputs

//java code

class PrintTwinPrimes{

public static void main(String[] args) {
int num1 = 0,num2 = 0;
int input1 = Integer.parseInt(args[0]);
int input2 = Integer.parseInt(args[1]);
for( int i = input1 ; i <= input2 ; i++)
{
if(isPrime(i))
{
num2 = num1;
num1 = i;
}
if( (num1 - num2) == 2 )
{
System.out.println(num1+"  "+num2);
num2 = 0;
}
}
}
static boolean isPrime(int num)
{
for(int i = 2 ; i <= num/2 ; i++ )
{
if( num % i == 0 )
{
return false;
}
}
return true;
}
}

// compile
javac PrintTwinPrimes.java
// run
java PrintTwinPrimes 2 1000

-- at the time of running the program need to give inputs, otherwise ArrayIndexOutOfBoundException will raise
or else you can validate the program instead

public static void main(String[] args) {
if(args.length != 2) {
System.out.println("Enter only two integer inputs");
return;
}
int num1 = 0,num2 = 0;
int input1 = Integer.parseInt(args[0]);
int input2 = Integer.parseInt(args[1]);
for( int i = input1 ; i <= input2 ; i++)
----------------
---------------

similarly if you want a robust program, you can validate that the first input should be smaller than second input

Program to find whether a given number is Palindrome or not ? if not add the number to the reverse of it, and check the sum for palindrome, repeat this until we get a palindrome

// code in java
class findPalindrome
{
public static void main(String[] args)
{
int num = 165;

System.out.println(findNumber(num));
}
static int findNumber(int num)
{
if(isPalindrome(num))
{
return num;
}
else
{
return findNumber(num + reverseNumber(num));
}
}
static int reverseNumber(int num)
{
int newNum = 0;
while(num > 0)
{
newNum = newNum*10 + num % 10;
num /= 10;
}
return newNum;
}
static boolean isPalindrome(int num)
{
return num == reverseNumber(num);
}
}