Computing.Net > Forums > Programming > Help modulud operator

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help modulud operator

Reply to Message Icon

Name: webseo
Date: November 2, 2009 at 23:03:46 Pacific
OS: Windows XP
Product: Hp (hewlett-packard) Compaq presario cq60-420us notebook
Subcategory: C/C++
Comment:

How can 0%3 =3 Please explain this.



Sponsored Link
Ads by Google

Response Number 1
Name: nbrane
Date: November 2, 2009 at 23:45:17 Pacific
Reply:

modulo, if i'm not mistaken, is fancy word for "remainder after integer division". so, 0 div by 3 is zero. the remainder is 3.
modulo is short-handed as %, which can lead to confusion considering windows' use of the percent sign. But your submitted equation is: 0 % 3 and therefore yields 3 as the remainder from the division. any further elucidation will require math courses, not programming. good luc.


1

Response Number 2
Name: webseo
Date: November 3, 2009 at 01:06:20 Pacific
Reply:

Thanks for your reply. I would like to create a C program to reverse the number entered by user. Here's what I have done. Is this the correct logic ?

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
}


0

Response Number 3
Name: Mechanix2Go
Date: November 3, 2009 at 03:36:32 Pacific
Reply:

math class

LOL

Modulo is a fancy name for modulo. And zero mod anything is zero.


=====================================
Helping others achieve escape felicity

M2


0

Response Number 4
Name: klint
Date: November 3, 2009 at 05:23:12 Pacific
Reply:

I always thought 0 % 3 == 0. Who said it == 3 ?


0

Response Number 5
Name: nbrane
Date: November 4, 2009 at 00:12:12 Pacific
Reply:

(red face) apologies. my logic wrong. not enough math\computer classes or too long since. Hoist on mey own petard Oi is. LOL

. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
}

my understanding is: for each digit in number, reflect the 10's complement (ie: 10 minus digit): 183 = 927 674 = 436 etc.
but i'm probl'y wong. (seems to be a trend lately.)
if you are fielding successive digits by division,
you would need to:
C = 0
OUT = 0
do while num>0
'capture first digit in A and strip from number
A=num % 10
num = num - A
'shift the main number 1 digit
num = num / 10

'reverse (10's-complement) the captured digiit
A = 10 - A
'modulo to make 10=0, 9=1,. etc.
A = A % 10
'exponentiate the digit (1's, 10's, 100's)
A = A* ( C^10 )
C=C+1
OUT = OUT + A
loop

sorry about my misinformation about modulo. seems i'm always wrong. take this into acct if using or adapting my code.


1

Related Posts

See More



Response Number 6
Name: webseo
Date: November 4, 2009 at 00:22:18 Pacific
Reply:

I mean numbers entered from 1,but not negative numbers.
It can be 123, so answer should be 321. But when I enter a long number, the answer is not what it should be. Why?


0

Response Number 7
Name: klint
Date: November 4, 2009 at 02:14:41 Pacific
Reply:

The program that you posted tries to deal with two-digit numbers. It doesn't yet do everything it needs to do to create the reversed number. You need to think a little bit more about exactly how you do this. Concentrate on two-digit numbers first, don't complicate things.

Once you have sorted out the two-digit problem, think about how you are going to do it for arbitrarily sized numbers. It will be more complicated, and you will need to start again, but carry over the knowledge and skill you've picked up in your first step.


0

Response Number 8
Name: Mechanix2Go
Date: November 4, 2009 at 12:41:33 Pacific
Reply:

I don't know why reversing a string would require math.

=================================
@echo off & setLocal EnableDELAYedExpansion

if %1'==' echo done && goto :eof
set N=
set S=%1

:loop
set /a N+=1
set v!N!=!S:~0,1!
if "!S:~1!" neq "" (
set S=!S:~1!
goto :loop
)

for /L %%a in (!N! -1 1) do (
set R=!R!!v%%a!
)

echo !R!


=====================================
Helping others achieve escape felicity

M2


0

Response Number 9
Name: klint
Date: November 4, 2009 at 14:40:41 Pacific
Reply:

Er, M2, I thought this was a question about reversing an integer
in C++.


0

Response Number 10
Name: Mechanix2Go
Date: November 4, 2009 at 23:59:01 Pacific
Reply:

Hi klint,

I guess you're right.


=====================================
Helping others achieve escape felicity

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Help modulud operator

Help with C++ Operator www.computing.net/answers/programming/help-with-c-operator/7103.html

Help writing code www.computing.net/answers/programming/help-writing-code/4852.html

Help w/ Simple C Program www.computing.net/answers/programming/help-w-simple-c-program/7850.html