Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a program that, beginning with the integer 1, finds the integers that are:
(a) evenly divisible by 3 or 4
(b) evenly divisible by 5 or 6
For each, print the integer and which of the two conditions apply (it could be both). The
program should keep going until you have found five integers that are evenly divisible by 5 or
6.
Sample Output:
3 is evenly divisible by 3 or 4
4 is evenly divisible by 3 or 4
5 is evenly divisible by 5 or 6
6 is evenly divisible by 3 or 4 and 5 or 6
8 is evenly divisible by 3 or 4
9 is evenly divisible by 3 or 4
10 is evenly divisible by 5 or 6
12 is evenly divisible by 3 or 4 and 5 or 6
15 is evenly divisible by 3 or 4 and 5 or 6

This is the code i have so far:

C#
#include <stdio.h>
int main(void)
{
    int i=1;
    int b=1;
    while (i<=5)
    {
        if(b%3 == 0 || b%4==0)
        {
            if(b%5==0||b%6==0)
            {
                printf("%d is evenly divisible by 3 or 4 and 5 or 6\n", b);
            }
        else 
        {
            printf("%d is evenly divisible by 3 or 4\n", b);
            }
        }
        if(b%5 == 0 || b%6==0)
        {
            i++;
            printf("%d is evenly divisible by 5 or 6\n", b);
        }
        
        b++;
            
    }
}


Sorry wrong CODE
im close to getting the right solution but it prints too many 5,6 or 3,4 please tell me whats wrong with my code.
Posted
Updated 19-Feb-10 7:12am
v3

1 solution

Firstly the line
if(num%3==0 && num%3,4==0 && num%3,4,5==0)

is wrong. The Expression num%3,4==0 will always yield 0 as it will evaluate num%3 then 4==0 and returns the value of the second expression which is always 0 since 4==0 is never true. Similarly the last set in the above line, and elsewhere. You need to find an alternative way of testing for these values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900