Questions tagged [modulus]

The remainder of the quotient of two numbers (usually integers).

847 questions
-3
votes
3 answers

How to individually select < li > and then use modulus to select the odd ones

ALright so I got a homework from my teacher to first select every individual < li > and then use modulus to select only the odd ones and change their color. And I have to say that I am completely stumped. I have tried selecting using child…
-3
votes
2 answers

How to use modulus with a global variable and namespace std?

Yes I know using namespace std is bad practice, but I have already written a majority of the code with this declaration in place, and I don't think I have the time to go back and modify it. The reason for the global variable is that I'm using…
-3
votes
3 answers

Using modulus operator in for Loop with if statements - Java beginner

I hope someone can help. My problem is with using the modulus operator in a for loop. My code is as follows: for (int i = 0; i < 10; i++) if (i % 2 == 0) { method1(); } else { method2(); } I understand how this loop works in that it iterates…
Mark Fenwick
  • 147
  • 1
  • 2
  • 10
-3
votes
1 answer

Check if incremented by 10 c++

I need to check of a number is incremented by 10 (70, 20, 110) example: input: (74, 21, 105) output: false input: (70, 20, 110) output: true I have tried if(num % 10 == 1)
-3
votes
1 answer

Number 2 recognized as prime - explaining 2 modulus

Can someone please explain why number 2 is also added to the list of prime numbers in the code below? As the function below should only recognize a number as prime when there is at least 1 number with modulus not equal to 0. As 2 % 0 = 0 2 % 1 =…
s3icc0
  • 41
  • 1
  • 7
-3
votes
2 answers

Can someone explain how reversing an integer using % 10 works?

To reverse an integer and put it into a list, one would do the following (where x is some integer): int lastDigit = x; while(lastDigit != 0) { list.add(lastDigit % 10); lastDigit /= 10; } So if x was 502, 2 0 and 5…
Ryan Williams
  • 119
  • 2
  • 8
-3
votes
2 answers

Go over all multiples of 3 between 0 and 100 (inclusive), and print the ones that are divisible by 2

What I have so far: num = range(0, 101, 3) list = [] if num % 3 == 0: list.append print(list)
pythonnewbie
  • 1
  • 1
  • 4
-3
votes
1 answer

Weird behavior when dividing hex numbers

I'm struggling with a really weird situation that I can't explain. Basically I don't understand why results of modulus or division between two hex numbers are wrong (I'm using C). I have a variable time which is a 32bit unsigned int and it's…
matteodv
  • 3,992
  • 5
  • 39
  • 73
-3
votes
5 answers

How to echo every after 9th iteration of a loop? First echo only counted 8 items

My code is below: function portfolio_gallery() { global $conn; $query = $conn->query("SELECT codename, namegroup, features, title, showimage FROM portfolio ORDER BY id DESC"); if ($query->num_rows > 0) { // output data of…
hello
  • 351
  • 3
  • 4
  • 16
-3
votes
2 answers

In javascript, can I use the modulus operator (%) on variables instead of integers?

I'm working on a Euler problem and am trying to build a function that checks a number to see if its a prime. I get error messages about the line: if (a)%(b)==0{ Is my syntax wrong or is it impossible to use % on a variable rather on an…
jasonp49
  • 1
  • 1
-3
votes
2 answers

b%a where b is very large

We are given two integers a and b, a <= 100000, b < 10^250. I want to calculate b%a. I found this algorithm but can't figure out how it works. int mod(int a, char b[]) { int r = 0; int i; for(i=0;b[i];++i) { r=10*r +(b[i] -…
daft300punk
  • 169
  • 3
  • 16
-3
votes
2 answers

Java Not greater than certain value

I just have a simple question. In Java, we know that the remainder of (y%x) can't be greater than x itself. Thus, we could hypothetically set all numbers less than a certain value of x, say 100. Yet what would be the opposite of this? What if we…
kris
  • 375
  • 2
  • 12
-3
votes
6 answers

JavaScript for-loop: Skip 2 Numbers Followed by a set of 3

I made a JavaScript loop that skips every set of two numbers that are followed by a set of three. Consider the following set of integers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 This is what the array returns: 1, 2, 3, 6, 7, 8 See how it skipped 4-5 and 9-10?…
Matthew
  • 2,158
  • 7
  • 30
  • 52
-3
votes
2 answers

Java modulus operator

Use a modulus operator is something which all programmers must to know. I know it =). In java we have : int a = 100 , b = 50, c; If we do : c = a % b; // c = 0 because : 100 = 50*2 + 0 | D = d*q + r using simple maths However I felt a little…
afym
  • 532
  • 2
  • 6
  • 22
-3
votes
1 answer

How to calculate modulus?

I have a task to create a mathematical formula in c++, input some variables and get a result. The problem is that I don't know how to represent and calculate the following: sin(3 mod 180) I`m aware that mod operator finds the remainder of a…
Vallerious
  • 570
  • 6
  • 13