Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Dec 6th, 2018 53 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include <stdio.h>
  2. int powerOfNum(int base, int exponent);
  3. int recursiveAdd(int number);
  4. int printMenu();
  5.  
  6. int recursiveAdd(int number)
  7. {
  8.   if (number != 0)
  9.   {
  10.     return (number % 10 + recursiveAdd(number/10));
  11.   }
  12.   else
  13.   {
  14.     return 0;
  15.   }
  16. }
  17.  
  18. int powerOfNum(int base, int exponent)
  19. {
  20.   if(exponent == 0)
  21.   {
  22.     return 1;
  23.   }
  24.   if(exponent == 1)
  25.   {
  26.     return base;
  27.   }
  28.   else
  29.   {
  30.     return base * powerOfNum(base, exponent-1);
  31.   }
  32. }
  33.  
  34. int printMenu()
  35. {
  36.   printf("%s\n%s\n", "Menu:","************************");
  37.   printf("%s\n%s\n%s\n\n", "(1) Add the digits of an integer together","(2) Find the power of a number","(3) Quit");
  38.   return 0;
  39. }
  40.  
  41. int main (void)
  42. {
  43.   char input = '0';
  44.   int numToAdd;
  45.   int addResult;
  46.   int baseNum;
  47.   int exponentNum;
  48.   int powerResult;
  49.  
  50.   int quitLoop = 0;
  51.  
  52.   printf("%s%s\n\n","To select a menu option, type the number that corresponds to that option.","\nEx: Type '2' to find the power of a number");
  53.   printMenu();
  54.  
  55.   while (quitLoop == 0)
  56.   {
  57.     scanf("%c", &input);
  58.  
  59.     if(input == '1')
  60.     {
  61.       printf("\n%s\n", "Please enter a number to add the digits of:");
  62.       scanf("%d", &numToAdd);
  63.  
  64.       addResult = recursiveAdd(numToAdd);
  65.  
  66.       printf("\nThe number %d yields the result %d\n\n", numToAdd, addResult);
  67.  
  68.       printMenu();
  69.     }
  70.     if (input == '2')
  71.     {
  72.       printf("\n%s\n", "Please enter a base number:");
  73.       scanf("%d", &baseNum);
  74.  
  75.       printf("%s\n", "Please enter an exponent:");
  76.       scanf("%d", &exponentNum);
  77.  
  78.       powerResult = powerOfNum(baseNum, exponentNum);
  79.  
  80.       printf("\nThe number %d to the power of %d is %d\n\n", baseNum, exponentNum, powerResult);
  81.       printMenu();
  82.  
  83.     }
  84.     if (input == '3')
  85.     {
  86.       quitLoop = 1;
  87.       printf("%s\n", "Goodbye!");
  88.     }
  89.   }
  90.   return 0;
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top