Thread: Exponent If and Else Statements

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    1

    Exponent If and Else Statements

    Okay so I am programming and my tutor told me to fix my program about exponents and computing exponents. My program will only tell the answers when the exponent is 0 or greater than one. Whenever I enter 1 as my exponent it will tell me the answer to the equation is 1 instead of the base number. I was wondering if someone could help me with this. Here is my code:
    insert
    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    /* This program computes the power of a number */
    main()
    {
        int base,exponent,counter,i;
        printf("This program computes the power of a number using multiplication.\n");
        printf("Enter your base number:");
        base = GetInteger();
        counter = base;
        printf("Enter your exponent:");
        i = 0;
        exponent = GetInteger();
            if(exponent>1)
            {
            
                while(i<exponent){
                base = base*counter;
                i = i + 2;
                }    
            }
                else if (exponent>=0){
                    base = 1;
                }
                    else if (exponent >= 1)
                    {
                        base = counter;
                    }
                        else
                        {
                            printf("There is a bug in this program.");
                        }
        
            
        
        
        printf("%d to the power of %d is:%d.",counter,exponent,base);
    }
    Now I don't know what is wrong with this but if someone could help me that would be great!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first thing is indent code properly so you (and everyone else) can see the program flow at a glance.
    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    /* This program computes the power of a number */
    main()
    {
        int base, exponent, counter, i;
        printf("This program computes the power of a number using multiplication.\n");
        printf("Enter your base number:");
        base = GetInteger();
        counter = base;
        printf("Enter your exponent:");
        i = 0;
        exponent = GetInteger();
        if (exponent > 1) {
            while (i < exponent) {
                base = base * counter;
                i = i + 2;
            }
        } else if (exponent >= 0) {
            base = 1;
        } else if (exponent >= 1) {
            base = counter;
        } else {
            printf("There is a bug in this program.");
        }
    
        printf("%d to the power of %d is:%d.", counter, exponent, base);
    }
    Next, better variable names would help.
    I mean, you basically reuse 'base' as the result, and store the original value in 'counter'.

    Wouldn't it be better to just leave the base variable alone, and have another variable called 'result'
    So you have
    result = 0 // for exponent = 0
    result = base // for exponent = 1
    etc
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using an Exponent in C++
    By NismoT in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2011, 09:11 AM
  2. Does anyone know how to do exponent with just addition?
    By cowboyz209 in forum C Programming
    Replies: 11
    Last Post: 05-03-2011, 02:26 PM
  3. Some Exponent Help
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-04-2005, 07:31 PM
  4. Exponent help
    By flatline911 in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 01:34 AM
  5. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 02:24 PM

Tags for this Thread