Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to solve the following problem:
-find flowchart or algorithm and show it in java languange
"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."
My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?
Since this is a learning assignment, I will give you only hints:
To get the last digit in base N, use x % N; for base ten, that would be x % 10
To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10
Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.
This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:
String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):
prints
4321
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have to check, roomNumber[i], if BOTH numbers (assuming it's two-digit) are not even, and not odd. Only then can I accept the number. Also need to determine if the second digit is not less than double the first. I assume that I need to some how split these into two different ints (a,b), but I'm not sure.
If you assume that you have only two-digit numbers, you can go with it this way:
int[] roomNumbers = new int[] { 12, 34, 56, 67, 78 };
for (int num : roomNumbers) {
int second = num % 10;
int first = (num - second) / 10;
System.out.println("first: " + first + " second: " + second);
}
You take the second number using the modulo (%) operator. To get the first number, you need to subtract the second number and divide it by 10.
Now you can process those numbers further.
Your questions have nothing to do with arrays. Let's reword them:
How do I test if a number is even?
You can google this and easily find the answer. Hint: use the modulus operator (%).
How do I test if the second digit is not less than double the first digit
Break this into smaller pieces: First you need to separate out the first and second digit. Again, you can google this if you don't know. (Hint: it's very similar to the first question. You can use the % operator and division (/).
Then you have to compare these according to the rule. Try to write something on your own and see what you can come up with.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm a beginner at java and I've been trying to make a calculator using Swing. So far everything is good, but I'm having trouble with how the number pad will work. I want the calculator's output to be a float value, but I want it to be so that if you click "1", the output will display "1", not "1.00". How could I go about this?
Also, I cannot think of a way to append a number to another number. For example, if I input 1 then input 2, the output would be 1.02, not 12. How do I get the program to make the output be a whole number when possible?
For the first question: You first want to check that there are no digits after the decimal point when just displaying 1 instead of 1.00. To do so you can check that this actually is the case with this if statement:
if(number % 1.0f == 0)
next, you want to convert this number into a string that just displays the number without the decimal points, to do so you can cast number to an int and then convert it into a string with:
String.valueOf((int) number)
For the second question: To append a new digit to the number I would suggest generating a string with the number the user enters using StringBuilder. In practice it would look something like:
StringBuilder sb = new StringBuilder();
// when user presses "2" button for example:
sb.append("2");
In the end you want to convert this generated String into a float in order to perform calculations on it, which you can do with:
float value = Float.valueOf(sb.toString());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
To help Beta Rabbit crack the lock, write a function answer(n) which returns the smallest positive integer base b, at least 2, in which the integer n is a palindrome. The input n will satisfy "0 <= n <= 1000."
Test cases
Inputs:
(int) n = 0
Output:
(int) 2
Inputs:
(int) n = 42
Output:
(int) 4
it is not the problem I need help with as there are similar ones on this site. It is the actual question. this input n they talk about, what the hell does it stand for? It can't be the base as b represents the base. It is not the positive integer they want for output because it is independent of that number and in the test cases it doesn't seem to have any correlation with it. Does it just want me to find the smallest palindrome of any base that's less than 1000 in decimal? Thanks to anyone that takes the time to help me figure this out , it's part of Google's foobar questions. I don't mind doing the work to solve the flecking thing as long as I can understand what the thing is asking.
What is Google Foobar?
I may not give you a full answer, but think about it this way. The number n, convert it to the output base... see if it's a palindrome
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.
For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.
I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.
The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0 means the number is even.
For more information please check Wikipedia.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm rather sure I haven't found an answer to this because I'm not sure of the correct term for what I'm trying to do, so apologies in advance if it's very straight forward/well documented.
I have a set of numbers which I need to output a number in a specific position (hundreds), IE:
For 1302, I need to output 3
For 1802, I need to output 8
etc
How can I accomplish this with Java?
I should note that this is easy with 100's ( / 100), however I can't seem to figure out how to do this when the number is > 100.
a easy way is use % (modulo) and then /
like :
(input % 1000) / 100
Edit It works if you use only int number.
If you want to keep the number,
(num / 100) % 10
should generally work.
If you want to output the second number of your number you could convert it to a String and then use the substring method (doc) to get the character you want. (of course it only works if the number you want is always located at the same position).
An example of one solution, to get you on your way :)
int i = 1302;
String hundreds = Integer.toString(i).substring(1, 2);
System.out.println(hundreds);