Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

write a c++ prgm to read a number and its cube ?

OpenStudy (anonymous):

@nick67

OpenStudy (nick67):

@Yahoo! could you better explain what the program has to do ?

OpenStudy (anonymous):

we have to set a number eg a=10 and the otput should be its cube

OpenStudy (nick67):

but where is the difference with the previous program ?

OpenStudy (anonymous):

in 1 st one we have nt set any number

OpenStudy (anonymous):

in 1 st one it is cin

OpenStudy (anonymous):

we can enter any number

OpenStudy (nick67):

well, I'm a little confused; in the previous program you can enter any integer and it shows the cube of that number

OpenStudy (anonymous):

but in this we should set a number and show its cube

OpenStudy (anonymous):

example if we set a= 10 the out put should be a*a*a = 1000

OpenStudy (nick67):

so, in this program we don't have any input ? Output only ?

OpenStudy (zepp):

Why C++? This basic thing could be written in C.

OpenStudy (anonymous):

@nick67 no.........we should insert a input

OpenStudy (zepp):

double number; double result; scanf("lf", %number); result = number ^ 3; printf("This number cube is " %lf, number);

OpenStudy (anonymous):

@zepp i want this in C++

OpenStudy (nick67):

@Yahoo! so this is the previous program :-)

OpenStudy (anonymous):

No!!

OpenStudy (anonymous):

previous prgm we dont have an input such as a=18

OpenStudy (nick67):

sorry, I can't catch the difference :-(

OpenStudy (anonymous):

in this we should give an input and find its cube

OpenStudy (nick67):

the input in the previous program is the instruction: "cin >> value" and the user can enter any value with the keyboard

OpenStudy (anonymous):

YuP ..........Here we xant enter any number it should only display the cube of input number

OpenStudy (zepp):

#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

OpenStudy (anonymous):

@zepp not this much i need a simple prgm

OpenStudy (zepp):

That's simple D:

OpenStudy (anonymous):

@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; }

OpenStudy (anonymous):

here we can enter any number and it will show its cube

OpenStudy (zepp):

..That's what I did.

OpenStudy (anonymous):

i want a prgm to chow cube of given number

OpenStudy (anonymous):

@zepp u have enterned many things

OpenStudy (anonymous):

@zepp i am new to C++ thats why>....

OpenStudy (zepp):

Those are comments lol

OpenStudy (anonymous):

cant u remove all those comment and paste that with input such as a=10

OpenStudy (rsadhvika):

int main() { int n=18; int cube = n*n*n; cout << cube; return 0; }

OpenStudy (nick67):

@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

OpenStudy (zepp):

#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; }

OpenStudy (zepp):

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.

OpenStudy (zepp):

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; }

OpenStudy (anonymous):

thxxx guys))).............@zepp , @nick67 , @rsadhvika

OpenStudy (zepp):

yw

OpenStudy (nick67):

@Yahoo! you're welcome

OpenStudy (nick67):

@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" ? :-)

OpenStudy (zepp):

int cube was to calculate the cube of value

OpenStudy (zepp):

and value(0), cube(0) doesn't change anything, it only stores 0 first;

OpenStudy (nick67):

are you sure this sintax is correct ?

OpenStudy (zepp):

You can always check :)

OpenStudy (nick67):

I mean, can you declare again "int cube" after it's first declaration ? gcc gives error

OpenStudy (zepp):

I'm pretty sure I can

OpenStudy (zepp):

um, I did that with a const, maybe it won't work :s

OpenStudy (nick67):

I have this on gcc: error: redeclaration of ‘int cube’ error: ‘int cube’ previously declared here

OpenStudy (zepp):

try int const cube = value * value * value; instead?

OpenStudy (nick67):

but why re-declare cube? it's been already declared before

OpenStudy (zepp):

notice 'const'

OpenStudy (nick67):

ok, thank you

OpenStudy (zepp):

@nick67 Sorry, it shouldn't be declared twice, my bad.

OpenStudy (zepp):

http://puu.sh/M9lF

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!