/* Filename: HW3.c*/ /* This program gives a menu for finding Factorials, * * Powers, Factors, Prime Numbers, Prime Factors, and Palindromes*\ /****************************************************************** * Programmer: Matthew Glynn * * Date : 02/07/2007 * * Homework # 3 * * HW3.c * * Algorithm: This program gives a menu for finding Factorials, * * Powers, Factors, Prime Numbers, Prime Factors, and Palindromes * * * * I have included the Factorial and Prime Number Problems. * ******************************************************************/ /* Submitted to ICON Dropbox at 5:07 PM on 02/0702007*/ #include /*the library to be used */ void printMenu(); /*User-defined function */ void printMenu() /* what is iside the function printMenu()*/ { printf("\n Welcome to Homework number 3!\n"); printf("\n 1. * Factorial\n "); printf("2. Power\n "); printf("3. Factors\n "); printf("4. * Prime Numbers\n "); printf("5. Prime Factors\n "); printf("6. Palindromes\n "); printf("================\n "); printf("0. Exit\n "); printf("\nEnter your choice: "); }/* end printMenu() */ int main( void ) /* function main begins program execution */ { int choice = 1; /* the selection from the menu */ int i; /* used as a constant in the factorial code */ int factorial; /* used as a variable in the factorial code */ int value; /* used as a variable in the factorial code */ int loop = 1; /* used for the while loop */ int p, n, div; /* used in Prime Numbers Program */ while(loop > 0 && choice != 0) /* loops the menu and sub programs until 0 is entered as a choice */ { printMenu(); /* Calling the user-defined function printMenu()*/ scanf("%d", &choice); /* obtain menu choice from user*/ switch (choice) /* select the type of calculation */ { case 0: /* 0 is the choice to exit the program */ printf("\nHave a good day!\n\n"); /* a nice way to say good bye*/ break; case 1: /* This is the factorial program */ { printf("\nWelcome to Factorials!!!\n"); printf("\nCompute the factorial of an integer.\n\n Enter an integer: "); scanf("%d",&value); /* Read value from user */ factorial=1; if (value >= 1 && value <= 10)/*check if value is in range of 1-10 */ { /*calculation of the factorial */ for (i=1; i<=value; i++) factorial=factorial*i; printf("%d! = %d\n\n",value,factorial); } else printf("\nPlease enter a number from 1-10 for factorials\n\n"); }/* end of factorial program */ break; case 2: printf("Power has not been created yet\n"); break; case 3: printf("Factors has not been created yet\n"); break; case 4: { printf("\nWelcome to Prime Numbers!!!\n"); printf("\n Find prime numbers less than an integer. \n"); printf("\nEnter an integer: "); scanf("%d", &n);/* Read value from user */ printf("\n\nPrime numbers less than %d: \n\n",n); for( p = 2; p <= n; p++) { /* try to find a divisor of p */ for( p = 2; p <= n; p++) { /* try to find a divisor of p */ for(div = 2; div < p; div++) { if( p % div == 0) /* found a divisor */ break; /* so p is not a prime */ } if (div == p) /* exhausted possible divisors */ printf(" %d\n ", p); /* display result of calculation */ /* so p is prime */ } printf("\n"); } } break; case 5: printf("Prime Factors has not been created yet\n"); break; case 6: printf("Prime Numbers has not been created yet\n"); break; default: printf("This is not an option\n"); } }/*end while*/ return 0; /* indicate program ended successfully */ } /* ends function main */ /************************************************************************* * Programmed by: Matt Glynn * *************************************************************************/