Print from 1 to 10 without using a loop

This is a discussion on Print from 1 to 10 without using a loop within the C++ Programming forums, part of the General Programming Boards category; Hi, I can't use loops for this. Will I have to declare each number from 1 to 10 if I ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Print from 1 to 10 without using a loop

    Hi,

    I can't use loops for this. Will I have to declare each number from 1 to 10 if I wish to print the square and cube of 1 until 10??

    Thanks
    Code:
    int main(int argc, char* argv[])
    {
       int number;
    
       cout << "Enter a number(s), and you will receive the square and cube of" <<
       " that number: " << endl;
       cin >> number;
    
       cout << "number\t" << "square\t" << "cube" << endl;
    
       cout << number << "\t";
    
       number = number * number;
       cout << number << "\t";
    
       number = number * number * number;
       cout << number;
    
       getch();
       return 0;
    }

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    How about a recursive function?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    I wish I could, but I am only at the beginning of my big book and haven't covered that yet. Only thing Iv'e used it if/else.
    So will I have to declare all ten numbers??

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    I really think that such questions are pointless, loops are there to use them, insead of forcing some technique upon the user, a book should give the user a problem to solve, and not ask for some way to solve it.

    /offtopic

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by glUser3f
    I really think that such questions are pointless, loops are there to use them, insead of forcing some technique upon the user, a book should give the user a problem to solve, and not ask for some way to solve it.

    /offtopic
    they are not useless, because the object of real computer science is not to learn the syntax of a language but to learn how to solve problems. I had to make a nine morris games once without using arrays or globals!

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I don't think exercises like this teach anything. They only serve to confuse the student - just look at how many questions we have from said 'confused students' on this board.

    Give em a real programming assignment. This is like asking them to build a house, but you can't use nails, wood, saws, or any other wood working tool that was designed for building houses.


    Not sure what you mean by square and cube of 1 to 10. Do you mean each number from 1 to 10 squared, or do you mean 1 squared, 2 cubed, 3 ....and so on?

  7. #7
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    In progrqamming logic, problems of this nature are solvable by either recursion or looping sensibly; the only other way would be for you to go with your correctly guessed solution if either of the other two option is not opened to you.
    Agree with axon, problems like this are there for students to grasp conceptual problems and solutions, Man didn't first build a house using bricks and nails, I believe he had to grasp the concept of wwhat a shelter is first before he invented those things.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Well I've seen the results of the 'concepts' they teach firsthand. This is convoluted crap no one will/should ever attempt.

    I've seen some very dumb assignments on here and I'll just add this one to my list.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    The only thing in a loop here is you asking how to do things without loops
    What's next - write an OS without loops?

    I bet your teacher is about 1/2 page in front of you in the book, and has yet to figure out what loops are for themselves.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    The only thing in a loop here is you asking how to do things without loops
    What's next - write an OS without loops?

    I bet your teacher is about 1/2 page in front of you in the book, and has yet to figure out what loops are for themselves.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by axon
    they are not useless, because the object of real computer science is not to learn the syntax of a language but to learn how to solve problems. I had to make a nine morris games once without using arrays or globals!
    Yes, but learning to solve problems without being allowed to use the obviously best option is, well, foolish.

    If they want to challenge students, give them a more complicated real-world problem, without constraints, rather than give a problem artificially made more complicated by the addition of insane rules.

    I have no problem with giving people realistic restrictions (e.g. restrictions on computation time, memory usage, etc.) but removing the obviously optimal solution, then trying to make people solve it, seems a bit pedantic.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    I figure by now you have figured soem kidn of solution, but here an example

    Code:
    #include <iostream>
    
    void NoLoop(int number) {
    if (number >= 0) {
    std::cout<<number<<endl;
    NoLoop(number - 1);
    }
    return;
    }
    
    int main() {
    NoLoop(10);
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add a Loop to print the output?
    By big-tony in forum C Programming
    Replies: 23
    Last Post: 05-22-2009, 10:38 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  4. Bowling again.
    By fatty acid in forum C Programming
    Replies: 4
    Last Post: 11-30-2005, 01:12 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21