I have a program that tells if a number from 1 to 100 is "happy" or not. (look it up if you don't know what that is).
I want to change it so it gives the prime happy numbers and doesn't say the ones that aren't happy and/or prime. I found a "snippet" of code that tests if a number is prime, and I need to know how to adapt it into the code...
Note: The parts with the opening and closing of folders isn't important to look at. It's not really as much code as it seems.
This is the Happy Numbers Prgm:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
FILE *outputFile;
FILE *inputFile;
char buffer[81];
int bytesWritten;
main()
{
void isHappy(int);
int addSquareDigits(int);
int i;
/*************************************/
/* Open file to write output */
/*************************************/
outputFile = fopen("DD:OUTPUT", "w");
if (outputFile == NULL)
{
printf("open error: %d/%s\n", errno, strerror(errno));
exit(99);
}
/*************************************/
/* Open file to read input */
/*************************************/
inputFile = fopen("DD:INPUT", "r");
if (inputFile == NULL)
{
printf("open error: %d/%s\n", errno, strerror(errno));
exit(98);
}
/*************************************/
/* Run program */
/*************************************/
while (fscanf(inputFile,"%d",&i) != EOF) {
isHappy(i);
}
/*************************************/
/* Close output file */
/*************************************/
fclose(outputFile);
/*************************************/
/* Close input file */
/*************************************/
fclose(inputFile);
return 0;
}
int addSquareDigits (int myInt)
{
int digit = 0;
int addMe = 0;
int newInt = 0;
do
{
digit = myInt % 10;
addMe = digit*digit;
newInt = newInt + addMe;
myInt = (myInt - digit)/10;
} while (myInt>0);
return newInt;
}
void isHappy (int myInt)
{
int phases[50];
int loopy=0;
int newInt = myInt;
phases[1]=myInt;
int i,j;
for (i = 2; (i < 50 && loopy==0); i++)
{
newInt = addSquareDigits(newInt);
phases[i]=newInt;
if (newInt==1) {
bytesWritten = sprintf(buffer,"%d is happy! :)\n",myInt);
fwrite(buffer, 1, bytesWritten, outputFile);
loopy=1;
}
else {
for (j=1; j<i; j++)
{
if (newInt==phases[j])
{
bytesWritten = sprintf(buffer,"%d is unhappy :(\n",myInt);
fwrite(buffer, 1, bytesWritten, outputFile);
/* writeToFile(myInt+" is unhappy :("); */
loopy=1;
}
}
}
}
return;
}
I'm not an expert at this type of code, so I'm not sure how to do much of this. I know I need to get rid of where it prints if it's happy and replace that with the test for being prime code. I also need to get rid of the printing of unhappy. The following snippet for the prime # test needs to replace the printing of "# is happy" and test it.
Prime Test code:
Code:
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
int isprime(int value);
int main(int argc, char *argv[]) {
int number = 0;
if(argc != 2) {
fprintf(stderr, "Usage: isprime INT\n");
return 1;
} else
number = atoi(argv[1]);
if(isprime(number) == 0)
printf("%2d is NO prime\n", number);
else
printf("%2d is a prime...\n", number);
return 0;
}
int isprime(int value) {
int retval = -1;
int i = 0;
for(i = 2; i <= value / 2; i++) {
if(value % i == 0) {
retval = 0;
break;
}
}
return retval;
}
Notice the variables are different... those need to be adapted too. Any help?