Saturday, March 19, 2011

WAP for Given a string s and an array of smaller strings T, design a method to search s for each small string in T.

First, create a suffix tree for s. For example, if your word were bibs, you would create the following
tree:

import java.util.*;
class SuffixTree
{
SuffixTreeNode root = new SuffixTreeNode();

public SuffixTree(String s)
{

for(int i = 0; i < s.length(); i++) { String suffix = s.substring(i); root.insertString(suffix, i); } } public ArrayList getIndexes(String s)
{
return root.getIndexes(s);
}
}

class SuffixTreeNode
{
HashMap children = new HashMap();
char value;
ArrayList indexes = new ArrayList();


public SuffixTreeNode() { }

public void insertString(String s, int index)
{
indexes.add(index);

if (s != null && s.length() > 0)
{
value = s.charAt(0);
SuffixTreeNode child = null;

if (children.containsKey(value))
{
System.out.println("exist -" + value);
child = children.get(value);
}
else
{
System.out.println("1st time -" + value);
child = new SuffixTreeNode();
children.put(value, child);
}

String remainder = s.substring(1);
child.insertString(remainder, index);
}
}

public ArrayList getIndexes(String s)
{
if (s == null || s.length() == 0)
{
return indexes;
}
else
{
char first = s.charAt(0);
if (children.containsKey(first))
{
String remainder = s.substring(1);
return children.get(first).getIndexes(remainder);
}
}
return null;
}
}

public class Question
{
public static void main(String[] args)
{
String testString = "ibabibsabs";//"mississippi";
String[] stringList = {"ib","bs"};//{"is", "sip", "hi", "sis"};
SuffixTree tree = new SuffixTree(testString);
for (String s : stringList)
{
ArrayList list = tree.getIndexes(s);
if (list != null)
{
System.out.println(s + ": " + list.toString());
}
}
}
}

Program Developed By Galye ( CCup Founder)

One more Link for Suffix Tree
http://marknelson.us/1996/08/01/suffix-trees/
http://sfxdisk.dead-inside.org/
http://stackoverflow.com/questions/398811/finding-long-repeated-substrings-in-a-massive-string

WAP to Check Integer OverFlow in Differnt Logical & Arithmetic Operation

Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.

Method 1
There can be overflow only if signs of two numbers are same, and sign of sum is opposite to the signs of numbers.

1) Calculate sum
2) If both numbers are positive and sum is negative then return -1
Else
If both numbers are negative and sum is positive then return -1
Else return 0

?
#include
#include

/* Takes pointer to result and two numbers as
arguments. If there is no overflow, the function
places the resultant = sum a+b in “result” and
returns 0, otherwise it returns -1 */
int addOvf(int* result, int a, int b)
{
*result = a + b;
if(a > 0 && b > 0 && *result < 0)
return -1;
if(a < 0 && b < 0 && *result > 0)
return -1;
return 0;
}

int main()
{
int *res = (int *)malloc(sizeof(int));
int x = 2147483640;
int y = 10;

printf("%d", addOvf(res, x, y));

printf("\n %d", *res);
getchar();
return 0;
}

Time Complexity : O(1)
Space Complexity: O(1)


Method 2

#include
#include
#include

int addOvf(int* result, int a, int b)
{
if( a > INT_MAX - b)
return -1;
else
{
*result = a + b;
return 0;
}
}

int main()
{
int *res = (int *)malloc(sizeof(int));
int x = 2147483640;
int y = 10;

printf("%d", addOvf(res, x, y));
printf("\n %d", *res);
getchar();
return 0;
}

Time Complexity : O(1)
Space Complexity: O(1)


Source http://joyofprogramming.com/Docs_ColumnArticles/27-JoP-Mar-09.pdf

WAP to Compute modulus division by a power-of-2-number

Compute n modulo d without division(/) and modulo(%) operators, where d is a power of 2 number.

Let ith bit from right is set in d. For getting n modulus d, we just need to return 0 to i-1 (from right) bits of n as they are and other bits as 0.

For example if n = 6 (00..110) and d = 4(00..100). Last set bit in d is at position 3 (from right side). So we need to return last two bits of n as they are and other bits as 0, i.e., 00..010.

#include

/* This function will return n % d.
d must be one of: 1, 2, 4, 8, 16, 32, … */
unsigned int getModulo(unsigned int n, unsigned int d)
{
return ( n & (d-1) );
}

