Computer Science
Numbers have different representations depending on the bases on which they are expressed. For example, in base 3, the number 12 is written as 110 (1 x 32 + 1 x 31 + 0 x 30) but base 8 it is written as 14 (1 x 81 + 4 x 80).
Consider for example, the integers 12 and 5. Certainly these are not equal if base 10 is used for each. But suppose, 12 was a base 3 number and 5 was a base 6 number then, 12 base 3 = 1 x 31 + 2 x 30, or 5 base 6 or base 10 (5 in any base is equal to 5 base 10). So, 12 and 5 can be equal if you select the right bases for each of them.
Write a program to input two integers x and y and calculate the smallest base for x and smallest base for y (likely different from x) so that x and y represent the same value. The base associated with x and y will be between 1 and 20 (both inclusive). In representing these numbers, the digits 0 to 9 have their usual decimal interpretations. The upper case letters from A to J represent digits 10 to 19 respectively.
Test your program for the following data and some random data.
Sample Data
Input:
x=12, y=5
Output:
12 (base 3)=5 (base 6)
Input:
x=10, y=A
Output:
10 (base 10)=A (base 11)
Input:
x=12, y=34
Output:
~~12 (base 8) = 34 (base 2)~~
12 (base 17) = 34 (base 5)
[∵ 34 (base 2) is not valid as only 0 & 1 are allowed in base 2]
Input:
x=123, y=456
Output:
123 is not equal to 456 in any base between 2 to 20
Input:
x=42, y=36
Output:
42 (base 7) = 36 (base 8)
Answer
import java.util.Scanner;
public class KboatFindBase
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter x: ");
String x = in.nextLine();
System.out.print("Enter y: ");
String y = in.nextLine();
int xMax = getHighestDigit(x);
int yMax = getHighestDigit(y);
boolean found = false;
int decimalArr[] = new int[20];
for (int i = xMax; i < 20; i++) {
decimalArr[i] = convertToDecimal(x, i+1);
}
for (int i = yMax; i < 20; i++) {
int t = convertToDecimal(y, i+1);
for (int j = xMax; j < 20; j++) {
if (t == decimalArr[j]) {
found = true;
System.out.println(x + " (base " + (j+1) + ") = "
+ y + " (base " + (i+1) + ")");
break;
}
}
if (found) {
break;
}
}
if (!found) {
System.out.println(x + " is not equal to "
+ y + " in any base\nbetween 2 to 20");
}
}
public static int convertToDecimal(String numStr, int base) {
int num = 0;
int len = numStr.length();
for (int i = 0; i < len; i++) {
int mul = (int)Math.pow(base, len - i - 1);
char ch = numStr.charAt(i);
int d = digitValue(ch);
num += d * mul;
}
return num;
}
public static int digitValue(char c) {
int value = 0;
if (Character.isDigit(c)) {
value = c - '0';
}
else if (Character.isLetter(c) && c >= 'A' && c <= 'J') {
value = c - 'A' + 10;
}
return value;
}
public static int getHighestDigit(String numStr) {
int high = 0;
int len = numStr.length();
for (int i = 0; i < len; i++) {
char ch = numStr.charAt(i);
int d = digitValue(ch);
if (d > high) {
high = d;
}
}
return high;
}
}
Output
Related Questions
Write a program to input N and M number of names in two different single dimensional arrays A and B respectively, such that none of them have duplicate names. Merge the arrays A and B into a single array C, such that the resulting array is sorted alphabetically. Display all the three arrays.
Test your program for the following data and some random data:
Sample data:
Input:
Enter the names in array A, N = 2
Enter the names in array B, M = 3
First array: A
Suman
Anil
Second array: B
Usha
Sachin
JohnOutput:
Sorted Merged array: C
Anil
John
Sachin
Suman
Usha
Sorted First array: A
Anil
Suman
Sorted Second array: B
John
Sachin
UshaWrite a program to accept a date in the string format dd/mm/yyyy and accept the name of the day on 1st of January of the corresponding year. Find the day for the given date.
Example:
Input:
Date: 5/7/2001
Day on 1st January : MONDAYOutput:
Day on 5/7/2001 : THURSDAYRun the program on the following inputs:
Input Date Day on 1st January Output day for 04/9/1998 THURSDAY FRIDAY 31/8/1999 FRIDAY TUESDAY 06/12/2000 SATURDAY WEDNESDAY The program should include the part for validating the inputs namely the date and day on 1st January of that year.
- Write a program to declare a square matrix A[ ][ ] of order N (N<20).
- Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix.
- Output the original matrix.
- Find the SADDLE POINT for the matrix. A saddle point is an element of the matrix such that it is the minimum element for the row and the maximum element for the column to which it belongs. Saddle point for a given matrix is always unique. If the matrix has no saddle point, output the message "NO SADDLE POINT".
- If the matrix has a saddle point element then sort the elements of the left diagonal in ascending order using insertion sort technique. All other elements should remain unchanged.
Test your program for the following data and some random data:
Sample data:
Input:
n = 4Matrix A[ ][ ] 2 5 6 9 8 4 12 3 6 7 3 1 12 24 2 11 Output:
Matrix A[ ][ ] 2 5 6 9 8 4 12 3 6 7 3 1 12 24 2 11 No Saddle Point
Matrix after sorting the Principal diagonal:
2 5 6 9 8 3 12 3 6 7 4 1 12 24 2 11 Input:
n = 3Matrix A[ ][ ] 4 6 12 2 8 14 1 3 6 Output:
Matrix A[ ][ ] 4 6 12 2 8 14 1 3 6 Saddle Point = 4
Matrix after sorting the Principal diagonal:
4 6 12 2 6 14 1 3 8 The manager of a company wants to analyze the machine usage from the records to find the utilization of the machine. He wants to know how long each user used the machine. When the user wants to use the machine, he must login to the machine and after finishing the work, he must logoff the machine.
Each log record consists of:
User Identification number
Login time and date
Logout time and dateTime consists of:
Hours
MinutesDate consists of:
Day
MonthYou may assume all logins and logouts are in the same year and there are 100 users at the most. The time format is 24 hours.
Design a program:
(a) To find the duration for which each user logged. Output all records along with the duration in hours (format hours: minutes).
(b) Output the record of the user who logged for the longest duration. You may assume that no user will login for more than 48 hours.
Test your program for the following data and some random data.
Sample Data
Input:
Number of users: 3
User IdentificationLogin Time and Date Logout Time and Date 20:10 20-12 2:50 21-12 12:30 20-12 12:30 21-12 16:20 20-12 16:30 20-12 Output:
User Identification Login Time and Date Logout Time and Date Duration
Hours:Minutes149 20:10 20-12 2:50 21-12 6:40 173 12:30 20-12 12:30 21-12 24:00 142 16:20 20-12 16:30 20-12 00:10 The user who logged in for longest duration:
173 12:30 20-12 12:30 21-12 24:00