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







<< PreviousNext >>

How to Generate Odd Numbers in C# for Kids




What are Odd Numbers? | Maths Explanation for C# 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 C# tutorial for kids, we'll explore how to list odd numbers using C# loops and conditions - perfect for primary school students learning to code.
To generate odd numbers in C#, we use a 'while' loop combined with a conditional statement. This simple C# exercise will help you understand number patterns and logic.

Create a new C# class file; Project, Add Class.
Call it OddNumbers.
Type out the adjoining C# code for listing odd numbers.



Note: You can comment out the EvenNumbers C# 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 C#

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

All we need is a way to ask the user for input.
For this purpose, we'll use the Console.ReadLine() C# library function.



So! C# Fun Practice Exercise - List Odd Numbers

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








C# Code for Odd Numbers class.

using System.Collections.Generic;

namespace Arithmetic
{
    class OddNumbers
    {
        private int start; // Our starting point
        private int stop; // where we will stop
        private List<int> list_of_primes; // We will house our gathered prime numbers here.

        // Our constructor
        public OddNumbers(int first, int last)
        {
            start = first;
            stop = last;
            list_of_primes = new List<int>();
        }

        public List<int> prepResult()
        {
            /*
            * Loop from start to stop and rip out even numbers;
             */

            while (start <= stop)
            {
                if (start % 2 != 0)
                {
                    list_of_primes.Add(start);
                }
                start++; // increase 'start' by 1
            }
            return list_of_primes;
        }
    }
}

C# Code for Odd Numbers - Main class.

using System;
using System.Collections.Generic;

namespace Arithmetic
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to our demonstration sequels");
            Console.WriteLine("Hope you enjoy (and follow) the lessons.");
            Console.WriteLine("");

            /* Use the Even Number class. */
            int first = 1;
            int last = 100;
            List<int> answer;

            OddNumbers odd_data = new OddNumbers(first, last); // odd numbers between 1 and 100
            answer = odd_data.prepResult();

            Console.WriteLine("Odd numbers between " + first + " and " + last + " are:");
            Console.WriteLine(String.Join(", ", answer));

        }
    }
}

C# Code for Odd Numbers - Main class for Collecting Input.

using System;
using System.Collections.Generic;

namespace Arithmetic
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to our demonstration sequels");
            Console.WriteLine("Hope you enjoy (and follow) the lessons.");
            Console.WriteLine("\r\n");

            /*
            * Collect input.
             */

            int first;
            int last;
            List<int> answer;
            string collect_input; // For collecting user input

            Console.Write("Enter your start number:   ");
            collect_input = Console.ReadLine();
            first = int.Parse(collect_input); // Convert it to integer

            Console.Write("Enter your stop number:   ");
            collect_input = Console.ReadLine();
            last = int.Parse(collect_input);

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

            Console.WriteLine("Odd numbers between " + first + " and " + last + " are:");
            Console.WriteLine(String.Join(", ", answer));

        }
    }
}



<< PreviousNext >>