/* Driver program to test above function */
int main()
{
unsigned int n = 6;
unsigned int d = 4; /*d must be a power of 2*/
printf("%u moduo %u is %u", n, d, getModulo(n, d));

getchar();
return 0;
}


Source http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs

Compute the minimum or maximum of two integers without branching

/* The obvious approach to find minimum (involves branching) */
int min(int x, int y)
{
return (x < y) ? x : y
}

Below are the methods to get minimum(or maximum) without using branching. Typically, the obvious approach is best, though.

Method 1(Use XOR and comparison operator)

Minimum of x and y will be

y ^ ((x ^ y) & -(x < y))

It works because if x < y, then -(x < y) will be all ones, so r = y ^ (x ^ y) & ~0 = y ^ x ^ y = x. Otherwise, if x >= y, then -(x < y) will be all zeros, so r = y ^ ((x ^ y) & 0) = y. On some machines, evaluating (x < y) as 0 or 1 requires a branch instruction, so there may be no advantage.

To find the maximum, use

x ^ ((x ^ y) & -(x < y));

?
#include

/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y ^ ((x ^ y) & -(x < y));
}

/*Function to find maximum of x and y*/
int max(int x, int y)
{
return x ^ ((x ^ y) & -(x < y));
}

/* Driver program to test above functions */
int main()
{
int x = 15;
int y = 6;
printf("Minimum of %d and %d is ", x, y);
printf("%d", min(x, y));
printf("\nMaximum of %d and %d is ", x, y);
printf("%d", max(x, y));
getchar();
}

Method 2(Use subtraction and shift)
If we know that

INT_MIN <= (x - y) <= INT_MAX

, then we can use the following, which are faster because (x - y) only needs to be evaluated once.

Minimum of x and y will be

y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

This method shifts the subtraction of x and y by 31 (if size of integer is 32). If (x-y) is smaller than 0, then (x -y)>>31 will be 1. If (x-y) is greater than or equal to 0, then (x -y)>>31 will be 0.
So if x >= y, we get minimum as y + (x-y)&0 which is y.
If x < y, we get minimum as y + (x-y)&1 which is x.

Similarly, to find the maximum use

x - ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)))

?
#include
#define CHAR_BIT 8

/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y + ((x - y) & ((x - y) >>
(sizeof(int) * CHAR_BIT - 1)));
}

/*Function to find maximum of x and y*/
int max(int x, int y)
{
return x - ((x - y) & ((x - y) >>
(sizeof(int) * CHAR_BIT - 1)));
}

/* Driver program to test above functions */
int main()
{
int x = 15;
int y = 6;
printf("Minimum of %d and %d is ", x, y);
printf("%d", min(x, y));
printf("\nMaximum of %d and %d is ", x, y);
printf("%d", max(x, y));
getchar();
}

Source http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax

Compute the minimum or maximum of two integers without branching

/* The obvious approach to find minimum (involves branching) */
int min(int x, int y)
{
return (x < y) ? x : y
}

Below are the methods to get minimum(or maximum) without using branching. Typically, the obvious approach is best, though.

Method 1(Use XOR and comparison operator)

Minimum of x and y will be

y ^ ((x ^ y) & -(x < y))

It works because if x < y, then -(x < y) will be all ones, so r = y ^ (x ^ y) & ~0 = y ^ x ^ y = x. Otherwise, if x >= y, then -(x < y) will be all zeros, so r = y ^ ((x ^ y) & 0) = y. On some machines, evaluating (x < y) as 0 or 1 requires a branch instruction, so there may be no advantage.

To find the maximum, use

x ^ ((x ^ y) & -(x < y));

?
#include

/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y ^ ((x ^ y) & -(x < y));
}

/*Function to find maximum of x and y*/
int max(int x, int y)
{
return x ^ ((x ^ y) & -(x < y));
}

/* Driver program to test above functions */
int main()
{
int x = 15;
int y = 6;
printf("Minimum of %d and %d is ", x, y);
printf("%d", min(x, y));
printf("\nMaximum of %d and %d is ", x, y);
printf("%d", max(x, y));
getchar();
}

Method 2(Use subtraction and shift)
If we know that

INT_MIN <= (x - y) <= INT_MAX

, then we can use the following, which are faster because (x - y) only needs to be evaluated once.

Minimum of x and y will be

y + ((x - y) & ((x - y) >>(sizeof(int) * CHAR_BIT - 1)))

This method shifts the subtraction of x and y by 31 (if size of integer is 32). If (x-y) is smaller than 0, then (x -y)>>31 will be 1. If (x-y) is greater than or equal to 0, then (x -y)>>31 will be 0.
So if x >= y, we get minimum as y + (x-y)&0 which is y.
If x < y, we get minimum as y + (x-y)&1 which is x.

