Jump to content
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int nos;
            int student;
            int result;
            int result2;

            nos = 100;
            student = 19;
            result = nos / student;
            result2 = 100 - (result * 19);

            Console.WriteLine(result2);

            Console.ReadKey();
            Console.WriteLine("Any key to end.");

        }
    }
}

A teacher has bought a packet of 100 sweets that she is going to share out equally between her 19 students. Because a single sweet cannot be shared, the teacher will keep what is not given to the children. The teacher will keep the minimum number of sweets possible.

Write a program to determine and output the number of sweets each child will receive. As a single sweet cannot be shared between pupils, your program must calculate the number of sweets per child and the number that the teacher keeps for herself.

 

Console outputs 5

A8-7600 {} Gigabyte FM2+ Board {} CX430 {} Corsair Vengeance LP 8gb {} MSI GTX 760 2GB {}

 

Console.WriteLine("C# is aids");

 

Link to comment
https://linustechtips.com/topic/748241-is-this-correct/
Share on other sites

Link to post
Share on other sites

Let's assume there are 20 kids in the class, each child would get 5 lollis (5*20=100, or alternatively 100/20=5)

 

Now if there is 19 students in the class, each student would still only get 5 lollies (19*5=95), since 5 (100-95=5) is not enough to give each student an extra lolli, each student will still get 5 lollies.

 

I don't like what you have name the lolli varible, lolli or sweet would be much clearer, I'd also recomend you make a comment giving a brief discription of what everything does, you are likely going to be marked on that too, not just the formular.

Link to comment
https://linustechtips.com/topic/748241-is-this-correct/#findComment-9474544
Share on other sites

Link to post
Share on other sites

Just now, Comic_Sans_MS said:

I don't like what you have name the lolli varible, lolli or sweet would be much clearer, I'd also recomend you make a comment giving a brief discription of what everything does, you are likely going to be marked on that too, not just the formular.

Yeah, i did that for the section in which the user imputs the number of students and number of sweets;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int sweets;
            int student;
            int result;
            int result2;

            //User imputs number of sweets
            Console.WriteLine("Enter number of sweets.");
            sweets = Convert.ToInt32(Console.ReadLine());

            //User imputs the amount of students in the class
            Console.WriteLine("Enter number of students");
            student = Convert.ToInt32(Console.ReadLine());

            //The results
            result = sweets / student;
            result2 = 100 - (result * 19);

            //Amount of sweets the teacher will recieve
            Console.WriteLine("The teacher will get {0} sweets.", result2);

            //Ends string
            Console.WriteLine("Any key to end.");
            Console.ReadKey();

        }
    }
}

 

A8-7600 {} Gigabyte FM2+ Board {} CX430 {} Corsair Vengeance LP 8gb {} MSI GTX 760 2GB {}

 

Console.WriteLine("C# is aids");

 

Link to comment
https://linustechtips.com/topic/748241-is-this-correct/#findComment-9474549
Share on other sites

Link to post
Share on other sites

4 hours ago, nobiggieBIG said:

A teacher has bought a packet of 100 sweets that she is going to share out equally between her 19 students. Because a single sweet cannot be shared, the teacher will keep what is not given to the children. The teacher will keep the minimum number of sweets possible.

Write a program to determine and output the number of sweets each child will receive. As a single sweet cannot be shared between pupils, your program must calculate the number of sweets per child and the number that the teacher keeps for herself.

What you're really looking for, to solve the general case, is Modulo. Modulo is division that returns the remainder of a division.

So the algorithm for calculating how much sweets each one gets then becomes:

// Setting up variables
int sweet = get_num_sweets();
int student = get_num_students();
int teacher_sweets;
int student_sweets;

// Calculating results
teacher_sweets = sweet % student;
student_sweets = sweet - teacher_sweets;
student_sweets /= student;


The algorithm that you have assumes that each student gets 19 candies and that there are less than 100 candies in total. Which brings me to the lesson of this thread: Never, ever, use "magic numbers" in your programs.

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/748241-is-this-correct/#findComment-9475276
Share on other sites

Link to post
Share on other sites

4 hours ago, straight_stewie said:

What you're really looking for, to solve the general case, is Modulo. Modulo is division that returns the remainder of a division.

So the algorithm for calculating how much sweets each one gets then becomes:


// Setting up variables
int sweet = get_num_sweets();
int student = get_num_students();
int teacher_sweets;
int student_sweets;

// Calculating results
teacher_sweets = sweet % student;
student_sweets = sweet - teacher_sweets;
student_sweets /= student;


The algorithm that you have assumes that each student gets 19 candies and that there are less than 100 candies in total. Which brings me to the lesson of this thread: Never, ever, use "magic numbers" in your programs.

Yeah, my lecturer knows c# but i dont think well enough, im beginner so its all a massive head ***

A8-7600 {} Gigabyte FM2+ Board {} CX430 {} Corsair Vengeance LP 8gb {} MSI GTX 760 2GB {}

 

Console.WriteLine("C# is aids");

 

Link to comment
https://linustechtips.com/topic/748241-is-this-correct/#findComment-9476633
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×