Thread: Modulus Program

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    Modulus Program

    Write a C program that reads in two numbers, calculates the modulus of the two and prints out the result. You must use at least three functions (plus main) in your program, however, you must not use return values.

    This is the question I have to answer and this is my code so far


    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
      float num1, num2, rem;
    
      printf("First Number?\n");
      scanf("%f%",&num1);
      printf("Second Number?\n");
      scanf("%f",&num2);
    
      rem = fmod(num1,num2);
    
      printf("%.2f divided by %.2f is remainder %.2f\n", num1, num2, rem);
    
    
      getchar();
      return 0;
    }


    But I have a few problems with it at the moment...

    1. I start the program and the message 'First Number?' comes up and I enter a number (lets say 10) but then message 'Second Number?' never appears! If you continue to enter a number (lets say 8), it ends up looking like this

    First Number?
    10
    8
    Second Number?
    10 divided by 8 is remainder 2


    2. At the moment, the program isn't in modular form but how can I use 3 functions for such a small program?


    Thanks for any help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    scanf("%f%",&num1);
    Need that %?

    And any particular statement can be made a function, if necessary....

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    dude there is something operator called in c++ which gives u the remainder

    %

    int a = 10
    int b = 8;

    int c = a % b;

    check this out

    Code:
    #include <stdio.h>
    
    int main() {
      int a = 10;
      int b = 8;
      int c = a % b;
      printf("a == %d || b == %d || c == %d\n", a, b, c);
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    @ Tabstop - lol thanks, I probably would have never noticed that

    @ Rocky - I've seen a lot of people using that actually but it just confuses me :/



    I will work on my code a little longer and try turn it into 3 functions

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    what is the confusion dude just googlie the modulo operator C++

    as like / divide operator it returns the quotient as % this operator returns the remainder

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    would that mean I could just use

    "rem = num1 % num2;"

    instead?

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    yes brother and before posting this i think u would have been done it there

    why u r not tryin this there on ur machine

    u will get to knw

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    eye tink he jus want anzers as fast as possibly
    Last edited by whiteflags; 10-13-2009 at 11:51 PM. Reason: for grammar and spelling fixes

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    lol I did try it on my compiler but it came up as

    Invalid Operands to Binary %

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    what u r using MS VC++ or gcc ??? and can u just run the code which i typed above

    Code:
    #include <stdio.h>
    
    int main() {
      int a = 10;
      int b = 8;
      int c = a % b;
      printf("a == %d || b == %d || c == %d\n", a, b, c);
      return 0;
    }

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Just so you know, the modulus operator does not work with floating point. The op had it right in the beginning. You're supposed to use fmod for floating point.

    And aren't you using three functions as it is, OP? printf, scanf, and fmod. That's more than enough when you include main. Otherwise, you can always further divide the program into steps. Think of what you need to do: read a number, calculate, print the answer....

  12. #12
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by whiteflags View Post
    eye tink he jus want anzers as fast as possibly
    Nice, lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM