Illidari's Newb Question #1

This is a discussion on Illidari's Newb Question #1 within the C Programming forums, part of the General Programming Boards category; Okay I am trying to teach myself C programming I am sure I will have many questions lol :P So ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    11

    Illidari's Newb Question #1

    Okay I am trying to teach myself C programming

    I am sure I will have many questions lol :P


    So I am using notes and an example:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void) {
    char ch; 
    int x,y; 
    
    ch = 'j';
    x = 6*2;
    y = 3*x - 50% x;
    
    printf("y = %d\n", y);
    
    	system("Pause");
    	return 0;
    }

    Why does this print y= 34 at 50%, 26 at 10% and 28 at 20%

    I can't figure out the pattern. What does that percent sign do to the equation. Currently set to 50% in the code.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Percent is the modulo operator. It returns the value of the of the remainder of dividing the first number by the latter.

    e.g. 10%3 = 1

    because 10 divided by 3 is quotient 3, remainder 1.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    In your case y = 3*12 + 50%12 which is 36 + 2 = 38.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Thank you good sir ^_^ That was quick.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    My pleasure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-23-2008, 12:37 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  3. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 09:04 AM
  4. newb question
    By Jan79 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2003, 10:59 AM
  5. Win app, newb question
    By Blender in forum Windows Programming
    Replies: 9
    Last Post: 02-04-2003, 12:17 PM

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