Division in C programming. plz help

Hi guys . i was working on given below question. and i try to solve it. can any one confirm it that is this correct solution for that question? if not then can u tell me whats the right code. Fix it if its not correct and also tell me reason thx
Much appreciate that


[b]Exercise: [/b]
[b]Create a MikroC program to read in an 8-bit unsigned number on PORTB. If the number is even AND divisible by 3, PORTC.F0 = 1. Otherwise PORTC.F0 = 0[/b].

[b]Solution[/b]

void main(){
ADCON1=7; TRISB=0b11111111; TRISC=0;
while (1)
{
else if (PORTB%3==0){PORTC.F0=1;}
else {PORTC.F0=0;}
}
}

Comments

  • [color=Blue]1st part is correct, but you did not check the 2nd part of condition. When value is even - last bit is always zero, so a simple code will check for that:[/color]
    [code]
    if ((value%3 == 0) && (value&1 == 0))
    {
    [color=Green]// value is both even and divisible by 3[/color]
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion