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
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 last month.
Improve this question
we have an array of 3 digit numbers sort only the middle digit of the arrayof numbers
I/p=540,984,902
O/p=500,944,982 and the middle number is change by using one array
I tried only the swap options .i am excepting by using one array
Something like this ?
int[] numbers = new int[]{540, 984, 902};// or however many 3-digit numbers
int[] mids = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
mids[i] = (numbers[i] / 10) % 10;
}
Arrays.sort(mids);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = (numbers[i] / 100) * 100 + mids[i] * 10 + numbers[i] % 10;
System.out.println("a[" + i + "] " + numbers[i]);
}
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) ;
}
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
This is my code but it gives me this error
class MultiplyDivide {
public static void main (String args[]) {
int i = 5;
int j = 10;
System.out.println("5 is " + i);
System.out.println("10 is " + j);
int k = i/j;
System.out.println("5/10 is " + k);
k = i * j;
System.out.println("5 * 10 is " + k);
}
}
Hi,
Actually the code that you posted should not give you an error.
It will return an output like:
5 is 5
10 is 10
5/10 is 0
5 * 10 is 50
One point that can be an error from your point of view is the 5/10 equals zero. But it is a correct java behavior because you are dividing integer by integer and assign a result to an integer.
If you want to get a double-type result you need to do something like:
double k = i * 1.0 / j;
Hope this resolves your issue.
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
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
Write a program that returns number of decimal digits in the given input string.
ex :
Input: 2345abc423
output: 7
This is my code:
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int len = s.length();
int numbers = 0 ;
for(int i=0; i < len; i++){
char c = s.charAt(i);
int num = (int) c;
while(num <= 57 && num >= 48){
System.out.println(numbers);
numbers++;
}
}
System.out.print(numbers);
/////
sorry because my question wasn't obvious
my program always return 0 , not the number of decimal digits
thanks #Keppil i tried if and it worked
thanks
Your program will never exit the while loop since the condition never changes. You should just use an if instead:
if (num <= 57 && num >= 48) {
System.out.println(numbers);
numbers++;
}
Solution that abuses regex:
System.out.println(input.replaceAll("\\D", "").length());