i'm developing a c++ program here and also i'm nwe to c++.
i have been given a program that has to print integers from 0-10 with their squares and cubes in a table form. but the problem is i'm only allowed to use if statement onyl to do this program.well this is how i did it.

#include<iostream>
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<<"enter any number"<<endl;
cin>>a;
square=a*a;
cube=a*a*a;

cout<<a<<"\t"<<square<<"\t"<<cube<<endl;

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

cout<<b<<"\t"<<square<<"\t "<<cube<<endl;

return 0;
}

i repeated the statement until i used all the variables above.
it appears like this

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

my problem is how to remove this numbers between because it has to appear like this

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

Recommended Answers

All 9 Replies

I imagine the problem was designed to force you to use a recursive solution. That's really the only way to loop without using a loop.

so how can i use the recursive loop because firstly i'm new to c++ programming and also this programming has to be done using if statement only.i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn

instead of writing
using std::cout;
using std::cin;
using std::endl;

you can just write
using namspace std;

thanks i will do. but still whenever i compile this program it is leaving this numbers in between so i don't know how to deal with that.

instead of writing
using std::cout;
using std::cin;
using std::endl;

you can just write
using namspace std;

thanks i will do. but still whenever i compile this program it is leaving this numbers in between so i don't know how to deal with that.

>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn
:icon_rolleyes:

>and also this programming has to be done using if statement only
That's exactly why I think you're meant to use recursion. The only other way is to write the code inline:

int main()
{
  // Print the square and cube of 0
  // Print the square and cube of 1
  // Print the square and cube of 2
  ...
  // Print the square and cube of 9
}

That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:

int main()
{
  for ( int i = 0; i < 10; i++ ) {
    // Print the square and cube of i
  }
}

Which can be done with a recursive function using an if statement as the base case:

void f ( int i )
{
  if ( i < 10 ) {
    // Print the square and cube of i
    f ( i + 1 );
  }
}

int main()
{
  f ( 0 );
}

>you can just write
>using namspace std;
That's generally considered a bad practice though because it opens up names that you don't use and defeats the purpose of namespaces in avoiding naming collisions.

>but still whenever i compile this program it is leaving this numbers in
>between so i don't know how to deal with that.
The numbers in between are the user input being echoed to the screen. You can avoid it by requiring the user to enter all numbers immediately and then simply not prompting further:

int main()
{
  int x, y, z;

  cout<<"Enter three numbers: ";
  cin>> x;
  cout<< x <<'\n';
  cin>> y;
  cout<< y <<'\n';
  cin>> z;
  cout<< z <<'\n';
}

>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn
:icon_rolleyes:

>and also this programming has to be done using if statement only
That's exactly why I think you're meant to use recursion. The only other way is to write the code inline:

int main()
{
  // Print the square and cube of 0
  // Print the square and cube of 1
  // Print the square and cube of 2
  ...
  // Print the square and cube of 9
}

That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:

int main()
{
  for ( int i = 0; i < 10; i++ ) {
    // Print the square and cube of i
  }
}

Which can be done with a recursive function using an if statement as the base case:

void f ( int i )
{
  if ( i < 10 ) {
    // Print the square and cube of i
    f ( i + 1 );
  }
}

int main()
{
  f ( 0 );
}

>you can just write
>using namspace std;
That's generally considered a bad practice though because it opens up names that you don't use and defeats the purpose of namespaces in avoiding naming collisions.

>but still whenever i compile this program it is leaving this numbers in
>between so i don't know how to deal with that.
The numbers in between are the user input being echoed to the screen. You can avoid it by requiring the user to enter all numbers immediately and then simply not prompting further:

int main()
{
  int x, y, z;

  cout<<"Enter three numbers: ";
  cin>> x;
  cout<< x <<'\n';
  cin>> y;
  cout<< y <<'\n';
  cin>> z;
  cout<< z <<'\n';
}

thanks so much for your help i will try it and i will definitely get back to u'thanks

thanks so much for your help i will try it and i will definitely get back to u'thanks

another thing i can ask if u don't mind, i'm also doing a bank account program but i will do it and come back to u,but what i want to know about it is the understanding of how i have to do.

>but what i want to know about it is the understanding of how i have to do.
That's a different problem and a different question, so create a new thread when you start working on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.