Similarly, to find the maximum use

x - ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)))

?
#include
#define CHAR_BIT 8

/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y + ((x - y) & ((x - y) >>
(sizeof(int) * CHAR_BIT - 1)));
}

/*Function to find maximum of x and y*/
int max(int x, int y)
{
return x - ((x - y) & ((x - y) >>
(sizeof(int) * CHAR_BIT - 1)));
}

/* Driver program to test above functions */
int main()
{
int x = 15;
int y = 6;
printf("Minimum of %d and %d is ", x, y);
printf("%d", min(x, y));
printf("\nMaximum of %d and %d is ", x, y);
printf("%d", max(x, y));
getchar();
}

Note that the 1989 ANSI C specification doesn't specify the result of signed right-shift, so above method is not portable. If exceptions are thrown on overflows, then the values of x and y should be unsigned or cast to unsigned for the subtractions to avoid unnecessarily throwing an exception, however the right-shift needs a signed operand to produce all one bits when negative, so cast to signed there.

WAP to Print Next Greater Elemnt in Given UnSorted Array

Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as -1.

Algorithm

1) Push the first element to stack.
2) Pick rest of the elements one by one and follow following steps in loop.
....a) Mark the current element as next.
....b) If stack is not empty, then pop an element from stack and compare it with next.
....c) If next is greater than the popped element, then next is the next greater element fot the popped element.
....d) Keep poppoing from the stack while the popped element is smaller than next. next becomes the next greater element for all such popped elements
....g) If next is smaller than the popped element, then push the popped element back.
3) After the loop in step 2 is over, pop all the elements from stack and print -1 as next element for them.
?
#include
#include
#define STACKSIZE 100

// stack structure
struct stack
{
int top;
int items[STACKSIZE];
};

// Stack Functions to be used by printNGE()
void push(struct stack *ps, int x)
{
if (ps->top == STACKSIZE-1)
{
printf("Error: stack overflow\n");
getchar();
exit(0);
}
else
{
ps->top += 1;
ps->items[ps->top] = x;
}
}

bool isEmpty(struct stack *ps)
{
return (ps->top == -1)? true : false;
}

int pop(struct stack *ps)
{
int temp;
if (ps->top == -1)
{
printf("Error: stack underflow \n");
getchar();
exit(0);
}
else
{
temp = ps->items[ps->top];
ps->top -= 1;
return temp;
}
}

/* prints element and NGE pair for all elements of
arr[] of size n */
void printNGE(int arr[], int n)
{
int i = 0;
struct stack s;
s.top = -1;
int element, next;

/* push the first element to stack */
push(&s, arr[0]);

// iterate for rest of the elements
for (i=1; i {
next = arr[i];

if (isEmpty(&s) == false)
{
// if stack is not empty, then pop an element from stack
element = pop(&s);

/* If the popped element is smaller than next, then
a) print the pair
b) keep popping while elements are smaller and
stack is not empty */
while (element < next)
{
printf("\n %d --> %d", element, next);
if(isEmpty(&s) == true)
break;
element = pop(&s);
}

/* If element is greater than next, then push
the element back */
if (element > next)
push(&s, element);
}

/* push next to stack so that we can find
next greater for it */
push(&s, next);
}

/* After iterating over the loop, the remaining
elements in stack do not have the next greater
element, so print -1 for them */
while(isEmpty(&s) == false)
{
element = pop(&s);
next = -1;
printf("\n %d --> %d", element, next);
}
}

/* Driver program to test above functions */
int main()
{
int arr[]= {11, 13, 21, 3};
int n = sizeof(arr)/sizeof(arr[0]);
printNGE(arr, n);
getchar();
return 0;
}

Output:

11 --> 13
13 --> 21
3 --> -1
21 --> -1

Time Complexity: O(n). The worst case occurs when all elements are sorted in decreasing order. If elements are sorted in decreasing order, then every element is processed at most 4 times.
a) Initialy pushed to the stack.
b) Popped from the stack when next element is being processed.
c) Pushed back to the stack because next element is smaller.
d) Popped from the stack in step 3 of algo.

Run Here https://ideone.com/z8Zpc

Approach & Solution Provided by Pchild

WAP to Given any two words, find the shortest distance (in terms of number of words) between them in the file.

You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. Can you make the searching operation in O(1) time? What about the space complexity for your solution?


