I need to make it for every integer

Nov 3, 2019 at 6:33am
Hello this is only for 13/2, I want to make it for every integer and also convert the while loop into a for one, kindly help me out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  // CPP program to divide a number by other 
// without using / operator 
#include <bits/stdc++.h> 
using namespace std; 

// Function to find division without using 
// '/' operator 
int division(int num1, int num2) 
{ 
if (num1 == 0) 
	return 0; 
if (num2 == 0) 
	return INT_MAX; 

bool negResult = false; 

// Handling negative numbers 
if (num1 < 0) 
{ 
	num1 = -num1 ; 
	if (num2 < 0) 
		num2 = - num2 ; 
	else
		negResult = true; 
} 
else if (num2 < 0) 
{ 
	num2 = - num2 ; 
	negResult = true; 
} 

// if num1 is greater than equal to num2 
// subtract num2 from num1 and increase 
// quotient by one. 
int quotient = 0; 
while (num1 >= num2) 
{ 
	num1 = num1 - num2 ; 
	quotient++ ; 
} 

// checking if neg equals to 1 then making 
// quotient negative 
if (negResult) 
		quotient = - quotient ; 
return quotient ; 
} 

// Driver program 
int main() 
{ 
	int num1 = 13, num2 = 2 ; 
	cout << division(num1, num2); ; 
	return 0; 
} 
Nov 3, 2019 at 6:50am
Well the answer would be easy, if you'd written the code yourself.

But you didn't.
https://www.geeksforgeeks.org/division-without-using-operator/

Look, either start doing your own work or get out of the kitchen.

> http://www.cplusplus.com/forum/beginner/264756/2/
Your last reply sums up what is entirely wrong with your approach.

Regardless of whatever grade you've currently accumulated through your various nagging efforts, it isn't worth a damn bean, because you actually don't know anything.

Sooner or later, someone will ask you to write some code in a closed environment, and you'll be screwed.

Last edited on Nov 3, 2019 at 3:15pm
Nov 3, 2019 at 7:10am
None of your business, I uploaded the code on that website, can you do anything?

I just asked for help regardless of from where I got that code
Nov 3, 2019 at 9:10am
See this:
int num1 = 13, num2 = 2 ;

Replace it with code that asks the user for two numbers.
Nov 3, 2019 at 11:21am
None of your business

salem c is doing their best to help you. It’s a pity you can’t realize it.
And being rude won’t be of much help in your life.

Nov 3, 2019 at 11:59am
None of your business

Are you sure?

Lets say you get "full marks". Then you seek a job. So does someone else. A company is sloppy and picks you due to the "full marks", when the others have non-full, but honest marks. It is clearly the business of those other applicants to be without job, salary, food due to you.

You are in the job and get to write an application. There are unexpected problems and thus additional costs. The company suffers due to incompetence.

The application gets to market. It relates to health-care. Salem is hospitalized and suddenly his life depends on the code that you wrote.

It is very much in the best interest (i.e. business) of everyone that all, who get grade, have really learned the material.
Nov 3, 2019 at 2:25pm
[My apologies to the regulars, who've heard me say this about 7 billion times]

Euno, coding is like football. The only way to learn it is to do it yourself. You'd never think that you could watch football videos and then join a team. No number of videos will help you actually play the game.

The fact that you couldn't figure out how to solve this on your own indicates that you know very little about programming. It appears that you've been getting along by finding code online. The point that Salem C and Keskiverto are making is that you'll hit a wall where this no longer works and almost certainly fail your course at that time. You need to start writing code yourself.

As a start, try this. The way your posted code handles negative numbers could be improved a lot. Can you do this on your own?

In pseudo-code, it should do this:
1
2
3
4
5
6
7
if numerator is negative then
    change the sign of numerator and denominator
// Now the numerator is guaranteed to be positive.
if (denominator is negative) then
    set the negResult flag
    change the sign of the denoninator
}


The rest of the code from line 32 is the same.

If you've learned about recursion then it's even easier:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if numerator is negative then
    change the sign of numerator and denominator
// Now the numerator is guaranteed to be positive.
if (denominator is negative) then
    return -(division(num1, -num2));  // 
}
// If you get here then numerator and denominator are positive.
int quotient = 0; 
while (num1 >= num2) 
{ 
	num1 = num1 - num2 ; 
	quotient++ ; 
}
return quotient;
Topic archived. No new replies allowed.