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 OverflowThe 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!
Videos
In what cases do we get an error from the compiler when using the modulo operator?
How does the % operator work?
I started learning C++ a few days ago. I've learned the basics for now. +,-.*,/ . But I can't properly understand the modulus operator. I know that if we take modulus of a number by 10, it will give us the last digit of that number. For example:
int num=12345; num%10; ----> 5
Now the problem is I can't understand this code. The question is:
A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input through the keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give to the withdrawer. i.e A user input 480 then the answer will be 4 notes of Rs. 100, one note of Rs. 50 and 3 notes of Rs. 10.
and the code is:
#include<stdio.h>
#include<conio.h>
void main(void){
int amount,t,f,h,c=0;
clrscr();
printf("Enter total amount:");
scanf("%d",&amount);
h=amount/100;
**f=(amount%100)/50; //line A
t=((amount%100)%50)/10; //line B
c=(((amount%100)%50)%10);** //line C
printf("\n\n100 Rs notes:%d",h);
printf("\n50 Rs notes:%d",f);
printf("\n10 Rs notes:%d",t);
printf("\nCoins remaining:%d",c);
getch();
}can someone explain why we did what we did in lines A,B,C?
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