# program problem

• (2 Pages)
• 1
• 2

## 16 Replies - 2254 Views - Last Post: 05 February 2002 - 04:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=2411&amp;s=2761945a7c4662f35022093aa193090f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 robarb

• D.I.C Lover

Reputation: 2
• Posts: 1,966
• Joined: 31-January 02

# program problem

Posted 04 February 2002 - 05:21 PM

I'm having a problem with my program for Mr. Hanosh.  It's for program 2, formula game.  I can't figure out how to do the square root or the prime/composite part.  I have no idea how to do the square root.  The only way I could think to do the prime/composite part was a big if/then statement... but that would be called "Junk Code".  Is there a better way?
Is This A Good Question/Topic? 0

## Replies To: program problem

### #2 Quik

• D.I.C Lover

Reputation: 2
• Posts: 3,750
• Joined: 06-March 01

## Re: program problem

Posted 04 February 2002 - 05:42 PM

Didnt you need help w/ program 1 also? *nods..
hehhe, I wish i could help, but I know 0 bout C++

### #3 supersloth

• serial frotteur - RUDEST MEMBER ON D.I.C.

Reputation: 4562
• Posts: 28,447
• Joined: 21-March 01

## Re: program problem

Posted 04 February 2002 - 05:51 PM

ok, heres some hints:

in your book, look up "sqrt" its a function, really easy to use.

for prime composite, this is where your algebra skills come in. there are 4 numbers, which you can use in conjunction with the % operator to figure out if its prime or composite, think a while on that one.

### #4 robarb

• D.I.C Lover

Reputation: 2
• Posts: 1,966
• Joined: 31-January 02

## Re: program problem

Posted 04 February 2002 - 06:47 PM

ok, i'll look up sqrt... but would this work?  For even/odd i tried an if/else statement with if as (num/2 >= 1).  I thought it'd work, but i didn't account for decimals.  How can i account for decimals?  By saying (num/2 >= 1) and adding something saying just as long as it's not a floating number?

### #5 supersloth

• serial frotteur - RUDEST MEMBER ON D.I.C.

Reputation: 4562
• Posts: 28,447
• Joined: 21-March 01

## Re: program problem

Posted 04 February 2002 - 07:03 PM

ok, dont use division for this part. mudolos. %  << use that symbol. its the key.

Reputation: 1
• Posts: 120
• Joined: --

## Re: program problem

Posted 04 February 2002 - 08:19 PM

#include<math.h>
#include<math.h>
#include<math.h>
#include<math.h>
#include<math.h>
#include<math.h>
#include<math.h>

Thats all i have to say

### #7 robarb

• D.I.C Lover

Reputation: 2
• Posts: 1,966
• Joined: 31-January 02

## Re: program problem

Posted 04 February 2002 - 08:26 PM

thanks dragon, i have <math.h>.  supersloth, i'll try looking up the %.  a question, though.  I found sqrt in chapter 9, and read about it, but how do i apply it?  do i put in the #include sqrt, or do i apply it as an actual function like "void main" only "void sqrt"?  I tried putting it in {cout << "the square root to" << num << "is" << double sqrt(double num)'
';}, and that didn't work.  the double was highlighted in blue.  basically, how do those functions work?

Reputation: 1
• Posts: 120
• Joined: --

## Re: program problem

Posted 04 February 2002 - 08:33 PM

i don't have the book any mo'

but its like
double sqrt(double num)

but not exactly

### #9 robarb

• D.I.C Lover

Reputation: 2
• Posts: 1,966
• Joined: 31-January 02

## Re: program problem

Posted 04 February 2002 - 08:37 PM

when did you have mr hanosh?  do you put it in the cout or something?

Reputation: 1
• Posts: 120
• Joined: --

## Re: program problem

Posted 04 February 2002 - 08:40 PM

Second Semester last year, and first semester this year,

and you can, or you can stick it into a variable

### #11 robarb

• D.I.C Lover

Reputation: 2
• Posts: 1,966
• Joined: 31-January 02

## Re: program problem

Posted 04 February 2002 - 08:44 PM

how would i put it into a variable?  would i say int sqrt, or int double sqrt(double num), or what?

### #12 supersloth

• serial frotteur - RUDEST MEMBER ON D.I.C.

Reputation: 4562
• Posts: 28,447
• Joined: 21-March 01

## Re: program problem

Posted 04 February 2002 - 09:16 PM

cout << "the square root is " << sqrt(num);

### #13 robarb

• D.I.C Lover

Reputation: 2
• Posts: 1,966
• Joined: 31-January 02

## Re: program problem

Posted 05 February 2002 - 03:23 PM

thanks, but i don't need that anymore... i figured it out already in class.  but how can i do the prime/composite.  I tried using an if/else statement, but that didn't work.  Someone said i should use a for loop.  which would be best and should i inculde the % modulus?

### #14 supersloth

• serial frotteur - RUDEST MEMBER ON D.I.C.

Reputation: 4562
• Posts: 28,447
• Joined: 21-March 01

## Re: program problem

Posted 05 February 2002 - 04:03 PM

num % whatever

its in your book, and your notes. and you dont need a for loop. your a whole program only needs one loop, and thats to ask if they are playing again.

### #15 robarb

• D.I.C Lover

Reputation: 2
• Posts: 1,966
• Joined: 31-January 02

## Re: program problem

Posted 05 February 2002 - 04:07 PM

so what does that mean?  use an if/then statement w/ modulous and forget the for loop?