Java: How to Count odd Integers?
Go to solution
Solved by Cela1,
32 minutes ago, Tocsin_786 said:Yea its a conditional statement, kinda like an If statement but condensed. So in this case the n input is either being subtracted or added by either 1 or 2. How would I change it so it does it in intervals automatically? like what if I want to count every second odd number or something like that?
I'm not *quite* sure what you mean, but if you change the -1 / -2 to a +3 / +5, then you should get the second odd number. If you actually want to count, then use a FOR loop like;
import java.util.Scanner;
public class OddPredecessor {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
n = (n-1 % 2==1) ? n -1 : n -2
for (int i = n; i < y; i+=2) {
System.out.println(n);
}
}
}
Where 'y' is where you want to stop (can be written as n+a, where a is the amount over the selected digit that you want to show).
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now