We will assume for this question that the word order does not matter. This is a question you should ask your interviewer. If the word order does matter, we can make the small modification
shown in the code below.

class min_dist_two_words
{
public static void main(String a[])
{

System.out.println(shortest(new String[]{"abc","def","gh","cd","fe"},"gh","fe"));

}
static int shortest(String[] words, String word1, String word2)
{
int pos = 0;
int min = Integer.MAX_VALUE / 2;
int word1_pos = -min;
int word2_pos = -min;
for (int i = 0; i < words.length; i++) {
String current_word = words[i];
if (current_word.equals(word1)) {
word1_pos = pos;
// Comment following 3 lines if word order matters
int distance = word1_pos - word2_pos;
if (min > distance)
min = distance;
} else if (current_word.equals(word2)) {
word2_pos = pos;
int distance = word2_pos - word1_pos;
if (min > distance) min = distance;
}
++pos;
}
return min;
}

}


Time Complexity O(n)
Space Complexity O(1)


To solve this problem in less time (but more space), we can create a hash table with each word and the locations where it occurs. We then just need to find the minimum (arithmetic) difference in the locations (e.g., abs(word0.loc[1] - word1.loc[5])).
To find the minimum arithmetic difference, we take each location for word1 (e.g.: 0, 3} and do a modified binary search for it in word2’s location list, returning the closest number. Our search for 3, for example, in {2, 7, 9} would return 1. The minimum of all these binary searches is the shortest distance.

Time Complexity O(1)
Space Complexity O(n)

Soln Provided By Gayle

Friday, March 18, 2011

WAP to Find all Possible Palindromes in a String Efficiently

This is not the regular palindrome finder. Given a string, we should be able to print all the possible palindromes. Let me quickly give a brief example.
For the following text : abccbab
All the possible palindromes are,
bab
abccba
bccb
cc


However, there is even a simpler solution available. First do a parse for odd occurring palindromes followed by even palindromes.For odd palindromes run through each character from the text. For each character, see if there the pre and post occuring characters are equal, if they are equal print them and do the same for the next levels. In the following example shown below, assume you are at 'y' and see the previous and next characters are equal. If they are see further more until the palindrome functionality ceases. Print all of them whilst this time.

java Program


class FindAllPalindromes {
 public static void main(String[] args){
  FindAllPalindromes finder = new FindAllPalindromes();
  finder.printAllPalindromes("abcddcbaABCDEDCBA");
 }
  
 public void printAllPalindromes(String inputText){
  if(inputText==null){
   System.out.println("Input cannot be null!");
   return;
  }
  if(inputText.length()<=2){
   System.out.println("Minimum three characters should be present");
  }
  //ODD Occuring Palindromes
  int len = inputText.length();
  for(int i=1;i<len-1;i++){
   for(int j=i-1,k=i+1;j>=0&&k<len;j--,k++){
    if(inputText.charAt(j) == inputText.charAt(k)){
     System.out.println(inputText.subSequence(j,k+1));
    }else{
     break;
    }
   }
  }
  //EVEN Occuring Palindromes
  for(int i=1;i<len-1;i++){
   for(int j=i,k=i+1;j>=0&&k<len;j--,k++){
    if(inputText.charAt(j) == inputText.charAt(k)){
     System.out.println(inputText.subSequence(j,k+1));
    }else{
     break;
    }
   }
  }
 
 }
}


/*
Sample Output:
DED
CDEDC
BCDEDCB
ABCDEDCBA
dd
cddc
bcddcb
abcddcba
*/


Time Complexity O(n)
Space Complexity O(n)

Run Here https://ideone.com/weINb

C++program
#include<string.h>
#include <iostream>
using namespace std;
 
void printAllPalindromes(char*);
 
char* subSequence(char*,int,int);
 
int main() {
 char s[] = "abcddcbaABCDEDCBA";
 printAllPalindromes(s);
 return 0;
}
 
char* subSequence(char* mainSequence, int from, int to){
 char * tgt = new char[to-from+1];
 for(int i=0;i<(to-from);i++){
  tgt[i] = mainSequence[i+from];
 }
 tgt[to-from] = '\0';
 return tgt;
}
 
