The powers (that be)

This is a discussion on The powers (that be) within the C Programming forums, part of the General Programming Boards category; I wrote a program to multiply any floating point number by any integer either positive or negative: insert Code: int ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    powers

    I wrote a program to multiply any floating point number by any integer either positive or negative:

    insert
    Code:
    int main()
    {
        int count, y, finish;
        float x, z;
        
        while(finish!=0)
        {
        printf("Enter base number\n");
        scanf("%f", &x);
        printf("Enter power number\n");
        scanf("%d", &y);
        
    if (y>0)     
    {    
        z=1;
        for(count=1;count<=y;count++)
        {
         z=z*x;
        }
    }    
    
    else;
    {
        z=1;
        for(count=-1;count>=y;count--)
        {
         z=z*x;
        }
    }
    
    printf("Answer = %f\n", z);
    printf("Enter 0 to quit\n");
    scanf("%d", &finish);
    }
    }
    Every time it prints the answer as 1.00000 and i dont know why

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Probably because of the ; after else.

    FYI, it's not nice to hijack other people's threads to ask unrelated questions.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Split from the hijacked thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    BTW, fabs() from math.h might make your program simpler.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And pow() might make it even simpler. That might be carrying it a bit too far though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. On Women and Their Powers
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 03-30-2004, 03:55 PM
  2. Powers and simplifications, please help me
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-21-2003, 06:09 PM
  3. With super powers would you:
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-17-2003, 11:27 PM
  4. Help with powers
    By Tim in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 02:52 AM
  5. Square roots / Powers etc.
    By Robert602 in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2001, 03:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21