Numbers & Stuff

numberStuff

This was a program that I came up with just playing around. You would enter a beginning number and an ending number. Then the program would get all of the even & odd numbers from the range given. It would also calculate the total of each number in the range. Again like most of small programs it’s a simple 2 class one.

NumberStuff Class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NumberStuff
{
    public partial class NumberStuff : Form
    {
        private Number _newNumber;

        public NumberStuff()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            _newNumber = new Number();

            int startNumber = 0;
            int endNumber = 0;
            bool isError = false;

            //Getting the values of what the user typed into the starting and ending number field.
            if (!int.TryParse(startingNumberTextBox.Text, out startNumber) || !int.TryParse(endingNumberTextBox.Text, out endNumber))
            {
                isError = true;
                MessageBox.Show("You must enter numbers for Step 1 & Step 2", this.Text, MessageBoxButtons.OK);
            }

            if (startNumber > endNumber)
            {
                isError = true;
                MessageBox.Show("The starting number must be less than the ending number", this.Text, MessageBoxButtons.OK);
            }

            if (!isError)
            {
                //Setting the values of the start and end number in the Number class.
                _newNumber.StartingNumber = startNumber;
                _newNumber.EndingNumber = endNumber;

                //Looping thru the number range.
                for (int i = _newNumber.StartingNumber; i <= _newNumber.EndingNumber; i++)
                {
                    //Getting the values of adding the start and end number.
                    Console.WriteLine(i);    //Test line to see the value of i dring each loop run.
                    _newNumber.Total = i + i;    //Operation that adds the value of i against itself.

                    //Checking to see if the value of i can be divided by 2. If it can even, if not odd.
                    if (i % 2 == 0)
                    {
                        _newNumber.EvenNumbers++;
                    }
                    else
                        _newNumber.OddNumbers++;
                }
                Console.WriteLine("Even Numbers: " + _newNumber.EvenNumbers);    //Test code checking to see how many even numbers.
                Console.WriteLine("Odd Numbers: " + _newNumber.OddNumbers);    //Test code checking to see how many odd numbers.
                Console.WriteLine("Total: " + _newNumber.Total);    //Test code checking the total.

                //Updating the odd number result field.
                oddNumbersTextBox.AppendText(_newNumber.OddNumbers.ToString());

                //Updating the even number result field.
                evenNumbersTextBox.AppendText(_newNumber.EvenNumbers.ToString());

                //Updating the total result field.
                totalTextBox.AppendText(_newNumber.Total.ToString());
            }
        }
    }
}

Number Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumberStuff
{
    public class Number
    {
        private int _startingNumber = 0;
        private int _endingNumber = 0;
        private int _oddNumbers = 0;
        private int _evenNumbers = 0;
        private int _total = 0;

        public int StartingNumber
        {
            get { return _startingNumber; }
            set { _startingNumber = value; }
        }

        public int EndingNumber
        {
            get { return _endingNumber; }
            set { _endingNumber = value; }
        }

        public int OddNumbers
        {
            get { return _oddNumbers; }
            set { _oddNumbers = value; }
        }

        public int EvenNumbers
        {
            get { return _evenNumbers; }
            set { _evenNumbers = value; }
        }

        public int Total
        {
            get { return _total; }
            set { _total = value; }
        }
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s