Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Question help >

  1. Jul 31, 2006 #1
    hey people,

    Im a beginner when it comes to programming and i have just stated C++..Im finding it a bit hard and boring but its only the beginning, so i think that it would get better as i move on....Anyway i need some help with these questions...

    BTW for your info: Im using a book called "A gentle introduction to C++" by Dan Hawhorn...
    So here are my questions:

    Part A
    1)Enter a number from the keyboard
    2)Test the number to make sure it is a positive integer
    3)If not, loop back and re enter a new number
    4)Calculate the display the sum of all integers from 1....
    5)e.g. If N = 8, then Sum = 1+2+3+4+5+6+7+8 = 36

    Part B

    1) Enter a number from the keyboard
    2) Test the number to make sure it is a positive integer, less than 18
    3) (otherwise the result will be too large)
    4) If not, loop back to re enter a new number
    5) Calculate and display the product of all integers from 1.....N
    6) e.g. If N = 5, then product = 1*2*3*4*5 = 120 = 5! = 5 factorial.

    I would be most happy if anyone could help me with writing these programs......

    The program i am using is Dev-C++, oh and i am progressing through those tutorials(stickies) on the top...

    Thanks a lot
  2. jcsd
  3. Jul 31, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Can you show us your thoughts on how these programs might be written? We can help you, if you can show us where you're stuck.

    - Warren
  4. Jul 31, 2006 #3


    User Avatar

    Staff: Mentor

    Here's a general tip: Don't try to write the entire program at once! Do part of it, and test it, then move on to another part and test it, etc. For example, your Part A divides neatly into two sections that you can solve separately:

    At this point, you should display the number that you finally got, with a 'cout', so you can verify that it's OK. When you get this working, you can move on to the rest of it:

  5. Jul 31, 2006 #4
    Ok here we go:

    For part A the 2nd question:

    Test the number to make sure it is a positive integer..

    So far i have done this....

    #include <iostream>

    using namespace std;

    int main()
    int number;

    cout<<"Please input a number from the keyboard: ";
    cin>> number;

    It works right upto here...

    Now i want to know how to assign to the program the point to make sure that it is a positive integer...SO what statement do i give for this???

    Following the 3rd which says: If not, loop back to re enter a new number...im thinking for this that i have to use an IF statement but not too sure about it...

    Hope that does it...
    Last edited: Jul 31, 2006
  6. Jul 31, 2006 #5

    Thanks for that, but i already knew that rule...it is at this point where i think i need some help.....i dont know what to enter to test to make sure whether the number is a positive integer or not following the looping part....
  7. Jul 31, 2006 #6
    Last edited: Aug 1, 2006
  8. Aug 1, 2006 #7
    That loop will not terminate as required. (Hint: in C, any non-zero value is true for Boolean purposes).

  9. Aug 3, 2006 #8
    Ok so your program pulls in the number. So in order to loop back you'll have to actually use a loop which would include "for" "while" and "do while"

    Using what you have there you could just put a while loop after it

    while (test){
    cout<<"Please input a number from the keyboard: ";
    cin>> number;

    ^ you can just put the same code you used to prep the loop

    From the sounds of it where i put (test) is where you're having the trouble?

    As far as that goes, every even number divided by 2 is an even number. So you could do something like

    while (num > 18 || (num % 2) > 0)

    That way the loop will only go into effect if a number is greater than 18 or if the number has a remainder (which if it were even it wouldn't).
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook