-1

I'm trying to write a program that takes numbers from user (a and b) then the program will count up from 1 to x (which is a) and output whether the number in each iteration is divisible by y (which is b) or not.

    //while loop deciding if a is divided by b
    for (count = 1; count <= a; count++) {
        if (a / b == 0) {
            printf("%d is divisible by %d\n", a, b);
        } 
        else if (a / b != 0) {
            printf("%d is not divisible by %d\n", a, b);
        }
    }
   return 0 ;
} 

but when i enter 10 for a and 2 for b the output is

10 is not divisible by 2

ten times

how can i change the code so that each iteration is checked?

JDK
  • 35
  • 1
  • 1
  • 6
  • Why would you expect a different result if you use the same `a` and `b` values for each iteration? Can you figure out which variable is the one that is actually changing on each iteration? – kaylum Jun 16 '17 at 04:58
  • 3
    The title of question is misleading – kuro Jun 16 '17 at 05:01
  • 1
    First of all `a%b == 0` means a is divisible by b. You want to fix that. Also can you point out the need for the loop. You want same message to be printed total a time? Lastly `if` and `else if` conditions are exact opposite. You don't need `else if`. Simple `else` will do – kuro Jun 16 '17 at 05:02
  • Check here http://ideone.com/PkxgK0 – roottraveller Jun 16 '17 at 05:03
  • 1
    Do not vandalize your question by deleting the code. – John Kugelman Jun 17 '17 at 00:53
  • Possible duplicate of [How do you check whether a number is divisible by another number (Python)?](https://stackoverflow.com/questions/8002217/how-do-you-check-whether-a-number-is-divisible-by-another-number-python) – phuclv Jun 17 '17 at 01:19
  • did you ever go to google and search for "C check divisibility"? – phuclv Jun 17 '17 at 01:21

4 Answers4

3

First - a / b tells how many times a can be divided by b, e.g. 9 / 2 will give you 4.

To know whether the division produces a reminder, you must use a % b == 0. Example: 9 % 2 will give you 1 while 8 % 2 will give you 0.

Next - You keep using a and b for the calculation inside the loop. You need to use count instead of a. This applies to the printfas well.

And - You don't need a condition on the else part.

Try something like:

for (count = 1; count <= a; count++) {
    if (count % b == 0) {
        printf("%d is divisible by %d\n", count, b);
    } 
    else {
        printf("%d is not divisible by %d\n", count, b);
    }
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1
#include <stdio.h>
int main()
{
    int a;
    printf("Enter a 2-digit number: ");
    scanf("%d",&a);
    for (int i = 0; i < a; i++)
    {
        if (i%2 == 0)
        {
            printf("\n%d is even.", i);
        }
        else if (i%2 != 0)
        {
            printf("\n%d is odd.",i);
        }
        
        
    }
    
    return 0;
}

This is the code for checking whether the number is even or odd.

0

Your program has two logical errors -

  • You are running a for loop to go from 1 to a, but are not using the loop variable count anywhere inside the loop.
  • a/b == 0 cannot be used to check if b divides a, instead use a%b == 0. % is modulus operator which returns the remainder when a is divided by b.
    Correct Code -

    for (count = 1; count <= a; count++) {
       if (count%b == 0) {
          printf("%d is divisible by %d\n", count, b);
       } 
       else {
           printf("%d is not divisible by %d\n", count, b);
       }
    }
    
GAURANG VYAS
  • 689
  • 5
  • 16
-1

every iteration you are using same values, then you can use count variable and at last print value of variable. you can write like this,

for (count = 1; count <= a; count++)
    {
            if (count % b == 0)
            {
                    printf("%d is divisible by %d\n", a, b);
                    n += 1;
            }
    }
    printf("count : %d\n",n);

it will show the count.

suraj
  • 403
  • 4
  • 15