Solved
Having Trouble with my C Script that determines Prime and Perfect numbers HELP!:)
Posted on 2004-10-28
I am having problems making this script below work. I want this script to check the numbers input from file imput.txt and tell me if they are Prime or Perfect. (example numbers would be 110, 7, 10, 6 etc. New to C and trying to learn it by doing these types of excersizes. Can someone tell me what I am doing wrong?
Thanks in advance,
Jennifer SEE SCRIPT BELOW:)
# include <stdio.h>
# include <math.h>
int main ()
{
FILE *inp;
FILE *outp;
int num, total, i;
float ans;
int IsPrim;
//Initialize
num = 0;
do
{
inp=fopen("C:input.txt", "r");
outp=fopen("C:output.txt", "w");
fscanf(inp, "%d", num);
printf(outp, "%d", num);
/* printf("\nEnter a number from 1 to 1000>");
scanf("%d", num); */
}
while (num<=0 || num>=1001);
//Initialize
total = 0;
ans = 0;
// For prime
ans = 1;
IsPrim = 1;
for(i = 2; i < num; i++);
if ( num % i == 0)
{
IsPrim=0;
printf("Divisor of prime");
}
if(IsPrim)
printf("It is Prim");
// For perfect
total = 0;
for ( i = 1; i < num; i++)
if(num%i==0)
total+=i;
if(total==num)
{
printf("Perfect");
for ( i = 1; i < num; i++)
if(num%i==0)
printf("Perfect divisor");
}
fclosef (inp);
fclosef (outp);
return (0);
}