PDA

View Full Version : printing out square or cube

mivec
02-13-2005, 05:51 PM
hi all,
how do u output out square or cube in C? what i mean is like this:

5(cube)+4(cube)+3(cube)+2(cube)+1(cube)

i know there is a ascii code or something like that for the cube or square but not sure what it is or how to use it. a helping hand here would be very much appreciated...thanks

Dr. Evil
02-13-2005, 07:21 PM
Just make a simple test to view all the ASCII characters and their codes, like this:

#include <stdio.h>

int main()
{

int i;
for(i=0; i<500; i+=6)
{
printf("%d: %c\t%d: %c\t%d: %c\t%d: %c\t%d: %c\t%d: %c\n", i, i, i+1, i+1, i+2, i+2, i+3, i+3, i+4, i+4, i+5, i+5);
}

return 0;
}

cfc
02-13-2005, 07:30 PM
I found an ASCII reference table here:
http://www.lookuptables.com/

Hope it helps.

mivec
02-14-2005, 01:24 AM
thanks cfc but i dun think there is the ascii code for cube.....so, it din really help but if u have any more info on this, pls do reply...thanks a million

Mhtml
02-14-2005, 02:25 AM
You mean like a superscript 3? Like ³?

Dunna
02-14-2005, 04:06 AM
Say you wanted to square a number, you could use this code (which I'm coming up with right now):

//Somewhere up top:
long Power(long,long);

// Say you want to put 5 to the 8th power:
long x = Power(5,8);

//Somewhere down below (or up top):
long Power(long x, long y)
{
long result = x;
for (long i = 1; i < y; i++)
result += result * x;
return result;
}

Correct me if I'm wrong or there's an easier way, but I think that will do what

cfc
02-14-2005, 04:22 AM
Correct me if ... there's an easier way

double pow(double base, double exponent) (http://www.cplusplus.com/ref/cmath/pow.html) in <math.h>

Mhtml
02-14-2005, 04:33 AM
pow() is very slow though. But is that even the problem here? I thought it was just a ASCII question?

mivec
02-14-2005, 07:32 AM
yes, MHTML got what i meant.....so how do u print the power of 3 like in the quotes??

You mean like a superscript 3? Like ³?

Mhtml
02-14-2005, 07:44 AM
Try a typecast to 252, like this:

(char)252

So using printf:

printf("%c",252)

Will output ³.

[edit:] 253 will output a superscript 2.