How to display a line of numbers equally in java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Im having trouble only displaying the correct numbers, I know how to get the formula is have count-1 have 10/2 = 5 but now I want to count up from the start making it 10 15 20.
//Static void dispalySpace(double start, double end, int count){
// Display numbers between start and end inclusively
//The numbers are spaced equally
//Assume start < end and count is at least 2.
//displaySpace(10,20,3)
//The answer to the question is 10.0 15.0 20.0
while(min<max && count>=2)
for(int i=min; i<=max; i++) {
for(double j = count-1; j>=2; j++) {
System.out.print(j + " ");
}
System.out.print(i + " ");
}
System.out.println();
}
I keep just getting 10-20 displaying on a loop.

You first need to calculate the steep size, ie what do you have to add to min so that you end up with count numbers. For the first output, you won't have to add anything, so it's one less than count , ie
double step = (max-min) / count - 1;
That's what you have to add each step of the loop, starting with min , so
for (double i = 0; i <= max ; i = i + step) {
System.out.print(i) ;
}

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

How do I find the output of the following code fragment in Java? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have the following code fragment, which yields a sum of 16.
int sum = 1;
for (int i = 0; i <= 5; sum = sum + i++)
System.out.println(sum);
From my understanding of sum = sum + i++ and for loops in general, my thought process for the loop would be like this:
0th loop: sum = 1 (do nothing)
1st loop: sum = 1 + 1 (uses sum = 1 from previous iteration )
2nd loop: sum = 2 + 2
3rd loop: sum = 4 + 3
4th loop: sum = 7 + 4
5th loop: sum = 11 + 5
Obviously, there is an error in my logic. Could you please explain why the sum would be 16?
Change System.out.println(sum); to include the loop number as well. Also, don't forget to print the values after your loop.
int sum = 1;
for (int i = 0; i <= 5; sum = sum + i++) {
System.out.printf("loop=%d, sum=%d%n", i, sum);
}
System.out.printf("after loop, sum=%d%n", sum);
Output
loop=0, sum=1
loop=1, sum=1
loop=2, sum=2
loop=3, sum=4
loop=4, sum=7
loop=5, sum=11
after loop, sum=16
Which I trust explains both how and why sum is 16 after the loop.

Why does my arraylist in java become empty by itself [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a simple code. I am dividing a number by its smallest factor till I get 1. But for some reason my arraylist which stores the factors is getting empty.
long total = 0;
long current = 6;
total += current;
while(current > 1){
ArrayList<Long> numbers = new ArrayList<>();
for(long j = 2; j<= Math.ceil(Math.sqrt(current)); j++){
if(current % j == 0){
numbers.add(j);
}
}
System.out.println(numbers);
total += current / numbers.get(0);
current = current / numbers.get(0);
}
My answer should be 6 + 3 because 6/2(smallest factor) + 1 because 3/3(smallest factor).
My error is coming in total += current/numbers.get(0);
When I print the arraylist it shows [2,3] but then becomes empty. This is the compiler.
Since you are instantiating the array list inside the while, it will be always empty if any if or the for condition are not matched
You can instantiate the array list before the while condition like below:
ArrayList<Long> numbers = new ArrayList<>();
while(current > 1)

Phone Call Programming Logic [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Some phone usage rate may be described as follows:
The first minute of a call costs min1 cents.
Each minute from the 2nd up to 10th (inclusive) costs min2_10 cents each minute.
After the 10th minute, the call costs min11 cents for every additional minute.
You have s cents on your account before the call. What is
the duration of the longest call (in minutes rounded down to the
nearest integer) you can have?
Input data:
For min1 = 3, min2_10 = 1, min11 = 2, and s = 20, the output should be phoneCall(min1, min2_10, min11, s) = 14.
Here's why:
The first minute costs 3 cents, which leaves you with 20 - 3 = 17 cents. The total cost of minutes 2 through 10 is 1 * 9 = 9, so you can talk 9 more minutes and still have 17 - 9 = 8 cents. Each next minute costs 2 cents, which means that you can talk 8 / 2 = 4 more minutes.
Thus, the longest call you can make is 1 + 9 + 4 = 14 minutes long.
I'm not sure what's wrong with my code's logic here.
int phoneCall(int min1, int min2_10, int min11, int s) {
int sum = 0;
if (s >= min1) {
sum++;
s = s - min1;
for (int i = 1; i <= 9; i++) {
if (s >= min2_10) {
sum = sum++;
s = s - min2_10;
} else
break;
}
sum = sum + s / min11;
}
return sum;
}
In the if statement inside of your for loop you can do one of two things here to get your return to be 14.
Change the
sum=sum++; to
sum += 1; or remove the
sum= so it its just
sum++;
This should return 14 as the sum.
sum=sum++;
should be replaced with sum++;
Read more about operator precedence operator precedence and return values
Hello #ShubhamSahay You gotta make your code like this:
public static int phoneCall(int min1, int min2_10, int min11, int s) {
int sum=0;
if(s>=min1)
{
sum++;
s=s-min1;
for(int i=1;i<=9;i++)
{
if(s>=min2_10)
{
/*Change 1*/ sum++;
s=s-min2_10;
}
else
break;
}
/*Change 2*/ sum=sum+(s/min11);
}
return sum;
}
So here is why
1st Change: You need to do just sum++
2nd Change: you gotta put those that things in brackets

Java 8 Unreachable Code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am fairly new to Java and I ran into this problem once or twice in one of my books. This is a super simple program and I just don't understand why it's not working. I know when you use return, anything after it in the method is meaningless. So does this mean after you perform an for statement or an if statement that is a return?
I am using Java 8 on Windows 8 in the latest version of Eclipse.
This is my simple program:
// Find the sum of 1 through 50 and the average.
class SumAndAverage
{
public static void main(String args[])
{
int sum = 0;
double average = 0;
for(int i = 1; 1 <= 50; i++)
{
sum += i;
}
// the following code is "unreachable"
average = sum / 100;
System.out.println("The sum is: " + sum);
System.out.println("The average is " + average);
}
}
1 is always less than or equal to 50, isn't it? You probably meant to compare i with 50:
for(int i = 1; i <= 50; i++)
{
sum += i;
}
for(int i = 1; 1 <= 50; i++)
1 is always less than or equal to 50.

Categories