- #1

- 45

- 0

I'm trying to implement *, /, %, powers, and square root.

I've been experimenting with the for loop but can't seem to make it work. Any tips on how to do these?

Thanks :D

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter ermines
- Start date

- #1

- 45

- 0

I'm trying to implement *, /, %, powers, and square root.

I've been experimenting with the for loop but can't seem to make it work. Any tips on how to do these?

Thanks :D

- #2

- 45

- 0

right now, i'm having a hard time implementing the cube, raised to any exponent and the square root.

- #3

HallsofIvy

Science Advisor

Homework Helper

- 41,833

- 963

- #4

lurflurf

Homework Helper

- 2,440

- 138

Just regular adds and subtracts, or bit level also? Like break numbers up into individual bits.ermines said:

right now, i'm having a hard time implementing the cube, raised to any exponent and the square root.

- #5

HallsofIvy

Science Advisor

Homework Helper

- 41,833

- 963

For powers such as Cube, here's what I would do

int Multiply(int x,int y)

{//returns x times y

int z= 0;

for (int i=0;i<x;i++)

z= z+ y;

return z;

}

int Square(int x)

{//returns x squared

return Multiply(x,x);

}

int Cube(int x)

{ //returns x cubed

int z= Square(x);

return Multiply(x,z);

}

int Power(int x, int n)

{ //returns x to the n power

int z= 1;

for (int i=0;i< n;i++)

z= Multiply(x,z);

return z;

}

- #6

- 70

- 0

ermines said:I'm trying to implement *, /, %, powers, and square root.

For what type of numbers? int,float,double,...

Do you also have to handle sign,overflow,divide by zero,...

I had this for a homework assignment once as well. It's all the special cases that make it hard.

- #7

- 45

- 0

My only problem now is the square root function of type int, not float. So how do i do it?

- #8

- 45

- 0

int sqRoot (int input)

{

int b=0;

while ((b*b)<=input)

{

b+=1;

}

b-=1;

return b;

}

now, if only i can find a way to implement some codes to float. any ideas, guys?

- #9

siddharth

Homework Helper

Gold Member

- 1,127

- 0

double sqRoot(double input)

{

double b=0;

while((b*b)<=input)

{

b=b+.0001;

}

return b;

}

Because you are looking at an approximate value, it won't make a big difference if you subtract by .0001 after the loop

- #10

Hurkyl

Staff Emeritus

Science Advisor

Gold Member

- 14,916

- 19

tsk, tsk, you used multiplication. What about calling your multiplication routine instead?

- #11

- 45

- 0

int sqRoot (int input)

{

int b=0;

while (square (b)<=input)

{

b+=1;

}

b-=1;

return b;

}

umm now guys, i'm faced with a new problem. how do i implement the inverse factorial program and also a program that produces all non-prime numbers from 1-100?. of course, * and / are already allowed. i'm still thinking of it and have a rough idea of how to do it like probrably the same with the division code...Still, i need some tips and help on how to carry this out. :D

thanks again. :D

- #12

- 45

- 0

int factorial (int x)

{

int base=1;

for (int i=1; i<=x; i++)

{

base*=i;

}

return base;

}

- #13

HallsofIvy

Science Advisor

Homework Helper

- 41,833

- 963

- #14

- 45

- 0

here's my NON-WORKING code. how do i fix it. im a beginner, remember. Basically i wan the input to be continuously divided until it reaches 1 and the i count the iterations to give me the base of the factorial.

int inv (int input)

{

int a=0;

while (input>=1)

{

a++;

input=input/(a+1);

}

a--;

return a;

}

- #15

Math Is Hard

Staff Emeritus

Science Advisor

Gold Member

- 4,527

- 28

my code is pretty clunky, but here's something I was playing around with:

int main()

{

int number = 24; //the input

int div = 1;

int count = 0;

while(number > 1)

{

number = number/div;

div ++;

count ++;

}

cout << count;

return 0;

}

int main()

{

int number = 24; //the input

int div = 1;

int count = 0;

while(number > 1)

{

number = number/div;

div ++;

count ++;

}

cout << count;

return 0;

}

Last edited:

- #16

- 45

- 0

int inv (int n)

{

int a=1;

if (n>=0)

{

while (n>=1)

{

a++;

n=n/a;

}

a--;

}

return a;

}

now all i need is the code for listing all non-prime numbers from 1-100 including 100. any tips?

- #17

- 35

- 0

ermines said:

int inv (int n)

{

int a=1;

if (n>=0)

{

while (n>=1)

{

a++;

n=n/a;

}

a--;

}

return a;

}

now all i need is the code for listing all non-prime numbers from 1-100 including 100. any tips?

This may not be the most efficient method, but try this:

Code:

```
for (int i = 1; i <= 100; i++) {
for (int j = 2; j < i; j++) {
if (Modulus(i, j) == 0) {
cout << i << endl;
break;
}
}
}
```

- #18

- 45

- 0

umm what is the modulus for? isn't it undeclared?

- #19

- 35

- 0

Code:

```
for (int i = 1; i <= 100; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
cout << i << endl;
break;
}
}
}
```

Basically once it finds

- #20

- 35

- 0

- #21

- 45

- 0

- #22

- 45

- 0

Share:

- Replies
- 1

- Views
- 5K

- Replies
- 1

- Views
- 2K