void printAllPalindromes(char* inputText) {
 if(!inputText) {
  printf("Input cannot be null!");
  return;
 }
 if(strlen(inputText)<=2) {
  printf("Minimum three characters should be present\n");
 }
 //ODD Occuring Palindromes
 int len = strlen(inputText);
 for(int i=1;i<len-1;i++) {
  for(int j=i-1,k=i+1;j>=0&&k<len;j--,k++) {
   if(inputText[j] == inputText[k]) {
    char* subSeq = subSequence(inputText,j,k+1);
    cout<<subSeq<<endl;
    delete subSeq;
   } else {
    break;
   }
  }
 }
 //EVEN Occuring Palindromes
 for(int i=0;i<len-1;i++) {
  for(int j=i,k=i+1;j>=0&&k<len;j--,k++) {
   if(inputText[j] == inputText[k]) {
    char* subSeq = subSequence(inputText,j,k+1);
    cout<<subSeq<<endl;
    delete subSeq;
   } else {
    break;
   }
  }
 }
 
}

/*
Sample Output:
DED
CDEDC
BCDEDCB
ABCDEDCBA
dd
cddc
bcddcb
abcddcba
*/


Time Complexity O(n)
Space Complexity O(n)
Run Here https://ideone.com/WhZzz

Wednesday, March 16, 2011

