usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

How to Generate Odd Numbers in Java for Kids



What are Odd Numbers? | Maths Explanation for Java Kids

Odd numbers are numbers that are not divisible by 2.

They include:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

In this beginner-friendly Maths Java tutorial for kids, we'll explore how to list odd numbers using Java loops and conditions - perfect for primary school students learning to code.
To generate odd numbers in Java, we use a 'while' loop combined with a conditional statement. This simple Java exercise will help you understand number patterns and logic.

Create a new Java class file; File, New File.
Call it OddNumbers.
Type out the adjoining Java code for listing odd numbers.



Note: You can comment out the evenNumbers Java object code in the main class from the previous lesson or simply continue from where it stopped.



Code for Odd Number List with User Input in Java

For a little more flexibility, let's add an input form to our Java code for odd numbers.

All we need is a way to ask the user for input.
For this purpose, we'll import and use the System.in and Scanner Java library classes.



So! Java Fun Practice Exercise - List Odd Numbers

As a fun practice exercise, feel free to try out your own boundary values, and see how the Java code lists the odd numbers between those boundary values.







Java Code for Odd Numbers class .

package arithmetic;

public class OddNumbers {

    private int start// Our starting point
    private final int stop// where we will stop
    private String result// Store result here.

    OddNumbers(int first, int last) {
        start = first;
        stop = last;
        result = "Odd numbers between " + first + " and " + last + " are: \n";
    }

    String prepResult() {
        /*
        * Loop from start to stop and rip out odd numbers;
         */

        while (start <= stop) {
            if (start % 2 != 0) {
                result += start + "; "// Mind the '+' in front of the '='
            }
            start++; // increase start by 1(same as start = start + 1
        }
        return result;
    }
    
}

Java Code for Odd Numbers - Main class.

package arithmetic;

public class Arithmetic {

    public static void main(String[] args) {
        System.out.println("Welcome to our demonstration sequels");
        System.out.println("Hope you enjoy (and follow) the lessons.");
        System.out.println("");
        
        /* Use the Odd Number class. */
        OddNumbers odd_data = new OddNumbers(1, 100); // odd numbers between 1 and 100
        System.out.println(odd_data.prepResult());

        System.out.println();
    }

}

Java Code for Odd Numbers - Main class for Collecting Input.

package arithmetic;

import java.util.Scanner;

public class Arithmetic {

    public static void main(String[] args) {
        System.out.println("Welcome to our demonstration sequels");
        System.out.println("Hope you enjoy (and follow) the lessons.");
        System.out.println("");
        
        /*
        * Collect input.
         */

        int first;
        int last;
        String collect_input; // For collecting user input

        /* Scanner takes care of collecting input using the System.in object. */
        Scanner user_choice = new Scanner(System.in);

        System.out.print("Enter your start number:   ");
        collect_input = user_choice.next(); // user input is a String object
        first = Integer.parseInt(collect_input); // Convert it to integer

        System.out.print("Enter your stop number:   ");
        collect_input = user_choice.next();
        last = Integer.parseInt(collect_input);

        // Use the Odd Number class.
        OddNumbers odd_input = new OddNumbers(first, last);
        System.out.println(odd_input.prepResult());

        System.out.println();
    }

}



<< PreviousNext >>