write a c++ prgm to read a number and its cube ?
@nick67
@Yahoo! could you better explain what the program has to do ?
we have to set a number eg a=10 and the otput should be its cube
but where is the difference with the previous program ?
in 1 st one we have nt set any number
in 1 st one it is cin
we can enter any number
well, I'm a little confused; in the previous program you can enter any integer and it shows the cube of that number
but in this we should set a number and show its cube
example if we set a= 10 the out put should be a*a*a = 1000
so, in this program we don't have any input ? Output only ?
Why C++? This basic thing could be written in C.
@nick67 no.........we should insert a input
double number; double result; scanf("lf", %number); result = number ^ 3; printf("This number cube is " %lf, number);
@zepp i want this in C++
@Yahoo! so this is the previous program :-)
No!!
previous prgm we dont have an input such as a=18
sorry, I can't catch the difference :-(
in this we should give an input and find its cube
the input in the previous program is the instruction: "cin >> value" and the user can enter any value with the keyboard
YuP ..........Here we xant enter any number it should only display the cube of input number
#include <iostream> using namespace std; int main() { double number(0) // Declare variables cout << "Enter a number" << endl; cout << ">>>"; //Store number in the variable number cin >> number; double const resultat(number^3); cout << This number cubed << " = " << resultat << endl; // Display result return 0; } Haven't done C++ in ages :S
@zepp not this much i need a simple prgm
That's simple D:
@zepp i need such a prgm #include <iostream> using namespace std; int main() { int value, cube; cin >> value; cube = value*value*value; cout << cube; return 0; }
here we can enter any number and it will show its cube
..That's what I did.
i want a prgm to chow cube of given number
@zepp u have enterned many things
@zepp i am new to C++ thats why>....
Those are comments lol
cant u remove all those comment and paste that with input such as a=10
int main() { int n=18; int cube = n*n*n; cout << cube; return 0; }
@Yahoo! if you execute the previous program (the one you closed the question) and you enter as input the number 10 it shows you as output the number 1000
#include <iostream> using namespace std; int main() { int value(0), cube(0); // Declare variables here cout << "???" << endl; // Here we are going to ask the user to enter a number cin >> value; // Store the value you've just entered in the variable value int cube = value * value * value; // Do the operation cout << Cube; // Display result return 0; }
If you don't like comments then... #include <iostream> using namespace std; int main() { int value(0), cube(0); cout << "???" << endl; cin >> value; int cube = value * value * value; cout << Cube; return 0; } comments are useful if you want to double check your code (big program takes thousand line), it's quite useful.
Sorry, lowercase C in cube, C/C++ are case sensitive. #include <iostream> using namespace std; int main() { int value(0), cube(0); cout << "???" << endl; cin >> value; int cube = value * value * value; cout << cube; return 0; }
thxxx guys))).............@zepp , @nick67 , @rsadhvika
yw
@Yahoo! you're welcome
@zepp just a few questions: why did you declare "value(0), cube(0)" instead of "value, cube" ? and why did you declare again "int cube" ? :-)
int cube was to calculate the cube of value
and value(0), cube(0) doesn't change anything, it only stores 0 first;
are you sure this sintax is correct ?
You can always check :)
I mean, can you declare again "int cube" after it's first declaration ? gcc gives error
I'm pretty sure I can
um, I did that with a const, maybe it won't work :s
I have this on gcc: error: redeclaration of ‘int cube’ error: ‘int cube’ previously declared here
try int const cube = value * value * value; instead?
but why re-declare cube? it's been already declared before
notice 'const'
ok, thank you
@nick67 Sorry, it shouldn't be declared twice, my bad.
Join our real-time social learning platform and learn together with your friends!