Hey, again... I've got another assignment coming up and I'm having issues... again. Basically, my major stumbling block is figuring out how to set the int number = returned value of enterNewNumber.
Also, is there any simple way I can store the digits of an integer as seperate numbers in an array or string? ie int 2315 = int[4] = (2,3,1,5) etc etc
Thanks for your help... Now I'm crashing...
Code enclosed
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
int enterNewNumber();
int printReversed(int);
int checkPalindrome(int);
int checkPrime(int);
int printPrimeFast(int);
int printPrimeSlow(int);
int printPrimeFactors(int);
int main(void){
int choice;
int number;
printf(" 1. Enter a new number.\n 2. Print number reversed.\n 3. Check if number is a palindrome.\n 4. Check if number is prime.\n 5. Print all prime numbers up to number (slow).\n 6. Print all prime numbers up to number (fast).\n 7. Print the prime factorization of the number.\n 8. Quit\n");
printf("Please select an option from the list: ");
scanf("%d", &choice);
printf("You selected choice %d.\n", choice);
switch(choice){
case 1: number = enterNewNumber();
break;
case 2: printReversed(number);
break;
case 3: checkPalindrome(number);
break;
case 4: checkPrime(number);
break;
case 5: printPrimeFast(number);
break;
case 6: printPrimeSlow(number);
break;
case 7: printPrimeFactors(number);
break;
case 8: exit(0);
break;
default: printf("Your choice was invalid.\n Please choose a number from the list.\n");
main();
}
return 0;
}
int enterNewNumber(){
int n;
printf("Please enter an int > 2:\n");
scanf("%d", &n);
if(n<2){
printf("The int you entered was < 2.\n The int has been set equal to 2.\n");
n=2;
return n;
main();
}
main();
return n;
}
int printReversed(int number) {
int digit;
int n = number;
printf("This is your number: %d.\n", n);
printf("This is your number reversed: ");
while(n>0) {
digit=n%10;
printf("%d", digit);
n=n/10;
}
printf("\n");
main();
return 0;
}
int checkPalindrome(int number){
/*stuff*/
return 0;
}
int checkPrime(int number){
int n=number, den;
for(den=2; den*den<=number; ++den){
if(number%den==0){
printf("Your number is not prime.\n It is divisible by %d.\n", den);
main();
return 0;
}
printf("Your number is prime.\n");
return 0;
}
int printPrimeFast(int number){
/*stuff*/
return 0;
}
int printPrimeSlow(int number){
/*stuff*/
return 0;
}
int printPrimeFactors(int number){
/*stuff*/
return 0;
}