Thread: Problem in for loop

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    16

    Problem in for loop

    I need to write a program that will determine whether a number is divisible by 7, 11, or 13, if the sum of its digits is even or odd, and if the number is prime.
    I'm pretty sure I have almost all of the code right, but I am running into a problem with my for loop to determine primes. When I enter a number, say 3733, the program returns:

    Enter a number: 3773
    3773 is a multiple of 7.
    The sum of the digits of 3773 is even.
    3773 is not a prime number
    3773 is a prime number

    I have tried moving around my prime number printf but can't get the program to work correctly. If anyone has a suggestion I'd greatly appreciate it.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    	int i, sum=0, temp, number;
     
    	printf("Enter a number: ");
    	scanf("%d", &number);
    
    /*MULTIPLE OF 7, 11, OR 13?*/
    	if(number == 0)
    	printf("0 is not a multiple of any number.");
    	else
    	{
    		if (number % 7 == 0)
    	        printf("%d is a multiple of 7.\n", number);
    	        else if (number % 11 == 0)
    		printf("%d is a multiple of 11.\n", number);
    	        else if (number % 13 == 0)
    		printf("%d is a multiple of 13.\n", number);
    	        else
    		printf("%d is not a multiple of 7, 11, or 13.\n", number);
    	}
    
    
    /*SUM OF DIGITS EVEN OR ODD?*/
    	temp = number;
    
    	while(temp > 0)
    	{
    		sum = sum + (temp % 10);
    		temp = temp / 10;
    	}
    		if (sum % 2 == 0)
    		printf("The sum of the digits of %d is even.\n", number);
    		else
    		printf("The sum of the digits of %d is odd.\n", number);
    
    /*PRIME NUMBER?*/
    	if(number == 2)
    	printf("2 is a prime number.\n");
    	else if((number <= 2) || (number % 2 == 0))
    	printf("%d is not a prime number\n", number);
    	else
    	{
    		for(i = 3; i < sqrt(number); i = i + 2)
    		{
    			if ((number % i) == 0)
    			{
    				printf("%d is not a prime number\n", number);
    				break;
    			}
    			else printf("%d is a prime number\n", number);
    		}
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Each iteration of your prime-checking loop prints out "%d is a prime number" if the iterator (“i”) is not divisible by two. You need to put that off till after the loop has run and you've found no factors. It's OK to immediately signal if it's not a prime, because that's known as soon as you see that “i” is a factor; but “i” not being a factor is not sufficient to determine whether it's prime; all “i” values must be checked.

  3. #3
    INSANE INSIDE ekosix's Avatar
    Join Date
    May 2010
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    I recoded your solution:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int number, prime = 1;
    	
    	printf("Number: ");
    	fflush(stdin);
    	scanf("%d", &number);
    
    	for(int i = (number - 1); i > 1; i--)
    	{
    		if(number % i == 0)
    		{
    			prime = 0;
    			printf("%d is not prime.", number);
    			break;
    		}
    	}
    	
    	if(prime) printf("%d is prime.", number);
    	
    	system("pause>>nul");
    	return 0;
    }
    Works great.
    You used a too much unnecessary stuff in your code... try to compare.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ekosix View Post
    I recoded your solution:
    Works great.
    You used a too much unnecessary stuff in your code... try to compare.
    Read again... he is doing HOMEWORK and following his assignment's requirements.

    And how exactly do you expect him to learn anything if you just hand out ready made code?

  5. #5
    INSANE INSIDE ekosix's Avatar
    Join Date
    May 2010
    Location
    Rio de Janeiro, Brazil
    Posts
    44

    I'm sorry...

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Read again... he is doing HOMEWORK and following his assignment's requirements.

    And how exactly do you expect him to learn anything if you just hand out ready made code?
    I'm a she. This board assumes all users are male way too much.

    But anyways, CommonTater, thank you for trying to help. I'm not sure I quite understand what you're saying. I tried moving the printf we're talking about outside of the if statement and outside of the for loop but neither work. Is this moving of the printf what you're referring to?

    Again, I appreciate the help.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jacobsh47 View Post
    I'm a she. This board assumes all users are male way too much.
    And about 90% of the time they're right... especially using a handle like Jacob.


    But anyways, CommonTater, thank you for trying to help. I'm not sure I quite understand what you're saying. I tried moving the printf we're talking about outside of the if statement and outside of the for loop but neither work. Is this moving of the printf what you're referring to?
    Ok lets take a look...
    Code:
    for(i = 3; i < sqrt(number); i = i + 2)
       {
           if ((number % i) == 0)
            {
               printf("%d is not a prime number\n", number);
               break;
            }
          else printf("%d is a prime number\n", number);  <--- this is inside the for loop where it can execute multiple times
    }
    A better, and probably faster way would be to use a flag, moving the reporting out of the loop altogether...

    PSEUDOCODE...
    Code:
    int flag initialize to 0.
    for test loop...
       if number mod loop is zero <-- not prime
         set flag to 1
         break out of loop
    loop again
    
    // after exiting the loop
    if the flag is 1
    print "not prime"
    otherwise
    print "is prime"

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    And about 90% of the time they're right... especially using a handle like Jacob.
    It's Jacobs, as in my last name. Or should I change it to make sure everyone knows my sex?

    Just because the majority of people are male doesn't mean you should assume so.

    Thanks, but I'll find help elsewhere. I already deal with people who subscribe to the notion that a programmer has to be a dorky male in real life, I don't need to deal with it here.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jacobsh47 View Post
    It's Jacobs, as in my last name. Or should I change it to make sure everyone knows my sex?

    Just because the majority of people are male doesn't mean you should assume so.

    Thanks, but I'll find help elsewhere. I already deal with people who subscribe to the notion that a programmer has to be a dorky male in real life, I don't need to deal with it here.
    Okey dokey... don't let the door hit yer butt on the way out...

    (Hint: If you had simply worked through the problem without getting all sexist about it, I'd have been glad to help you, as would most everyone else here. What is offputting is that you had to bring gender into a genderless situation. You aren't the only female here but you are the only one making an issue of it. )

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No. Nobody needs to know your sex. Nobody cares. But since you made it such an issue, I don't know why you feel being mistakenly taken for male is such a terrible slight?
    Do you expect everyone to continually use he/she or him/her. You've come here to get programming help. Which you presumably received. Not to start some gender animosity where there was none.

    Hey and not all programmer males are dorky. That's a male bashing remark. Perhaps it's you that needs sensitivity training. I hardly ever wear my pocket protector any more.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    If you had simply apologized for your mistake I wouldn't have had to get all "sexist" about it. You made the situation a gender situation by calling me a male and not caring that you made a mistake.

    As for me being the only female making an issue of this, I've actually seen females on here who have had to correct posters who assumed their gender, and the offender sincerely apologized.

    That would have been the better route to go than defending your actions with stereotypes. That is what made this a gender issue for me.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Quote Originally Posted by nonoob View Post
    No. Nobody needs to know your sex. Nobody cares. But since you made it such an issue, I don't know why you feel being mistakenly taken for male is such a terrible slight?
    Do you expect everyone to continually use he/she or him/her. You've come here to get programming help. Which you presumably received. Not to start some gender animosity where there was none.

    Hey and not all programmer males are dorky. That's a male bashing remark. Perhaps it's you that needs sensitivity training. I hardly ever wear my pocket protector any more.
    I put in that remark about being dorky to emphasize the stereotyping that we don't subscribe to with males. But apparently it's okay for females. As for always writing he/she, see my previous post.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    There's nothing wrong with being referred to by the wrong gender where there are no other clues apparent. Get over it.
    This is the internets where text instead of face-to-face or voice may give a better clue.
    There was no insult indented. Not that there ever is one if male or female were interchanged (again, by mistake no less). Neither gender should ever be implied to be "better".
    I hope no one feels the need to apologize here. You started stereotyping by calling all male programmers dorky males. I think all of us "dorky males" need an apology.

    Maybe this thread needs to be split and moved to another topic area.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    No need to split the thread, I am done reading any more comments on it. Obviously nobody understands what I'm trying to say. Not surprising since "about 90% of you are male."

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jacobsh47 View Post
    No need to split the thread, I am done reading any more comments on it. Obviously nobody understands what I'm trying to say. Not surprising since "about 90% of you are male."
    The plain truth is that 90% or so of programmers --especially C programmers-- are male.
    You use a handle that could very easily be a guy's name ... "Jacob"

    Someone leaps to the obvious conclusion and now you're all bent out of shape.

    You make your own problems, my friend...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another for loop problem
    By Rasher in forum C Programming
    Replies: 7
    Last Post: 10-15-2009, 07:52 AM
  2. Problem with do while loop
    By tru.cutru in forum C Programming
    Replies: 2
    Last Post: 06-29-2008, 10:36 PM
  3. for loop problem
    By Makoy in forum C++ Programming
    Replies: 1
    Last Post: 12-25-2004, 10:08 AM
  4. while loop problem
    By MedicKth in forum C++ Programming
    Replies: 11
    Last Post: 10-02-2003, 09:23 PM
  5. WHILE LOOP problem
    By jrahhali in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2003, 06:42 PM