need help with % operator

This is a discussion on need help with % operator within the C Programming forums, part of the General Programming Boards category; My homework was to write a program that reads two integers and determines and prints if the first is a ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    need help with % operator

    My homework was to write a program that reads two integers and determines and prints if the first is a multiple of the second. hint use the reminder operator.

    here is my code that compiles with no errors but does not work when you run exe

    Code:
    #include <stdio.h>
    
    //Begins Main
    int main()
    {
    	int odd ;
    	int even;
    	int num1;
    	int num2;
    
    	printf( "Please enter two digits for multiplication evaluation:\n" );
        scanf ( "%d%d", &num1, &num2 );
    
    	odd = num1 % num2;
    	even = num1 / num2;
    
    
    	if ( num1 && num2 == even){
    
    		printf ("%d is a mulipule of %d\n", num1, num2);
    	}
    
    	   
    		else if ( num1 && num2 == odd){
    		printf ("%d is not a mulipule of %d\n", num1, num2);
    		}
    	  
    
    	
    
    	return 0;
    
    }  //ends main
    any ideas???

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hint:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int den = 3, num;
       for ( num = 0; num < 8; ++num )
       {
          printf("%d %% %d = %d\n", num, den, num % den);
       }
       return 0;
    }
    
    /* my output
    0 % 3 = 0
    1 % 3 = 1
    2 % 3 = 2
    3 % 3 = 0
    4 % 3 = 1
    5 % 3 = 2
    6 % 3 = 0
    7 % 3 = 1
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you should only need one variable to hold the % test
    example:
    Code:
    int remainder;
    
    remainder = num1 % num2;
    you then just have to test if (remainder == 0)
    to find out if num1 was a multiple of num2

    you should also check to make sure the user doesn't enter 0 for num2
    or you have a division by 0

    --little late looks like

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    thanks sl4nted your code correction worked perfect

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you be paying attention to which number is bigger? You don't mention it in your rules of entering numbers, but you are assuming that the first number entered is the larger of the two when you compare them. Also, you don't need any variables for this other than the two you're filling initially.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Well quazah you could write a SWAP function and use a simple if statement comparing the sizes of the X and Y. Persudo Code would be like.
    If X < y then send X,Y to swap. Swap then returns the new updates value of X and the value of Y.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The point wasn't that I couldn't figure out a way around it. It was that they weren't providing the correct prompt, or handling of the situation. The point was that you need to either tell them the exact way to input the data, or you have to account for the fact that you haven't told them the perfect way to do it.

    In short, pretend your end users are completely stupid, and will if at all possible, do everything wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Sorry bro I was not saying that "you" where not able to do that (I have read your other posts and code) but I feel that I am not a power programmer that when I say something like thats its with the intent that I could 90% of the time be wrong, so instead of making my own post and waste someones time. Lol I dont understand the point of jumping around the bush, lol I will try not to blurt out the answer. After all it was not code I didnt pull a Salem showing my godly power I just posted my idea of what to do. There would be tagentable learning in simply proto'ing out the function and the math to swap X, Y. So to a degree I gave the answer but hell it sucks to be left in the dark lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21