Java conversion table [closed] - java

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 days ago.
This post was edited and submitted for review 8 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm writing a program to print out a unit conversion table for lengths.
But I'm getting stuck on formatting the answers in a table array like this:
feet miles kilometers | kilometers miles feet
5280 1 1.609 | 1 0.622 3281.541
10560 2 2.218 | 2 1.243 6563.083
21120 4 6.436 | 4 2.486 12126.165
[![expected result][1]][1]
Here is the code I've written so far:
class Main {
public static void main(String[] args) {
// values below:
//int m;
int mil;
//double fe;
double ki;
double mfe = 5280;
double kilo = 1.609;
double kf = 0.000189394;
System.out.println("feet: miles: kilometers: | kilometers: miles: feet:");
for (int m = 1, fe = 1; m <= 15 && fe <= 15; m++){
System.out.println(m);
System.out.printf("%.3f", (m * mfe));
System.out.printf("%.3f", (m * kilo));
System.out.println(" | ");
}
for (int k = 1, m = 1; k <= 15 && m <= 15; k++){
System.out.printf("%-11d%-10.3f", k, (k / kilo));
System.out.printf("%.3f", (k * kf));
}
}
}
When I run this code, it produces:
feet: miles: kilometers: | kilometers: miles: feet:
1
5280.0001.609 |
2
10560.0003.218 |
3
15840.0004.827 |
4
...
73920.00022.526 |
15
79200.00024.135 |
1 0.622 0.0002 1.243 0.0003 1.865 0.0014
I'm trying to get the code to print to 3 decimal places in a table array like stated above.
Towards the two goals I mentioned above, I'm having the following difficulties:
How can I get the two lists to print side by side?
How can I get the right amount of whitespace between the numbers?

Related

java program to loop and output 2,5,7,10,12 [closed]

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 4 months ago.
Improve this question
Loop
how to loop this without using array? I'm clearly confuse in this in java module I don't know the solution ;-; I'm new to java pls helppp mee
Try this.
for (int i = 5; i <= 25; i += 5)
System.out.println(i / 2);
Or
for (int i = 1; i <= 5; i++)
System.out.println(i * 5 / 2);
output:
2
5
7
10
12
Think for yourself why this produces the correct result.
public class Main {
public static void main(String[] args) {
int outputNum = 0;
for(int i = 0; i < 5; i++){
if (i % 2 == 0){
outputNum += 2;
}
else {
outputNum += 3;
}
System.out.print(outputNum + (i == 4 ? "" : " "));
}
}
}
This does exactly what you need.
Explanation:
In the loop if the i variable is even we add 2 to the output number (since you need to add 2 then 3 then 2...) and if i is uneven then we add three. At the end of the for loop we print the number without the newline using System.out.print and we separate them by a space if we aren't printing the last number using a ternary operator (i == 4 ? "" : " ") which returns a space if i is not equal to 4 and an empty string if it is, we do this to avoid having a space on the end of our output, so we get this 2 5 7 10 12 instead of this 2 5 7 10 12

Read n, Find the value of M where M = 2 * 4 * 6 * … * ≤ n [closed]

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 3 years ago.
Improve this question
n is any integer value, given by the user. M is Multiply of all even numbers in 1..n:
M = 2 * 4 * 6 * … * ≤ n
Example :
int n = 9
output
int output = 2*4*6*8; // 384
My code:
Scanner in = new Scanner(System.in)
int n=inut.nextInt();
for(....)
In short you need to multiply all even numbers between 1 and n.
For this you can use a for-loop and if-statement. for-loop will give you all numbers between 1..n, and if-statement will reject odd numbers, leaving only even.
Last path would be to multiply all values.
int n = 9;// input;
int result = 1; // because you are multiplying, initial result must be 1
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) { // ignore all odd numbers
result *= i; // multiply result with next even value
}
}
System.out.println(result); // print the result: 384
You can look at this like an assembly line. At the start someone is generating numbers from 1 to n. Then someone called 'the filter' rejects (push to trash) all odd numbers, at the end of the line someone called 'the aggregator' multiplies all values into an result.
With Java 8 and streams this can be represented by:
int result = IntStream.range(1,n)// generate numbers from 1 to n
.filter(value->value%2==0) // reject all odd numbers
.reduce(1, (a,b)-> a*b); // multiple all values, with 1 as initial result
System.out.println(result);

Construct Array A of 1 and -1 of size N,such that sum of all A[i]*A[j] is Minimum and Positive. 1 <= i < j <= N [closed]

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 3 years ago.
Improve this question
I came across this question in a competition. We have given a number N and we need to construct an array of size N which consists of 1 and -1 only, such that the value of sum of the product of each pair is minimum and Positive.
i.e. If the array is A then
Sum of ( A[i] * A[j] ) over all 1 <= i < j <= N is Minimum and Positive.
Example:
Input => 3
Output => [1,1,1]
Explanation - All possible cases are:
[1,1,1] = 3
[1,1,-1] = -1
[1,-1,-1] = - 1
[-1,-1,-1] = 3
And so all combination and the minimum possible positive case is 3.
How can we find such an array?
I tried to find a pattern but that did not worked.
Analitically it's very simple, no need to write a program for it.
Let's notice, that:
(a1 + a2 + ... + an)^2 = (a1^2 + a2^2 + ... + an^2) + 2 * (a1a2 + a1a3 + ... + ana(n-1))
Or in other words (cannot format it here nicely):
(sum_{i}(ai))^2 = sum_{i}(ai^2) + 2 * sum_{1 <= i < j <= N}(ai * aj)
Here we're looking for sum_{1 <= i < j <= N}(ai * aj).
After some simple additions we obtain:
sum_{1 <= i < j <= N}(ai * aj) = 1 / 2 * ((sum_{i}(ai))^2 - sum_{i}(ai^2))
Also notice, that sum_{i}(ai^2) is constant, since it's equal to N (only -1 or 1), thus the solution is when (sum_{i}(ai))^2 is the smallest, so equal 0, when N even and 1 when odd.
Solution:
For N even - any permutation of N / 2 times 1 and N / 2 times -1.
For N odd - any permutation of (N - 1) / 2 times 1 and (N + 1) / 2 times -1 or (N - 1) / 2 times -1 and (N + 1) / 2 times 1.
Edit - for the minimum positive sum:
Having a base of:
sum_{1 <= i < j <= N}(ai * aj) = 1 / 2 * ((sum_{i}(ai))^2 - sum_{i}(ai^2)) = 1 / 2 * ((sum_{i}(ai))^2 - N)
we need to find ai, so that (sum_{i}(ai))^2 > N => sum_{i}(ai) > sqrt(N).
If we have ceil(sqrt(N)) times 1, we have to distribute N - ceil(sqrt(N)) = A between 1 and -1 to keep their sum smallest. Solution is obvoius:
For A = 2 * B => B times 1 and -1.
For A = 2 * B + 1 => B + 1 times 1 and B times -1.

Integer to char - cast and print: [closed]

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 6 years ago.
Improve this question
I want to know the logic behind this programm and (char) cast.
How does it work and how it is printing all the letters, symbols and numbers
package ascii1
public class Ascii1 {
public static void main(String[] args) {
int i=1;
while(i<122)
{
System.out.println((char)i+"\t");
if (i%10==0)
System.out.println("");
i++;
}
}
}
Its output is:
//Blanks in the beginning...
! "
$ % & ' (
)
*
+ ,
- . / 0 1 2
3 4 5 6 7 8 9 : ; <
=
? # A B C D E F
G H I J K L M N O P
Q R S T U V W X Y Z
[ \ ] ^
_ ` a b c d
e f g h i j k l m n
o p q r s t u v w x
y BUILD SUCCESSFUL (total time: 0 seconds)
Using ASCII representation, every char has a numerical value.
When you iterate, adding +1 to the i variable, you get to numbers on the ASCII table representing some characters.
Finally, the (char) cast returns the above ASCII character.

How to print the even numbers out of any random number in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am looking for how to do the following.
I am looking to have a method accept a parameter of any number and have the method return the even numbers that were in the number that was the parameter. So if my number was "1234", the method would return the value 24, by removing the odd numbers (1 and 3), or pulling out the even numbers (2 and 4).
Here is what I want my main method to look like:
System.out.println("testing removeOdds: 123456 = " + removeOdds(123456)); // Returns 246
System.out.println("testing removeOdds: 24680 = " + removeOdds(24680)); // Returns 24680
System.out.println("testing removeOdds: 13579 = " + removeOdds(13579)); // Returns 0
I don't want to use a while loop or a string to do this. Thanks.
You will need
a loop (preferably a while loop)
the division operator (/)
the modulo operator (%)
a Java compiler
EDIT:
Assume your number is "n" = 1234
now n % 10 gives you 4.
n / 10 = 123.
Now if you do n % 10, you get 3.
So, put everything in a while loop with condition n > 0 do n % 10 then do n % 2. Take the value of n % 2 out and print it.
Code:
public static void main(String[] args) {
int n = 123435644;
while (n > 0) {
int val = n % 10;
if (val % 2 == 0) {
System.out.println(val);
}
n = n / 10;
}
}
O/P:
4
4
6
4
2

Categories