Java code for generating odd numbers:
import javax.swing.JOptionPane;
public class odd{
public static void main(String[] args){
int x = 0;
do{
String inputNum = JOptionPane.showInputDialog(null, "Enter any positive number: ", "Input", JOptionPane.INFORMATION_MESSAGE);
x = Integer.parseInt(inputNum);
if(x<1){
JOptionPane.showMessageDialog(null, "ERROR. It must be a positive integer.", "Error", JOptionPane.ERROR_MESSAGE);
}
}while(x<1);
System.out.println("\nYou entered " + x + ".");
System.out.println("\n\nOdd numbers found: ");
System.out.println("------------------");
for(int j = 1; j<=x;){
System.out.print(j + ", ");
j = j + 2;
if(j>x)
break;
}
System.out.println("\n\n");
}
}
Sample Output:
Your thoughts about this post? Leave a commment now.