WAP to Sort Most Freq..Appearing Word Based on Their Frequency From Non Incresing Order

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class WordCounting
{
static final Integer ONE = new Integer(1);

public static void main(String args[]) throws IOException {
Hashtable map = new Hashtable();
FileReader fr = new FileReader(args[0]);
BufferedReader br = new BufferedReader(fr);
String line;
StringTokenizer st;
while ((line = br.readLine()) != null)
{

st= new StringTokenizer(line," ");

while (st.hasMoreTokens())
{
String word=st.nextToken();
Object obj = map.get(word);

if (obj == null)
{
map.put(word, ONE);
}
else
{
int i = ((Integer) obj).intValue() + 1;
map.put(word, new Integer(i));
}
//word=null;
}

}

Enumeration e = map.keys();
while (e.hasMoreElements())
{
String key = (String) e.nextElement();
System.out.println(key + " : " + map.get(key));
}


//sort According to Freqency of valuew of each word

Collection set = map.values();
Iterator iter = set.iterator();
Integer ar[]=new Integer[map.size()];
int i=0;
while (iter.hasNext())
{
ar[i]=(Integer)iter.next();

i++;
}


quickSort(ar,0,i);

for(i=0;i System.out.println(ar[i]);


//After Sorting Printing According to Freq from Higer to Lower


/* Enumeration e = map.keys();
while (e.hasMoreElements())
{
String key = (String) e.nextElement();
System.out.println(key + " : " + map.get(key));
}*/






}


private static void quickSort(Integer arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSort(arr, left, index - 1);
if (index < right)
quickSort(arr, index, right);
}

private static int partition(Integer arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
return i;
}




}

Base System Conversion BInary--Octa-Decinal-HexaDecimal

Program for Binary, Octal, Hexadecimal Conversions

Program for Binary, Octal, Hexadecimal Conversions

#include
#include
#include

void bd();
void db();
void doc();
void dh();
void od();
void ob();
void bo();
void bh();
void hb();
void hd();
void oh();
void ho();

void main()
{
int n;
char c;
begin:
clrscr();
printf(" ****MAIN MENU****
");
printf("Enter your choise.(1-12)
");
printf("1. Binary to Decimal.
");
printf("2. Decimal to Binary.
");
printf("3. Decimal to Octal.
");
printf("4. Decimal to Hexadecimal.
");
printf("5. Octal to Decimal.
");
printf("6. Octal to Binary.
");
printf("7. Binary to Octal.
");
printf("8. Binary to Hexadecimal.
");
printf("9. Hexadecimal to Binary.
");
printf("10. Hexadecimal to Decimal.
");
printf("11. Octal to Hexadecimal.
");
printf("12. Hexadecimal to Octal.
");
scanf("%d",&n);
if(n<1 || n>12)
printf("Invalid Choise
");
if(n==1)
bd();
else if(n==2)
db();
else if(n==3)
doc();
else if(n==4)
{
long a;
clrscr();
printf("Conversion from Decimal to Hexadecimal
");
printf("Enter the decimal number.
");
scanf("%ld",&a);
dh(a);
}
else if(n==5)
od();
else if(n==6)
ob();
else if(n==7)
bo();
else if(n==8)
bh();
else if(n==9)
hb();
else if(n==10)
hd();
else if(n==11)
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("Conversion from Octal to Hexadecimal.
");
printf("Enter a Octal number
");
scanf("%ld",&n);
i=0;
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==0)
{
printf("Hexadecimal eq=");
oh(i);
}
else if(t==1)
printf("Numbert entered is not octal.
");
}
else if(n==12)
ho();
printf("
Do you Wish to continue(Y/N)
");
scanf("%s",&c);
c=toupper(c);
if(c=='Y')
goto begin;
getch();
}

void bd()
{
int n,b=0,a[6],i=0,t=0;
clrscr();
printf("Conversion from Binary to Decimal
");
printf("Enter Binary Number
");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]!=1 && a[i]!=0)
t=1;
i++;
}
a[i]=2;
n=1;
for(i=0;a[i]!=2;i++)
{
b=b+a[i]*n;
n=n*2;
}
if(t==0)
printf("Decimal Equivalent=%d",b);
else if(t==1)
printf("Entered number is not binary.");

}

void db()
{
int dec,bin,n,i=0,a[10];
clrscr();
printf("Conversion from Decimal to Binary
");
printf("Input decimal no.");
scanf("%d",&dec);
do
{
a[i]=dec%2;
dec=dec/2;
i++;
}while(dec!=0);
for(n=i-1;n>=0;n--)
printf("%d",a[n]);
}

void doc()
{
int n,i,a[10];
clrscr();
printf("Conversion from Decimal to Octal
");
printf("Enter a Decimal number
");
scanf("%d",&n);
i=0;
printf("Octal equavalent of %d is ",n);
while(n!=0)
{
a[i]=n%8;
n=n/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}

void dh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
dh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}

void od()
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("Conversion from Octal to Decimal
");
printf("Enter a Octal number
");
scanf("%ld",&n);
i=0;
printf("Decimal equavalent of %ld",n);
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==0)
printf("= %ld",i);
else if(t==1)
printf(" can't be calculated because it is not an Octal Number.
");
}

void ob()
{
int n,a[6],i=0,t=0;
clrscr();
printf("Convertion from Octal to Binary.
");
printf("Enter an Octal Number.
");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]>7)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("000");
break;
case 1:
printf("001");
break;
case 2:
printf("010");
break;
case 3:
printf("011");
break;
case 4:
printf("100");
break;
case 5:
printf("101");
break;
case 6:
printf("110");
break;
case 7:
printf("111");
break;
}
}
if(t==1)
printf("Not a Octal number
");
}

void bo()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Convertion From Binary to Octal
");
printf("Enter a Binary number
");
scanf("%ld",&n);
while(n!=0)
{
a[i]=n%1000;
n=n/1000;
if(a[i]>111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
default:
printf("
Entered number is not binary.
Printed value is not
correct.
");
break;
}
}
if(t==1)
printf("Number is not Binary
");
}

void bh()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Convertion from Binary to Hexadecimal
");
printf("Enter a Binary number
");
scanf("%ld",&n);
while(n!=0)
{
a[i]=n%10000;
n=n/10000;
if(a[i]>1111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
case 1000:
printf("8");
break;
case 1001:
printf("9");
break;
case 1010:
printf("A");
break;
case 1011:
printf("B");
break;
case 1100:
printf("C");
break;
case 1101:
printf("D");
break;
case 1110:
printf("E");
break;
case 1111:
printf("F");
break;
default:
printf("
Entered number is not binary.
Printed value is not
correct.
");
break;
}
}
if(t==1)
printf("Number is not Binary
");
}

void hb()
{
int i;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Binary
");
printf("Enter a Hexadecimal number
");
scanf("%s",s);
//gets(s);
printf("Binary Equivalent=");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'a':
case 'A':
printf("1010");
break;
case 'b':
case 'B':
printf("1011");
break;
case 'c':
case 'C':
printf("1100");
break;
case 'd':
case 'D':
printf("1101");
break;
case 'e':
case 'E':
printf("1110");
break;
case 'f':
case 'F':
printf("1111");
break;
default:
printf("
Entered number is not Hexadecimal.
Printed value is not
correct.
");
break;
}
}
}

void hd()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Decimal
");
printf("Enter a Hexadecimal number
");
scanf("%s",s);
printf("Decimal Equivalent=");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf("
Entered number is not Hexadecimal.
Printed value is not
correct.
");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
printf("%ld ",h);
}

void oh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
oh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}

void ho()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Octal
");
printf("Enter a Hexadecimal number
");
scanf("%s",s);
// Converting hex to dec first
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf("
Entered number is not Hexadecimal.
Printed value is not
correct.
");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
// Now convering from decimal to octal (h)
i=0;
printf("Octal equavalent=");
while(h!=0)
{
a[i]=h%8;
h=h/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}