- Prime or Composite: does the number have factors other than 1 and itself?
- Even or Odd: is the number divisible by 2 without a remainder?
- Fibonacci Number: is the number an element of the set of all F whereby Fn+1=Fn + Fn-1 where F0 = 1 and F1 = 1?
- Lucas Number: is the number an element of the set of all F whereby Fn+1=Fn + Fn-1 where F0 = 1 and F1 = 3?
- Perfect Number: does the number's factors add up to itself?
- Almost Perfect Number: is the number one off from a perfect number? (All known almost perfect numbers are powers of 2).
- Power of 2: is the number a power of 2?
- Perfect Square: is the square root of the number an integer?
- Perfect Cube: is the cube root of the number an integer?
- Triangular Number: is the number an element of the set of all xn whereby xn = n(n+1)/2 ?
- Amicable Numbers: for number A and number B, does number A's factors add to number B, and number B's factors add to number A?
You tell the program whether you want to enter 1 or 2 numbers. If you enter 1 number, it tests if the number has all the above properties, except for amicability. If you enter 2 numbers, it tests if both the numbers has all the above properties, and if they're amicable. Additionally, it shows their product, sum, difference, and division.
Here's the code for it:
#include <stdlib.h> #include <stdio.h> #include <float.h> #include <math.h> #define DBL_EPSILON 2.2204460492503131e-16 int perfectNumber(int theNum) { int divisorList[100]; int j = 0; int sumTotal = 0; for (int i = 1; i < theNum; i++) { if (theNum % i == 0) { divisorList[j] = i; j++; sumTotal += i; } } if (sumTotal == theNum) { return 1; } else { return 0; } } int primeNumber(int theNum) { int divisorList[100]; int j = 0; int sumTotal = 0; for (int i = 1; i < theNum; i++) { if (theNum % i == 0) { divisorList[j] = i; j++; sumTotal += i; } } if (sumTotal == 1) { return 1; } else { return 0; } } int evenOdd(int theNum) { if (theNum % 2 == 0) { return 1; } else { return 0; } } int almostPerfectNumber(int theNum){ int divisorList[100]; int j = 0; int sumTotal = 0; for (int i = 1; i < theNum; i++) { if (theNum % i == 0) { divisorList[j] = i; j++; sumTotal += i; } } if (sumTotal + 1 == theNum) { return 1; } else { return 0; } } int fibonacciNumber(int theNum) { int fib1 = 1; int fib2 = 1; int tempFib; int fibList[3000]; fibList[0] = fib1; fibList[1] = fib2; for(int i = 2; i < sizeof(fibList)/sizeof(int); i++){ tempFib = fib1+fib2; fib1 = fib2; fib2 = tempFib; fibList[i] = tempFib; } for(int i = 0; i < sizeof(fibList)/sizeof(int); i++) { if (theNum == fibList[i]){ return 1; } } } int lucasNumber(int theNum) { int fib1 = 1; int fib2 = 3; int tempFib; int fibList[3000]; fibList[0] = fib1; fibList[1] = fib2; for(int i = 2; i < sizeof(fibList)/sizeof(int); i++){ tempFib = fib1+fib2; fib1 = fib2; fib2 = tempFib; fibList[i] = tempFib; } for(int i = 0; i < sizeof(fibList)/sizeof(int); i++) { if (theNum == fibList[i]){ return 1; } } } int perfectCube(int theNum) { double fTheNum = (double)theNum; if (theNum > 0){ for (int i = 1; i < theNum; i++) { if (fabs(i-cbrt(fTheNum)) < DBL_EPSILON) { return 1; break; } } } } int perfectSquare(int theNum) { double fTheNum = (double)theNum; if (theNum > 0){ for (int i = 1; i < theNum; i++) { if (fabs(i-sqrt(fTheNum)) < DBL_EPSILON) { return 1; break; } } } } void factors(int theNum) { if (theNum > 0) { int divisorLen = 100; int divisorList[divisorLen]; int j = 0; int sumTotal = 0; printf("The factors of %d are: ", theNum); for (int i = 1; i < theNum; i++) { if (theNum % i == 0) { divisorList[j] = i; j++; sumTotal += i; printf("%d, ", i); } } if (theNum > 1){ printf("and %d \n", theNum); } if (theNum == 1){ printf("1"); } } } int amicableNumbers(int theNum, int theNum2) { int divisorList[100]; int j = 0; int sumTotal = 0; for (int i = 1; i < theNum; i++) { if (theNum % i == 0) { divisorList[j] = i; j++; sumTotal += i; } } int divisorList2[100]; int k = 0; int sumTotal2 = 0; for (int i = 1; i < theNum2; i++) { if (theNum2 % i == 0) { divisorList[k] = i; k++; sumTotal2 += i; } } if ((sumTotal == theNum2) && (sumTotal2 == theNum)){ return 1; } else { return 0; } } int triangularNumber(int theNum){ int triangleLength = 10000; int triangularList[triangleLength]; for(int i = 0; i < triangleLength; i++) { triangularList[i] = (i * (i + 1))/2; } for (int i = 0; i < triangleLength; i++) { if (theNum == triangularList[i]) { return 1; break; } } return 0; } int main() { int userNum = 1; int userNum2 = 0; int comparisons = 3; while (comparisons != 0){ printf("Do you want the properties of one number or a pair of numbers (Enter 1 or 2, or 0 to quit.): \n"); scanf("%d", &comparisons); if (comparisons == 0) { continue; } if (comparisons == 1) { printf("Enter a non-zero integer: \n"); scanf("%d", &userNum); printf("\n"); } else if (comparisons == 2) { printf("Enter a pair of non-zero integers, separated by a space: \n"); scanf("%d %d", &userNum, &userNum2); printf("\n"); } else { printf("You've entered an invalid response. Please try again. \n\n"); } if ((comparisons == 1) || (comparisons == 2)) { if(userNum != 0) { if (evenOdd(userNum) == 1){ printf("%d is an even number. \n", userNum); } if (evenOdd(userNum) == 0) { printf("%d is an odd number. \n", userNum); } if (perfectNumber(userNum) == 1) { printf("%d is a perfect number. \n", userNum); } if (primeNumber(userNum) == 1) { printf("%d is a prime number. \n", userNum); } if (primeNumber(userNum) == 0) { printf("%d is a composite number. \n", userNum); } if (almostPerfectNumber(userNum) == 1) { printf("%d is an almost perfect (deficient) number. \n", userNum); printf("%d is a power of 2. \n", userNum); } if (fibonacciNumber(userNum) == 1) { printf("%d is a Fibonacci number. \n", userNum); } if (lucasNumber(userNum) == 1) { printf("%d is a Lucas number. \n", userNum); } if (perfectSquare(userNum) == 1) { printf("%d is a perfect square. (The square root of %d is %d.) \n", userNum, userNum, (int)sqrt(userNum)); } if (perfectCube(userNum) == 1) { printf("%d is a perfect cube. (The cube root of %d is %d.) \n", userNum, userNum, (int)cbrt(userNum)); } if (triangularNumber(userNum) == 1){ printf("%d is a triangular number. \n", userNum); } factors(userNum); printf("\n"); } if (userNum2 != 0){ if (evenOdd(userNum2) == 1){ printf("%d is an even number. \n", userNum2); } if (evenOdd(userNum2) == 0) { printf("%d is an odd number. \n", userNum2); } if (perfectNumber(userNum2) == 1) { printf("%d is a perfect number. \n", userNum2); } if (primeNumber(userNum2) == 1) { printf("%d is a prime number. \n", userNum2); } if (primeNumber(userNum2) == 0) { printf("%d is an composite number. \n", userNum2); } if (almostPerfectNumber(userNum2) == 1) { printf("%d is an almost perfect (deficient) number. \n", userNum2); printf("%d is a power of 2. \n", userNum2); } if (fibonacciNumber(userNum2) == 1) { printf("%d is a Fibonacci number. \n", userNum2); } if (lucasNumber(userNum2) == 1) { printf("%d is a Lucas number. \n", userNum2); } if (perfectSquare(userNum2) == 1) { printf("%d is a perfect square. (The square root of %d is %d.) \n", userNum2, userNum2, (int)sqrt(userNum2)); } if (perfectCube(userNum) == 1) { printf("%d is a perfect cube. (The cube root of %d is %d.) \n", userNum2, userNum2, (int)cbrt(userNum2)); } if (triangularNumber(userNum2) == 1){ printf("%d is a triangular number. \n", userNum2); } factors(userNum2); printf("\n"); if (amicableNumbers(userNum, userNum2) == 1) { printf("%d and %d are amicable numbers. \n\n", userNum, userNum2); } printf("The sum of %d and %d is %d. \n", userNum, userNum2, userNum+userNum2); printf("The difference between %d and %d is %d. \n", userNum, userNum2, userNum-userNum2); printf("The product of %d and %d is %d. \n", userNum, userNum2, userNum * userNum2); printf("The division of %d and %d is about %0.2f. \n", userNum, userNum2, (float)userNum/(float)userNum2); printf("\n\n"); } } } }