Guest_imported
New member
- Jan 1, 1970
- 0
I'm trying to teach myself C and have been somewhat successful so far, but have hit a snag with a program that I am trying to write. All I want it to do is ask the user to input a number, then tell the user whether or not that number is prime and, if not, what its divisors are. I thought I had it all done perfectly, but the output isn't working.
Here is my code:
/* This Program Asks the user for a number, then determines that numbers divisors or if that number is prime */
#include <stdio.h>
main()
{
int num, numdiv, halfnum, divisors[50], div;
/*explains program and asks for number in question*/
printf("This program determines whether or not a number is prime.\n"
printf("It will also tell you the divisors of any non-prime numbers.\n"
printf("\nPlease enter a number: "
scanf(" %i", &num);
/*ensures that halfnum will be equal to or greater than half of num*/
if (num % 2 == 1)
{numdiv = num++;}
else
{numdiv = num;}
halfnum = numdiv/2;
/*tests integers from two to half of num for divisability*/
int cntdivs = 0;
for (div=2; div<=halfnum; div++)
{
/*if divisors found, store in "divisors" array*/
if (num % div == 0)
{
divisors[cntdivs] = div;
cntdivs++;
}
}
if (cntdivs == 0)
{printf("\n\n %i is a prime number!", &num);}
else
{
int cntprint;
printf("\n\n%i is not prime.\n\n", &num);
printf("It is divisable by: "
for (cntprint=0; cntprint<=cntdivs; cntprint++)
{printf(" %i and", &divisors[cntprint]);}
printf(", of course, 1 and itself!\n\n"
}
return 0;
}
The program runs the way it should, but it adopts VERY odd number values when printing the interger num and its divisors. Here is a sample output:
***output***
This program determines whether or not a number is prime.
It will also tell you the divisors of any non-prime numbers
Please enter a number: 10
1245052 is not prime.
It is divisable by: 1244844 and 1244848 and 1244852 and, of course, 1 and itself!
***end output***
does this make sense to anyone?? I tried initializing num with a value of 0 before modifying its value using sendf(), but I got the same exact result. I hope someone is able to help me, i'd greatly appreciate the assistance from you programming geniuses out there ;-).
Here is my code:
/* This Program Asks the user for a number, then determines that numbers divisors or if that number is prime */
#include <stdio.h>
main()
{
int num, numdiv, halfnum, divisors[50], div;
/*explains program and asks for number in question*/
printf("This program determines whether or not a number is prime.\n"
printf("It will also tell you the divisors of any non-prime numbers.\n"
printf("\nPlease enter a number: "
scanf(" %i", &num);
/*ensures that halfnum will be equal to or greater than half of num*/
if (num % 2 == 1)
{numdiv = num++;}
else
{numdiv = num;}
halfnum = numdiv/2;
/*tests integers from two to half of num for divisability*/
int cntdivs = 0;
for (div=2; div<=halfnum; div++)
{
/*if divisors found, store in "divisors" array*/
if (num % div == 0)
{
divisors[cntdivs] = div;
cntdivs++;
}
}
if (cntdivs == 0)
{printf("\n\n %i is a prime number!", &num);}
else
{
int cntprint;
printf("\n\n%i is not prime.\n\n", &num);
printf("It is divisable by: "
for (cntprint=0; cntprint<=cntdivs; cntprint++)
{printf(" %i and", &divisors[cntprint]);}
printf(", of course, 1 and itself!\n\n"
}
return 0;
}
The program runs the way it should, but it adopts VERY odd number values when printing the interger num and its divisors. Here is a sample output:
***output***
This program determines whether or not a number is prime.
It will also tell you the divisors of any non-prime numbers
Please enter a number: 10
1245052 is not prime.
It is divisable by: 1244844 and 1244848 and 1244852 and, of course, 1 and itself!
***end output***
does this make sense to anyone?? I tried initializing num with a value of 0 before modifying its value using sendf(), but I got the same exact result. I hope someone is able to help me, i'd greatly appreciate the assistance from you programming geniuses out there ;-).