| | |
someone please help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 30
Reputation:
Solved Threads: 0
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
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
>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn

>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:
That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:
Which can be done with a recursive function using an if statement as the base case:
>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:
>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:
C++ Syntax (Toggle Plain Text)
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 }
C++ Syntax (Toggle Plain Text)
int main() { for ( int i = 0; i < 10; i++ ) { // Print the square and cube of i } }
C++ Syntax (Toggle Plain Text)
void f ( int i ) { if ( i < 10 ) { // Print the square and cube of i f ( i + 1 ); } } int main() { f ( 0 ); }
>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:
C++ Syntax (Toggle Plain Text)
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 here to prove you wrong.
•
•
Join Date: May 2007
Posts: 30
Reputation:
Solved Threads: 0
•
•
•
•
>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn
>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:
That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:C++ Syntax (Toggle Plain Text)
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 }
Which can be done with a recursive function using an if statement as the base case:C++ Syntax (Toggle Plain Text)
int main() { for ( int i = 0; i < 10; i++ ) { // Print the square and cube of i } }
>you can just writeC++ Syntax (Toggle Plain Text)
void f ( int i ) { if ( i < 10 ) { // Print the square and cube of i f ( i + 1 ); } } int main() { f ( 0 ); }
>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:
C++ Syntax (Toggle Plain Text)
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'; }
•
•
Join Date: May 2007
Posts: 30
Reputation:
Solved Threads: 0
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: I have a questoin about 2Dimenional Array?
- Next Thread: frustrated newbie needs help
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






