Thread: Table of exponents...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Table of exponents...

    the question is, user inputs a BASE a MIN EXPONENT and a MAX EXPONENT. have the program print out a table with teh correct values of the answers.

    eg...

    Base Exponent Answer

    2 2 4
    2 3 8
    2 4 16
    2 5 32

    this is for the test data: Base = 2, MIN EXPONENT = 2, MAX EXPONENT = 5.

    i have to make it WITHOUT useing the power function to show we can use while loops, sentinal and counter.

    ---------------------------------------------------------------------------

    #include <stdio.h>
    #include <stdlib.h>

    void main()
    {
    int base, min_exponent, max_exponent, answer;
    int sentinel, counter, exponent;

    sentinel = 1;
    answer = 1;
    counter = 0;

    while (sentinel == 1)
    {
    answer = 1;
    /*Input the base, max_exponent and min_exponent*/
    /*Assign base, max_exponent and min_exponent to their variables*/
    printf("Please enter the base: \n");
    scanf("%d",&base);

    printf("Please enter the minimum exponent: \n");
    scanf("%d",&min_exponent);

    printf("Please enter the maximum exponent: \n");
    scanf("%d",&max_exponent);

    if (base >= 0 && base <= 10)
    //if (min_exponent > 0 && min_exponent < max_exponent)
    //if (max_exponent > min_exponent && max_exponent > 20)
    {
    counter = min_exponent;
    answer = min_exponent;
    exponent = min_exponent;
    while (counter <= max_exponent)
    {
    //if (exponent == 0)
    //{
    // printf("\n%d\t0\t1\n",base);
    //}
    //else
    answer = answer * base;
    printf("\n%d\t%d\t%d\n",base,exponent,answer);
    counter++;
    exponent++;
    }
    }
    else
    printf("Please enter data again:\n");
    /*Ask user if they want to run program [Y]es or [N]o*/
    printf("Do you wish to run program again?? 1 = yes or 0 = no\n");
    scanf("%d",&sentinel);
    }
    }

    ----------------------------------------------------------------------------------

    any ideas whats going wrong??

    thanx,

    DAVE.
    Dangerous Dave

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Can you use for-loops?
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    void main() 
    { 
       int base, min_exponent, max_exponent, answer; 
       int sentinel, counter, exponent; 
       int i;
    
       sentinel = 1; 
       while (sentinel == 1) 
       { 
          /*Input the base, max_exponent and min_exponent*/ 
          /*Assign base, max_exponent and min_exponent to their variables*/ 
          printf("Please enter the base: \n"); 
          scanf("%d",&base); 
    
          printf("Please enter the minimum exponent: \n"); 
          scanf("%d",&min_exponent); 
    
          printf("Please enter the maximum exponent: \n"); 
          scanf("%d",&max_exponent); 
    
          if (base >= 0 && base <= 10) 
          //if (min_exponent > 0 && min_exponent < max_exponent) 
          //if (max_exponent > min_exponent && max_exponent > 20) 
          { 
             answer = 1;
             for (i=0; i<min_exponent; i++)
                answer *= base;
             counter = min_exponent; 
             exponent = min_exponent; 
             while (counter <= max_exponent) 
             { 
                //if (exponent == 0) 
                //{ 
                // printf("\n%d\t0\t1\n",base); 
                //} 
                //else 
                   printf("\n%d\t%d\t%d\n",base,exponent,answer); 
                answer *= base;
                counter++; 
                exponent++; 
             } 
          } 
          else 
             printf("Please enter data again:\n"); 
          /*Ask user if they want to run program [Y]es or [N]o*/ 
          printf("Do you wish to run program again?? 1 = yes or 0 = no\n"); 
          scanf("%d",&sentinel); 
       } 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  2. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM
  3. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  5. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 03:49 PM