0

Hello I am new to this community and newbie to c language. I want to code for modulus operator (%) in c.. Need Help.


Thanks in Advance

4
Contributors
8
Replies
10
Views
6 Years
Discussion Span
Last Post by Snehamathur
0

% operator is use to divide number, but it output the remainder of the number .exp:4%3=1. 5%3=2.

0

The mod operator is simply the remainder after division. For example, 4%2 is 0 because the remainder after 4/2 is 0. The mod operator works only on integers, not floats or doubles. There is no code to give you because that's all it is -- a simple 5th grade math operation.

[edit]And what ^^^ said.[/edit]

Edited by Ancient Dragon: n/a

0

i know how (%) op works,but i need to calculate reminder without using it.

0

Then just use normal division (either float or double). After doing the divion you can call modf() to split the variable into integer and fractional parts. Certainly a lot easier to just use % operator.

0

Actually, i was asked this question." How would you calculate reminder of two ints without using modulus operator ?"
and i am so confused about it.

0

just like I said

int a = 40;
int b = 3;
double intpart = 0;
double result = (double)a/b;
double fract = modf(result,&intpart);
int result = (int)fract*10; // convert 0.3333... to just 3

Edited by Ancient Dragon: n/a

2

Try this one also..

# include <stdio.h>

int main(){
    int a=10,b=3,reminder;
    // suppose you want a%b.

     reminder = a -(a/b)*b;
     
     printf("\n\n  Reminder of %d/%d is %d",a,b,reminder);
     return 0;
   }

This will also work like % operator..

Votes + Comments
Appriciated
nice -- a lot better than mine :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.