a c++ program printing squares and cube but using if statement only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nkhosinathie
    New Member
    • May 2007
    • 91

    a c++ program printing squares and cube but using if statement only

    hello everyone

    may someone please help me with this c++ program.

    i'm developing a program that will print numbers from 0 to 10 which must appear like a table but i;m only allowed to use if statement only and that program must take any number apart from 0-10.
    the program should look like this.

    integer square cube
    0 0 0
    1 1 1
    2 4 8

    i will be so much pleased if someone out there may help me.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    So 'for', 'while' and 'do' loops are not allowed. Recursion mayhap or a filthy 'goto'
    statement by any chance? Is that allowed by your assignment text?

    kind regards,

    Jos

    Comment

    • Nkhosinathie
      New Member
      • May 2007
      • 91

      #3
      Originally posted by JosAH
      So 'for', 'while' and 'do' loops are not allowed. Recursion mayhap or a filthy 'goto'
      statement by any chance? Is that allowed by your assignment text?

      kind regards,

      Jos
      no sir what is allowed is only if statement only. and at the same same time it has to appear in a table form.and it is an assignment

      Comment

      • Nkhosinathie
        New Member
        • May 2007
        • 91

        #4
        Originally posted by Nkhosinathie
        no sir what is allowed is only if statement only. and at the same same time it has to appear in a table form.and it is an assignment
        what i did is i used this information to do it.but the problem is it doesn;t appear like it should be.i did like this.i used 11 letters because i'm printing 0-10

        #include<iostre am>

        using namespace std;
        using std::endl;
        using std::cin;
        using::std cout;

        int main( )
        {
        int a;int b;int c;int d;int e;int f;int g;int h;int i;int j;int k;
        int square;
        int cube;

        cout<<"integer any integer"<<endl;
        cout<<"integer\ t<<square\t<<cu be<<endl;
        cin>>a;
        square=a*a;
        cube=a*a*a;

        cout<<a<<"\t"<< square<<"\t"<<c ube<<endl;

        so i repeated this statement 11 times and it appeared like this.

        integer square cube
        0 0 0
        1
        1 1 1
        2
        2 2 2

        so i want to know how to remove this between numbers,

        Comment

        • Nkhosinathie
          New Member
          • May 2007
          • 91

          #5
          using an if statement only

          hello u all.may someone help me with a c++ program that is printing integers from 0-10 with their squares and cubes in a table form but using if only statement.

          what i did is this:and my problem is i want to remove the numbers in between

          #include<iostre am>

          using namespace std:
          using std::cout;cin;e ndl;

          int main( )
          { int a;b;c;d;e;f;g;h ;i;j;k
          int square;
          int cube;

          cout<<"enteger any integer"<<endl;
          cin<<a;
          square=a*a;
          cube=a*a*a;

          cout<<a<<"\t"<< square<<"\t"<<c ube<<endl;

          so i repeated the statement 11 times. my problem is it appears like this

          integer square cube
          0 0 0
          1
          1 1 1
          2
          2 2 2

          so how can i remove the numbers in between?
          please help

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Nkhosinathie
            hello u all.may someone help me with a c++ program that is printing integers from 0-10 with their squares and cubes in a table form but using if only statement.

            what i did is this:and my problem is i want to remove the numbers in between

            #include<iostre am>

            using namespace std:
            using std::cout;cin;e ndl;

            int main( )
            { int a;b;c;d;e;f;g;h ;i;j;k
            int square;
            int cube;

            cout<<"enteger any integer"<<endl;
            cin<<a;
            square=a*a;
            cube=a*a*a;

            cout<<a<<"\t"<< square<<"\t"<<c ube<<endl;

            so i repeated the statement 11 times. my problem is it appears like this

            integer square cube
            0 0 0
            1
            1 1 1
            2
            2 2 2

            so how can i remove the numbers in between?
            please help
            Do not double post. Keep one problem in one thread. It's easier for everyone that way.

            Threads merged.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Your code sample does not compile due to syntax errors. How can you get output from code that won't compile?

              Comment

              • Nkhosinathie
                New Member
                • May 2007
                • 91

                #8
                Originally posted by weaknessforcats
                Your code sample does not compile due to syntax errors. How can you get output from code that won't compile?
                i'm giving u again sir;it is compiling sir

                #include<iostre am>

                using namespace std:
                using std::cout;
                using std::cin;
                using std::endl;

                int main()
                {
                int a;int b;int c;int d;int e;int f;int g;int h;int i;int j;int k;
                int square;
                int cube;

                cout<<"enteger any number"<<endl;
                cin>>a;
                square=a*a;
                cube=a*a*a;

                cout<<a<<"\t"<< square<<"\t"<<c ube<<endl;

                cin>>b;
                square=b*b;
                cube=b*b*b;

                cout<<b<<"\t"<< square<<"\t"cub e<<endl;



                return 0;
                }

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  It still won't compile. There are errors in these two lines:
                  Code:
                  using namespace std:
                  cout<<b<<"\t"<<square<<"\t"cube<<endl;
                  However, I fixed them and when I enter 2 and a 3 my output is:

                  2
                  2 4 8
                  3
                  3 9 27

                  which is correct for the code.

                  Now if you are trying to make look like this:

                  2 4 8
                  3 9 27

                  then you can't have those cin>> lines throughout the code. You are seeing the echo from your data entry. You will need to have one cin>> at the beginning, enter all the numbers and then display all the results.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    I find this assignment extremely silly the way it is described now. The tabular
                    output simply screams for some form of loop. Just 'if statements' don't mean much.
                    I'm afraid the OP misunderstood some requirements.

                    kind regards,

                    Jos

                    Comment

                    • Nkhosinathie
                      New Member
                      • May 2007
                      • 91

                      #11
                      Originally posted by weaknessforcats
                      It still won't compile. There are errors in these two lines:
                      Code:
                      using namespace std:
                      cout<<b<<"\t"<<square<<"\t"cube<<endl;
                      However, I fixed them and when I enter 2 and a 3 my output is:

                      2
                      2 4 8
                      3
                      3 9 27

                      which is correct for the code.

                      Now if you are trying to make look like this:

                      2 4 8
                      3 9 27

                      then you can't have those cin>> lines throughout the code. You are seeing the echo from your data entry. You will need to have one cin>> at the beginning, enter all the numbers and then display all the results.
                      thanks for your help but still i want to know if i want to have that one cin statement only how must i do it?

                      Comment

                      Working...