Java decimal to binary without api's and strings - java

I must write a program in java (homework) that gives the input (x), the input in binary, tells if the input is a palindrome and tells if the binary from the input is a palindrome. I may not use api's other than System.out.print and I may not use strings.
So far so good: I've written the program and it works till x = 1023 (because of the int). Which piece of code must I edit, so the input can be any positive number?
class Palindromes {
public static int DtoBinary(int x) {
int y = x;
int w = 1;
int v = 0;
int z = 1;
int u = 0;
while (z < y) {
z = z * 2;
u++;
}
z = z / 2;
for (int t=1; t<u; t++) {
w = 10 * w;
}
v = v + w;
y = y - z;
while (y > 0) {
z = z / 2;
if (z <= y) {
w = w / 10;
v = v + w;
y = y - z;
} else if (y == 1) {
v = v + 1;
y = 0;
} else {
w = w / 10;
v = v + 0;
}
}
return v;
}
public static boolean Palindrome(int x) {
int s = x;
int r = 0;
while (s > 0) {
int q = s % 10;
r = r * 10 + q;
s = s / 10;
}
if (x == r) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
int x = 1023;
System.out.print(x + " " + DtoBinary(x));
if (Palindrome(x)) {
System.out.print(" yes");
} else {
System.out.print(" no");
}
if (Palindrome(DtoBinary(x))) {
System.out.print(" yes");
} else {
System.out.print(" no");
}
}
}

You can use a char array instead of an int to store large binary numbers for your palindrome class.
public static char[] DtoBinary(int x) {
<insert code to convert x to an array of zeroes and ones>
}
You will need to write a method that checks if a char array is a palindrome.

I can offer short solution for homework.
public static boolean isPalindrome(int x) {
String inputted = "" + x;
String reverse = new StringBuffer(inputted).reverse().toString();
return inputted.equals(reverse);
}
or
public static boolean isPalindrome(int x) {
String inputted = "" + x;
int length = inputted.length;
for(int i = 0; i< inputted/2; i++){
if(inputted.charAt(i)!= inputted.charAt(inputted - 1 - i)){
return false;
}
}
return true;
}

Related

Why does my loop increment 1 over the value needed?

This is my first CS class ever, and I'm trying to learn everything the best that can. In this loop I am trying to find valid mastercard numbers using Luhn's formula. The program works, but it's been bugging me that my final for loop outputs 1 number over the correct value, unless I subtract 1 from z after the loop finishes iterating. For example: if the sum = 56 , then z would = 5 , when the correct answer would be 4. How can I fix this going forward?
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class MCGenerator {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
Random rand = new Random();
System.out.print("How many Mastercard numbers would you like to generate? ");
int quantity = scnr.nextInt();
int x;
int use;
int range;
int appendI;
Long appendL;
String firstDigits;
String append;
String preliminary;
int y;
int c;
int sum;
int z;
int findDigit;
String lastNum;
String cardNumber;
System.out.println("\nHere you go, have fun: ");
for(x = 0; x < quantity; x++ ) {
use = rand.nextInt(2 - 1 + 1) +1;
if(use == 1) {
range = rand.nextInt(55 - 51 + 1) + 51;
}
else {
range = rand.nextInt(272099 - 222100 +1) + 222100;
}
if(range < 56) {
appendL = ThreadLocalRandom.current().nextLong(1000000000000L, 10000000000000L);
append = String.valueOf(appendL);
}
else {
appendI = rand.nextInt(999999999 - 100000000 + 1) + 100000000;
append = String.valueOf(appendI);
}
firstDigits = String.valueOf(range);
preliminary = firstDigits + append;
for(y = 0, sum = 0; y < 15; y++ ) {
c = preliminary.charAt(y) - '0';
if(y % 2 == 0){
c *= 2;
}
if(c > 9) {
c -= 9;
}
sum += c;
}
for(z = 0, findDigit = sum; findDigit > 0; z++){
findDigit = sum + z;
findDigit %= 10;
}
z -= 1;
lastNum = String.valueOf(z);
cardNumber = preliminary + lastNum;
System.out.println(cardNumber);
}
}
}

How do I carry over an integer from within a if/else if code into an integer from outside that code? (java)

I'm trying to write a code that can solve exponential questions, and can solve exponential questions from integer inputs and word inputs (i.e one, two... etc). I've pretty much figured out the integer parts, however i'm stuck on the "Word input" part. Mainly, the final part in my code for the word part (int base = y, exponent = z), in which i can calculate the exponent doesn't work because y and z cannot be pulled from the if/else if statements
How do i make my code so that int base = y, exponent = z can pull from their respective if/else if codes?
Thanks a bunch!
public static void main(String[] args) {
Scanner choice = new Scanner(System.in);
System.out.println("Enter 1 for Alphabetical Input, 2 for numerical input:");
int solution = choice.nextInt();
if(solution == 1) {
for(int i=1; i<=100;i++) {
Scanner words = new Scanner(System.in);
System.out.print("Enter your base");
String x = words.toString();
String x1 = "zero";
String x2 = "one";
String x3 = "two";
String x4 = "three";
String x5 = "four";
String x6 = "five";
String x7 = "six";
String x8 = "seven";
String x9 = "eight";
String x10 = "nine";
String x11 = "ten";
if(x == x1) {
int y = 0;
}
else if(x == x2) {
int y = 1;
}
else if(x == x3) {
int y = 2;
}
else if(x == x4) {
int y = 3;
}
else if (x == x5) {
int y = 4;
}
else if (x == x6) {
int y = 5;
}
else if (x == x7) {
int y = 6;
}
else if (x == x8) {
int y = 7;
}
else if (x == x9) {
int y = 8;
}
else if (x == x10) {
int y = 9;
}
else if (x == x11) {
int y = 10;
}
else {
System.out.println("I can't read that!");
}
System.out.println("Enter your exponent");
Scanner exponent = new Scanner(System.in);
String a = exponent.toString();
if(a == x1) {
int z = 0;
}
else if(a == x2) {
int z = 1;
}
else if(a == x3) {
int z = 2;
}
else if(a == x4) {
int z = 3;
}
else if (a == x5) {
int z = 4;
}
else if (a == x6) {
int z = 5;
}
else if (a == x7) {
int z = 6;
}
else if (a == x8) {
int z = 7;
}
else if (a == x9) {
int z = 8;
}
else if (a == x10) {
int z = 9;
}
else if (a == x11) {
int z = 10;
}
else {
System.out.println("I can't read that!");
}
int base = y, exponents = z; //this does not work, y and z aren't pulled
double result = Math.pow(base, exponents);
}
}
else if(solution ==2 ) {
for(int i=1;i<=100;i++){
Scanner number = new Scanner(System.in);
System.out.print("Enter your base:");
int x = number.nextInt();
Scanner up = new Scanner(System.in);
System.out.print("Enter your exponent:");
int y = up.nextInt();
int base = x, exponent = y;
double result = Math.pow(base, exponent);
System.out.println("Answer = " + result);
}
}
else {
System.out.println("Could not read input");
}
}
}
Declare them as #Shuang Li said in front of the for-loop as otherwise y and z are only visible within the regarding if.
Apart from that you should not use if (x == x1) as this checks if your input is equal to the memory address of String x1 and thus will print out "I can't read that!". For checking if the strings are equal you want to use if (x.equals(x1)).
Further reading in the user's input using String x = words.toString(); is not doing what you would want it to do as it instantly skips to the if-part passing null to String x. Instead you should use
String x = words.next(); or String x = words.nextLine();
as unlike to .toString(); the methods .next(); and .nextLine(); are methods of Scanner and so they will wait for any input and only after getting an input the code will be further executed.
So this would be the could you would aim for:
public static void main(String[] args) {
Scanner choice = new Scanner(System.in);
System.out.println("Enter 1 for Alphabetical Input, 2 for numerical input:");
int solution = choice.nextInt();
int y = 0;
int z = 0;
if (solution == 1) {
for (int i = 1; i <= 100; i++) {
Scanner words = new Scanner(System.in);
System.out.print("Enter your base");
String x = words.next();
String x1 = "zero";
String x2 = "one";
String x3 = "two";
String x4 = "three";
String x5 = "four";
String x6 = "five";
String x7 = "six";
String x8 = "seven";
String x9 = "eight";
String x10 = "nine";
String x11 = "ten";
if (x.equals(x1)) {
y = 0;
} else if (x.equals(x2)) {
y = 1;
} else if (x.equals(x3)) {
y = 2;
} else if (x.equals(x4)) {
y = 3;
} else if (x.equals(x5)) {
y = 4;
} else if (x.equals(x6)) {
y = 5;
} else if (x.equals(x7)) {
y = 6;
} else if (x.equals(x8)) {
y = 7;
} else if (x.equals(x9)) {
y = 8;
} else if (x.equals(x10)) {
y = 9;
} else if (x.equals(x11)) {
y = 10;
} else {
System.out.println("I can't read that!");
}
System.out.println("Enter your exponent");
Scanner exponent = new Scanner(System.in);
String a = exponent.next();
if (a.equals(x1)) {
z = 0;
} else if (a.equals(x2)) {
z = 1;
} else if (a.equals(x3)) {
z = 2;
} else if (a.equals(x4)) {
z = 3;
} else if (a.equals(x5)) {
z = 4;
} else if (a.equals(x6)) {
z = 5;
} else if (a.equals(x7)) {
z = 6;
} else if (a.equals(x8)) {
z = 7;
} else if (a.equals(x9)) {
z = 8;
} else if (a.equals(x10)) {
z = 9;
} else if (a.equals(x11)) {
z = 10;
} else {
System.out.println("I can't read that!");
}
int base = y, exponents = z; // this does not work, y and z aren't pulled
double result = Math.pow(base, exponents);
System.out.println("Answer = " + result);
}
} else if (solution == 2) {
for (int i = 1; i <= 100; i++) {
Scanner number = new Scanner(System.in);
System.out.print("Enter your base:");
int x = number.nextInt();
Scanner up = new Scanner(System.in);
System.out.print("Enter your exponent:");
y = up.nextInt();
int base = x, exponent = y;
double result = Math.pow(base, exponent);
System.out.println("Answer = " + result);
}
} else {
System.out.println("Could not read input");
}
}
Declare y and z in front of the entire for loop.
What u did before in those if is that you declare a variable, you assigned a value to that variable and you throw it away immediately. This is no python. IDE should give you a warning for that.

What is wrong with my java code for Project Euler's program 4? (finding the largest palindrome of 2 3 digit numbers)

This is my code and the answer always seems to 100001 (its not even
performing the loop).
I know there are much easier ways to solve this problem but what exactly is wrong with this particular code? and how do I fix it?
public class LargestPalindromes
{
public static void main(String[] args)
{
int largest = 100001;
for(int i = 100; i < 1000; i++)
{
for(int j = 100; j < 1000; j++)
{
int mult = i * j;
if(largest < mult && isPalindrome(mult))
largest = mult;
}
}
System.out.printf("\n\nThe largest palindrome is: %d\n\n", largest);
}
public static boolean isPalindrome(int mult)
{
int n1=0, n2=0, n3=0, n4=0, n5=0, n6=0;
int largest = 0, count = 0, p =100000;
int x = mult;
while(count < 6)
{
if(count == 1)
n1 = x / p;
else if(count == 2)
n2 = x / p;
else if(count == 3)
n3 = x / p;
else if(count == 4)
n4 = x / p;
else if(count == 5)
n5 = x / p;
else if(count == 6)
n6 = x / p;
x %= p;
p /= 10;
count++;
}
int reverse = Integer.valueOf(String.valueOf(n1) + String.valueOf(n2) + String.valueOf(n3) + String.valueOf(n4) + String.valueOf(n5) + String.valueOf(n6));
return reverse == mult;
}
}
There were too many errors in your original public static boolean isPalindrome(int mult) method. So I replaced it with the standard version:
public static boolean isPalindrome(int mult)
{
int temp=mult;
int r,sum=0;
while(mult>0){
r=mult%10; //getting remainder
sum=(sum*10)+r;
mult=mult/10;
}
if(temp==sum)
return true;
else{
return false;
}
}

exponents without math.pow using for loop (in java)

i need to take 2 inputted numbers and calculate variable 1 to the power of variable 2 is without using math.pow and using a for loop. This is what i have now
Scanner in = new Scanner(System.in);
System.out.print("Please enter your base: ");
int base = in.nextInt();
System.out.print("Please enter your exponent: ");
int power = in.nextInt();
int result = mathPower(base, power);
System.out.println(base + " to the power of " + power + " is " + result + ".");
}
public static int mathPower(int a, int b)
{
int result = a;
if (b == 0) {
result = 1;
}
if (b < 0) {
a = (1 / a);
b = -b;
}
for (a = 1; a < b; a++) {
result = result * a;
return result;
}
return result;
}
}
It only seems to work if the exponent is 0, otherwise it just displays the a value. I need both positive and negative exponents. Thanks in advance!
The case with b<0 only makes sense with floating point numbers, so I changed the type of a and the return value to double.
public static double mathPower(double a, int b)
{
double result = 1;
if (b < 0) {
a = 1.0 / a;
b = -b;
}
for (int i = 0; i < b; i++) {
result = result * a;
}
return result;
}
You have three main problems:
The return statement inside the loop is breaking it in the first repetition.
You're using your a variable as the loop variable.
If you allow negative exponents then the return value should be a double.
public static double mathPower(double a, int b)
{
double result = 1.0;
if (b == 0)
{
result = 1.0;
}
if (b < 0)
{
a = (1.0 / a);
b = -b;
}
for (int i = 0; i < b; i++)
{
result = result * a;
}
return result;
}
suppose you have numb1=2 and numb2=6.
then
temp=1;
if (numb2 < 0) {
numb1 = 1 / numb1;
numb2 = -numb2;
}
for(int n = 1; n<=numb2; n++){
temp=temp*numb1;
}
public double power(double base,int pow)
{
double result = 1;
if(pow==0)
return 1;
if(base == 0)
return 0;
if(pow>0)
{
for(int i = 0;i<pow;i++)
{
result *= base;
}
}
else
{
for(int i = pow;i<0;i++)
{
result *= base;
}
result = 1/result;
}
return result;
}

Missing return statement (Java): Calculating first 100 pentagonal number)

I am trying to find the first 100 pentagonal numbers using methods. Below is my code, however, I keep getting missing return statement. I sure that I am not placing return statement at the appropriately, besides I wouldn't know if the process is right. I would therefore appreciate guidance. Thank you.
PS: yet to learn arrays and this is no homework
public class PentagonNumber {
public static void main(String[] args) {
int numberperline = 10;
int n = 1;
int count = 0;
int pent = getpentagonnumber(n);
count++;
if (count % numberperline == 0)
System.out.println();
else System.out.print(pent + "\t");
}
public static int getpentagonnumber(int x) {
for (int count = 0; count < 100; count++) {
for (x = 1; x <= 100; x++) {
int result;
result = x * (3 * x - 1) / 2;
return result;
}
}
}
}
Your code should be changed like this:
public class PentagonNumber {
public static void main(String[] args) {
int numberperline = 10;
//int n = 1; // you do not need N
//int count = 0; // you do not this either
for (x = 0; x < 100; x++) { // moved
int pent = getpentagonnumber(x+1); // +1 so it goes 1::100
//count++;
if (x % numberperline == 0)
System.out.println();
//else // you were skipping every tenth result.
System.out.print(pent + "\t");
}// close for
}
public static int getpentagonnumber(int x) {
//for (int count = 0; count < 100; count++) { // moved
//for (x = 1; x <= 100; x++) { // removed
int result; // no need to declare and then calculate
result = x * (3 * x - 1) / 2; // but not wrong either.
return result;
//}
//}
}
}
You only have a return value inside a for loop. You probably meant for it to be:
public static int getpentagonnumber(int x ){
int result = 0;
for (int count=0; count<100; count++){
for (x=1;x<=100;x++){
result = x*(3*x-1)/2;
}
} //this was missing
return result;
}

Categories

Resources