The program should find the integer and the remainder using only additions and subtractions. When a residue appears, the program gives the following, although it works fine without remainder (for example, 15 by 3 divides normally) enter image description here

Here is the code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Training { class cmon { public int num; public int k, count; public void enter() { Console.Write("Введите делимое: "); num = Convert.ToInt32(Console.ReadLine()); Console.Write("Введите делитель: "); k = Convert.ToInt32(Console.ReadLine()); } public void del() { do { num -= k; count++; } while (num != 0); } public void print() { Console.WriteLine("Целое: " + count); Console.WriteLine("Остаток: " + num); } } class Program { static void Main(string[] args) { cmon h = new cmon(); h.enter(); h.del(); h.print(); Console.ReadKey(); } } } 
  • There are generally a lot of mistakes. - DanielOlivo
  • @ DanielOlivo - no, there are few errors: while(num > 0) and after the cycle, if the remainder is less than zero, you need to add back the divider. - Igor

1 answer 1

 public void del() { for (count = 0; num >= k; ++count, num -= k) {} } 

num and k better do uint

  • something screwed up with the last line. count = 0; num >= k; - Igor
  • @Igor yes you are right, it's what I screwed up, I'll fix it now - Mirdin