AllTheTimeWorld.com

csharp arithmeticFinding Dozens

Finding Dozens

A program to select quantity on scroll bar and display dozens: quantity%12

Finding Dozens

A program to select quantity on scroll bar and display dozens: quantity%12

We have some quantity of eggs to sell. We want to package it by the dozen. We want find how many dozen we can sell and how many we will have left over.

Build the form as shown below:

Horizonal Scroll Bar

  Name:  hsbEggs
  LargeChange = 1

Labels

  Name:  lblInstructions
  Text = "Select the number of eggs:"

  Name:  lblEggs
  Text:  "0"

  Name:  lblDozen
  Text: "Dozen=0"

  Name:  lblRemainder
  Text: "Remainder=0"

Write the code as shown below:

// Programmer: Janet Joy
// Finding dozen and remainder for a value on scrollbar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace eggs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void hsbEggs_Scroll(object sender, ScrollEventArgs e)
        {
            // Find the number of dozen and remainder for value in scroll bar.
            int eggs = hsbEggs.Value;
            lblEggs.Text = "" + eggs;
            int dozen = eggs / 12;
            lblDozen.Text = "Dozen = " + dozen;
            int remain = eggs % 12;
            lblRemainder.Text= "Remainder =" + remain;
        }
    }
}

Remember, you should try doing each of these programs on your own. Get it to work, break it, modify it, and then try to write it on your own!