The modulo operator in C will give the remainder that is left over when one number is divided by another. For example, 23 % 4 will result in 3 since 23 is not evenly divisible by 4, and a remainder of 3 is left over.
If you want to output whether or not a number is divisible by 4, you need to output something other than just the mod result. Essentially, if mod = 0 than you know that one number is divisible by another.
If you want to output whether or not the number is divisible by 4, I would suggest creating a new character that is set to "y" (yes) or "n" (no) depending on the result of the mod operation. Below is one possible implementation to generate a more meaningful output:
#include <stdio.h>
#include <ctype.h>
#include <math.h>
int main()
{
int my_input[] = {23, 22, 21, 20, 19, 18};
int n, mod;
char is_divisible;
int nbr_items = sizeof(my_input) / sizeof(my_input[0]);
for (n = 0; n < nbr_items; n++)
{
mod = my_input[n] % 4;
is_divisible = (mod == 0) ? 'y' : 'n';
printf("%d modulo %d --> %c\n", my_input[n], 4, is_divisible);
}
}
This will give the following:
23 modulo 4 --> n
22 modulo 4 --> n
21 modulo 4 --> n
20 modulo 4 --> y
19 modulo 4 --> n
18 modulo 4 --> n
Answer from rakeshb on Stack OverflowVideos
In what cases do we get an error from the compiler when using the modulo operator?
How does the % operator work?
The modulo operator in C will give the remainder that is left over when one number is divided by another. For example, 23 % 4 will result in 3 since 23 is not evenly divisible by 4, and a remainder of 3 is left over.
If you want to output whether or not a number is divisible by 4, you need to output something other than just the mod result. Essentially, if mod = 0 than you know that one number is divisible by another.
If you want to output whether or not the number is divisible by 4, I would suggest creating a new character that is set to "y" (yes) or "n" (no) depending on the result of the mod operation. Below is one possible implementation to generate a more meaningful output:
#include <stdio.h>
#include <ctype.h>
#include <math.h>
int main()
{
int my_input[] = {23, 22, 21, 20, 19, 18};
int n, mod;
char is_divisible;
int nbr_items = sizeof(my_input) / sizeof(my_input[0]);
for (n = 0; n < nbr_items; n++)
{
mod = my_input[n] % 4;
is_divisible = (mod == 0) ? 'y' : 'n';
printf("%d modulo %d --> %c\n", my_input[n], 4, is_divisible);
}
}
This will give the following:
23 modulo 4 --> n
22 modulo 4 --> n
21 modulo 4 --> n
20 modulo 4 --> y
19 modulo 4 --> n
18 modulo 4 --> n
I'm sure we know the basic division equation from high school math
dividend = divisor*quotient + remainder
Now:
1. The "/" operator gives us the quotient.
2. The "%" operator gives us the remainder
example:
say a = 23, b = 4
a / b = 23 / 4 = 5
a % b = 23 % 4 = 3
23 = 4*5 + 3
Here 4 is the quotient and 3 is the remainder.
If a number is perfectly divisible by a divisor, then remainder is zero.
So:
20/4 = 5 (quotient)
20%4 = 0 (remainder)
To test if a no if divisible by 4, the check should be something like if (num % 4 == 0).
Hope this helps!
Hello, so I need to print out a statement with the data type being a double, however I need to use the modulus operator but I can't because it only takes integer division.
Situation for context I have $511.72 as the balance and I am calculating the amount in coins and remaining balance.
int balance = 500;
printf("Toonies:%d %lf", balance / 2, balance % (balance / 2);
printf("Loonies:%d %lf", (balance % (balance / 2) / 1, (balance % (balance / 2) % 1)
So I'm really struggling with the credit problem in pset1. I'm completely new to coding and computer science and everything but I really want to try doing all of the problems in this class. I had to look up mario and cash to gain some insight into what I've been doing wrong when I approach a problem. With each completed problem the pieces come together in my head a little more than they were before.
Credit has me totally through a loop. It seems like for all of the problems I understand what to use in C as far as loops and what have you but I really struggle with figuring out what like operations I'm supposed to use to tell the program exactly what to do. With the whole modulo operation thing I don't really understand what it's doing and what I have to do to get it to give me the numbers I need. Something tells me I need to use different powers of ten to somehow separate the full credit card number into individual digits but because I don't understand what % 10 is actually doing I can't fully figure out what I need to do.
I'm struggling with trying to decide if I should just skip this problem and try again another time or if I should contemplate it more. With each problem it seems like I struggle with like some kind of mathematical aspect, I feel like there's something that I'm missing but I can't tell if I just how to make myself think about things differently. I feel like I'm making myself crazy about this lol