Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange integer behavior-- any geniuses available?

Status
Not open for further replies.

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(&quot;This program determines whether or not a number is prime.\n&quot;);
printf(&quot;It will also tell you the divisors of any non-prime numbers.\n&quot;);
printf(&quot;\nPlease enter a number: &quot;);
scanf(&quot; %i&quot;, &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 &quot;divisors&quot; array*/
if (num % div == 0)
{
divisors[cntdivs] = div;
cntdivs++;
}
}
if (cntdivs == 0)
{printf(&quot;\n\n %i is a prime number!&quot;, &num);}
else
{
int cntprint;
printf(&quot;\n\n%i is not prime.\n\n&quot;, &num);
printf(&quot;It is divisable by: &quot;);
for (cntprint=0; cntprint<=cntdivs; cntprint++)
{printf(&quot; %i and&quot;, &divisors[cntprint]);}
printf(&quot;, of course, 1 and itself!\n\n&quot;);
}

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 ;-).
 
Hi quamusa,

Try to remove the '&' from the printf statements in the bottom of the code.
The '&' returns the addresses of your input and results instead of the values, and should not be used here ...

/JOlesen
 
Problem 1 : Format-argument mismatch
You're passing the wrong argument to printf(). You want to print the value of num not its address.
Problem 1 : Locality of declaration
All variable declarations in C are done at the beginning of the block. No locality of declaration as in C++.

Here is your code( with a little formatting for readability :))
Code:
/* This Program Asks the user for a number, then determines that numbers divisors or if that number is prime */

#include <stdio.h>

int main(void)

    {
        int num, numdiv, halfnum, divisors[50], div;
        int cntdivs = 0;	/* DZH */
        
        /*explains program and asks for number in question*/
        printf(&quot;This program determines whether or not a number is prime.\n&quot;);
        printf(&quot;It will also tell you the divisors of any non-prime numbers.\n&quot;);
        printf(&quot;\nPlease enter a number: &quot;);
        scanf(&quot; %i&quot;, &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*/
        /*
         * All variable declarations in C are done at the beginning of the block. No
         * locality of declaration as in C++. With a C compiler the following will trigger
         * an error.
         * David Zhuwao.
         */
        /*int cntdivs = 0;*/
        for (div=2; div<=halfnum; div++)        
        {
        /*if divisors found, store in &quot;divisors&quot; array*/
            if (num % div == 0)
            {
                divisors[cntdivs] = div;
                cntdivs++;
            }
        }
        if (cntdivs == 0)
        {
        	/* Corrected format-argument mismatch. David */
        	printf(&quot;\n\n %i is a prime number!&quot;, num);
        }
        else
        {
            int cntprint;	/* OK! */
            
            /* Corrected format-argument mismatch. David */
            printf(&quot;\n\n%i is not prime.\n\n&quot;, num);
            printf(&quot;It is divisable by: &quot;);
            for (cntprint=0; cntprint<=cntdivs; cntprint++)
            {
            	/* Corrected format-argument mismatch. David */
            	printf(&quot; %i and&quot;, divisors[cntprint]);
            }
            printf(&quot;, of course, 1 and itself!\n\n&quot;);
        }

        return 0;

}

The above code compiled and executed successfully using GCC (2.95.3-5) under CYGWIN.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top