在没有 Math 类库的情况下用 C# 进行除法

发布于 2024-12-04 15:51:21 字数 634 浏览 3 评论 0原文

我编写了一个不带任何算术字符进行除法的程序,代码如下:

        int num1;
        int num2;

        Console.WriteLine("Enter first number");
        int.TryParse(Console.ReadLine(), out num1);

        Console.WriteLine("Enter Second Number");
        int.TryParse(Console.ReadLine(), out num2);
        int sum = 0;

        sum = num1;
        int counter = 0;

        while (sum > 0)
        {
            sum -= num2;
            counter += 1;
        }
        Console.WriteLine("The division of the two numbers is " + counter);

我现在想让它显示除法的余数,例如 10 / 3 将显示 1,因为总和还剩下 1。

我该怎么做呢?我应该使用什么样的循环以及它与我的除法循环有多相似?

I have made a program that divides without any arithmetic characters, here is the code:

        int num1;
        int num2;

        Console.WriteLine("Enter first number");
        int.TryParse(Console.ReadLine(), out num1);

        Console.WriteLine("Enter Second Number");
        int.TryParse(Console.ReadLine(), out num2);
        int sum = 0;

        sum = num1;
        int counter = 0;

        while (sum > 0)
        {
            sum -= num2;
            counter += 1;
        }
        Console.WriteLine("The division of the two numbers is " + counter);

I would now like to make it show the remainder of a division e.g. 10 / 3 will show 1 as there is 1 left from the sum.

How would I go about doing it? What kind of loop should I use and how similar is it to my division loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

毁梦 2024-12-11 15:51:21
var rem = num1;
while (rem >= num2)
{
  rem -= num2;
}

Console.WriteLine("The remainder is " + rem);

或者如果你想让它变得非常简单,那么在循环之后:

var rem = sum + num2;

由于你的程序有缺陷,你应该将循环更改为:

while (sum >= num2)
{
  sum -= num2;
  counter += 1;
}

然后你在 counter 中得到除法结果,在 sum< 中得到余数/代码>。

var rem = num1;
while (rem >= num2)
{
  rem -= num2;
}

Console.WriteLine("The remainder is " + rem);

or if you want to make it really easy then after your loop:

var rem = sum + num2;

Since your program is flawed you should change the loop to:

while (sum >= num2)
{
  sum -= num2;
  counter += 1;
}

Then you have the division result in counter and the remainder in sum.

七色彩虹 2024-12-11 15:51:21

然后将此行更改

while (sum > 0)

while (sum >= num2 )

sum将保留其余部分,Counter实际上将给出正确的结果。

GJ

Change this line

while (sum > 0)

to this

while (sum >= num2 )

then sum will hold the remainder, and counter will actually give the correct result.

GJ

白日梦 2024-12-11 15:51:21

余数将为sum + num2

The remainder will be sum + num2.

游魂 2024-12-11 15:51:21

更改 while

while (sum >= num2)

并在末尾添加

Console.WriteLine("The remainder of the division of the two numbers is " + sum);

Change the while in

while (sum >= num2)

and add at the end

Console.WriteLine("The remainder of the division of the two numbers is " + sum);
寒江雪… 2024-12-11 15:51:21

我确实希望这只是一个练习,看看你是否可以在不实际除法的情况下进行除法,而不是真正尝试制定有效的除法算法!

我不想看到计算1,000,000,000 / 1需要多长时间

无论如何,如果你必须这样做,那么余数将是sum加上的最终值num2

I do hope this is just an exercise in seeing if you can divide without actually dividing and not a genuine attempt to make an efficient dividing algorithm!

I'd hate to see how long it takes to calculate 1,000,000,000 / 1

Anyway, if you must do it this way, then the remainder will be the final value of sum plus num2.

誰ツ都不明白 2024-12-11 15:51:21
while (sum >= num2 ) // <- changed
{
    sum -= num2;
    counter += 1;
}
Console.WriteLine("The division of the two numbers is " + counter);
Console.WriteLine("The remainder of the two numbers is " + sum);
while (sum >= num2 ) // <- changed
{
    sum -= num2;
    counter += 1;
}
Console.WriteLine("The division of the two numbers is " + counter);
Console.WriteLine("The remainder of the two numbers is " + sum);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文