What is a nasty number? Well it is a number whose four different factors can be arranged in such a way that sum of two is equal to difference of other two. Take an example of nasty number 36. It have factors 1,2,3,4,6,9,12,18. So, we have 2+1=6-3 . So, it is a nasty number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include < stdio.h > #include < conio.h > void main() { int i, j, yes = 0, z = 0, d = 0, v = 0, f = 0, compare, compare2, number, n = 0, ary_fac[100], ary_plus[100][100], ary_minus[100][100]; clrscr(); do { // Get the number from user to check if nasty printf("Enter any number less than 1000\n"); scanf("%d", & number); if (number > 1000) printf("Number is more than 1000\n"); } while (number > 1000); if (number < 0) // if number is negative make it positive number = -number; for (i = 1; i <= number; i++) { //loop to find factors if ((number % i) == 0) { ary_fac[n] = i; n++; } } //for loop ends for finding factors for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { ary_plus[i][j] = ary_fac[i] + ary_fac[j]; // loop to add numbers in matrix way } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { ary_minus[i][j] = ary_fac[i] - ary_fac[j]; // loop to minus numbers in matrix way if (ary_minus[i][j] < 0) ary_minus[i][j] = -ary_minus[i][j]; } } for (i = 1; i <= n * n; i++) { // loop to get the value to be compared from plus arrays compare = ary_plus[d][z]; z++; if (z == n) { //regulate the value of array index z = 0; // make z zero if it is equal to number of columns if (d < n) { // increase d until it is less than n. d++; } } for (j = 1; j <= n * n; j++) { // loop to get the value to be compared from minux array compare2 = ary_minus[v][f]; // regulate the value of array index f++; if (f == n) { f = 0; v++; if (v >= n) { // make v zero so that it is reset and we can compare all the values again with plus array v = 0; } } if (compare == compare2 && d != z && d != v && d != f && v != f && z != f && z != v) { // compare above values and make sure factors are different on both sides. yes = 1; } // if condition is satisfied then make yes=1 } } if (yes == 1) { // print if nasty number printf("yes it is a nasty no."); } else { printf("Not a nasty number"); } getch(); } |