Basic if else question

This is a discussion on Basic if else question within the C Programming forums, part of the General Programming Boards category; I have to create a program that takes 2 numbers. If the first number is equally divisible by the second ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Basic if else question

    I have to create a program that takes 2 numbers. If the first number is equally divisible by the second number, then print the number of times it's divisible, otherwise, print a message saying "not divisible". I have no clue how to write an if statement to find out if the first number is equally divisible by the second number.

    My code is below - please explain.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x, y, z;
        
        
        printf("Enter in 2 numbers: \n");
        scanf("%d%d", &x, &y);
        
        if 
           {x % y ==z} 
           {
                 printf("%d\n", z);
           }
                 else
                 {
                     printf("not divisible\n");
                 }
        } 
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by benrogers View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
        int x, y, z;
        
        
        printf("Enter in 2 numbers: \n");
        scanf("%d%d", &x, &y);
        
        if 
           {x % y ==z} 
           {
                 printf("%d\n", z);
           }
                 else
                 {
                     printf("not divisible\n");
                 }
        } 
        
        return 0;
    }
    bold does the trick.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x, y, z;
        
        printf("Enter in 2 numbers: \n");
        scanf("%d%d", &x, &y);
      
        z = x / y;
      
        if (x % y == 0)
         { 
              printf("%d\n",z); 
              }
         else
         { 
             printf("not divisible\n");
        } 
        
        return 0;
    }

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Code:
    while (!(num1 % num2))
    {
         count++ ;
          num1/=2 ;
    }
    i think that would work
    You ended that sentence with a preposition...Bastard!

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    1. You didn't assign z to anything.
    2. The expression in the if statement should be in parenthesis, not braces.
    3. For future reference, you should write code that catches division by zero, like this.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x, y, z;
        
        printf("Enter in 2 numbers: \n");
        scanf("%d%d", &x, &y);
      
        if (y != 0) z = x / y;
        else {
            printf("division by zero\n");
            return 1;
        }
        if (x % y == 0)
         { 
              printf("%d\n",z); 
              }
         else
         { 
             printf("not divisible\n");
        } 
        
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 03:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 03:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 04:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21