Thread: Calculating modulus

  1. #1
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38

    Calculating modulus

    Hi!

    I am working on a project and I am not taking modulus! The sign is "%" but it doesn t work! Take a look this,please!

    if( (min+max)<21)


    printf( "The modula is : %d\n",min+max);
    if ((min+max)>=21)
    printf("The modula is : %d % 21",min+max);

    Of course it is not the whole code.I am trying to take modula21, but where is wrong???The whole code is
    Code:
    #include<stdio.h> 
    main() 
    { 
    int n,m,i,min,max; 
    
    
    printf("Please enter a positive integer:"); 
    scanf("%d",&n); 
    printf("Enter integers:"); 
    scanf("%d",&m); 
    max=m;
    min=m; 
    for(i=2;i<=n;i++) 
    { 
    scanf("%d",&m); 
    if(m<min) 
    min=m; 
    if(m>max)
    max=m;
    } 
    printf("The smallest number is %d\nThe greatest number is %d\n",min,max);
    printf("The sum is : %d\n",min+max);
    if( (min+max)<21)
    
    
    printf( "The modula is : %d\n",min+max);
    if ((min+max)>=21)
    printf("The modula is : %d % 6",min+max);
    
    
    }

  2. #2
    spaghetticode
    Guest
    printf() knows nothing about arithmetic. Any % inside it is just the sign that a placeholder for an argument will follow.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    printf("The modula is : %d % 6",min+max)
    Maybe you mean (min+max) % 6 outside of the string.

    The percent sign has a special meaning in printf, it is called a conversion specifier and it marks where printf has to convert data to text.

  4. #4
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    printf("The modula is : (min+max) % 21",min+max);

    I wrote this still not working! " The modula is <min+max> is written! And I delete printf, but this time nothing is written???? I use scanf , still nothing!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    To get printf() to print a %, you write something like

    printf("The modula is : %d %% 21",min+max);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    This time it is "The modula is : number % 21" . It is better. But still it can t divide by 21. The operation can not work. Why?? WhYYYYYY?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're still not using % outside of the string, like how you use + outside of the string.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    Thanks Imalc . You were right. It works !

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You're welcome.

  10. #10
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    @ whiteflags I said "thanks" million times But sure you can not hear. I am sorry forgetting to write. But again thank you all.

  11. #11
    spaghetticode
    Guest
    I still get the impression you get the concept of printf() and scanf() wrong. Revise this. What book are you working with?

  12. #12
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    I have a deitel. I know that a scanf allow you to enter an element from keyboard and printf write. Am I wrong??

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... programming lesson...

    Learn how to look things up for yourself.

    If you don't have the library documentation for your compiler ... get it ... and ... use it.

    It's very normal for working programmers to "look stuff up" dozens, even hundreds of times a day.

    (but yes, your assumption is correct)

  14. #14
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    Thanks Commontater

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. %(Modulus)
    By cuo741 in forum C Programming
    Replies: 19
    Last Post: 05-14-2010, 08:50 AM
  2. Modulus division
    By WatchTower in forum C Programming
    Replies: 4
    Last Post: 07-20-2009, 11:26 PM
  3. modulus
    By nicknack in forum C Programming
    Replies: 7
    Last Post: 12-15-2007, 09:45 AM
  4. Using the Modulus Operator
    By tlove71 in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2007, 11:59 AM
  5. Help with Modulus
    By boontune in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 10:26 AM