Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question printing out square or cube

    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
    Warm Regards,
    Mivec

  • #2
    New Coder
    Join Date
    Nov 2004
    Location
    Netherlands
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just make a simple test to view all the ASCII characters and their codes, like this:

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

  • #3
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found an ASCII reference table here:
    http://www.lookuptables.com/

    Hope it helps.
    Last edited by cfc; 02-13-2005 at 09:32 PM.

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    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
    Warm Regards,
    Mivec

  • #5
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    You mean like a superscript 3? Like ³?
    Omnis mico antequam dominus Spookster!

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    New Hampshire, America
    Posts
    246
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Say you wanted to square a number, you could use this code (which I'm coming up with right now):

    Code:
    //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
    you're asking.

  • #7
    cfc
    cfc is offline
    Regular Coder
    Join Date
    Dec 2004
    Location
    Keswick, Ontario
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dunna
    Correct me if ... there's an easier way
    double pow(double base, double exponent) in <math.h>

  • #8
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    pow() is very slow though. But is that even the problem here? I thought it was just a ASCII question?
    Omnis mico antequam dominus Spookster!

  • #9
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ³?
    Warm Regards,
    Mivec

  • #10
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try a typecast to 252, like this:

    Code:
    (char)252
    So using printf:

    Code:
    printf("%c",252)
    Will output ³.

    [edit:] 253 will output a superscript 2.
    Last edited by Mhtml; 02-14-2005 at 06:53 AM.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •