Thread: I cant figure the problem out!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    I cant figure the problem out!

    Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.

    I dont know what I need to do to fix this!

    Code:
    #include <stdio.h>
    
    int multiple(int x, int y, int a);
    
    int main() {
    
    	int x;
    	int y;
    	int m;
    	int a;
    
    
    	printf("Enter first integer here:");
    	scanf("%d", &x);
    
    	printf("Enter second integer here:");
    	scanf("%d", &y);
    
    	m = multiple(x,y,a);
    
    	printf("%d is a multiple of the %d\n", m, m);
    
    	return 0;
    
    
    }
    
     int multiple(int x, int y, int a) {
    	
    	 a = x % y;
    
    	 if(a == 0){
    		printf("%d is a multiple of the %d\n", y, x);
    		return 1;
    	else(a != 0);
    		printf("%d is not a multiple of the %d\n", y, x);
    		return 0;
    	}
    	    
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    you're else statement is incorrect
    Code:
     int multiple(int x, int y, int a) {
    	
    	 a = x % y;
    
    	 if(a == 0){
    	 	printf("%d is a multiple of the %d\n", y, x);
    	 	return 1;
    	 }else{
    	 	printf("%d is not a multiple of the %d\n", y, x);
    	 	return 0;
    	 }
    	    
    }
    you should initialize a inside the subroutine instead of passing it down btw
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jturner38 View Post
    Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.

    I dont know what I need to do to fix this!

    Code:
    #include <stdio.h>
    
    int multiple(int x, int y);
    
    int main() {
    
    	int x;
    	int y;
    
    	printf("Enter the larger integer:");
    	scanf("%d", &x);
    
    	printf("Enter smaller integer here:");
    	scanf("%d", &y);
    
    	if(multiple(x,y))
              printf("%d is not a multiple of %d\n", x, y);
           else
              printf("%d is a multiple of %d\n", x, y);
           
    	return 0;
    }
    
     int multiple(int x, int y) {
    
          return (x % y);
    	    
    }

    One problem was the semi-colon at the end of your
    if (statement) {
    //line of code here
    //line of code here
    } //where the curly brace for the if lines of code should go
    else;
    //this code never executes because of the semi-colon which
    //ended the line of code for the compiler.

    }//where the curly brace ONLY for the else lines of code, should go.

    You misplaced the curly brace from before the else statement, to after it.

    The above code may have it's own errors, as I haven't run it, but it will be very close.
    Last edited by Adak; 03-23-2009 at 08:03 PM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Yea it did have errors! I set multiple(x,y) to variable m. Then had if m is not equal to 0 then print it is not a multiple. Its not printing when numbers are multiples.

    Code:
    #include <stdio.h>
    
    int multiple(int x, int y);
    
    int main() {
    
    	int x;
    	int y;
    	int m;
    
    	printf("Enter smaller integer here:");
    	scanf("%d", &x);
    
    	printf("Enter larger integer here:");
    	scanf("%d", &y);
    
    	m = multiple(x,y);
    
    	if(m == 0)
    		printf("%d is a multiple of %d\n", x, y);
    	else
    		printf("%d is not a multiple of %d\n", x, y);
    	
    
    	return 0;
    }
    
     int multiple(int x, int y) {
    	
    	 return (x % y);
     }

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    You want to find if the second number is a multiple of the first number....then you have to check your multiple(int x, int y) function on your return statement. It's backward. That's not the only thing that is backwards, look at the variables on your printf statements within your if/else statements.
    Last edited by peeweearies; 03-24-2009 at 08:53 PM.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Replace else ---> else if

    Quote Originally Posted by peeweearies View Post
    You want to find if the second number is a multiple of the first number....then you have to check your multiple(int x, int y) function on your return statement. It's backward. That's not the only thing that is backwards, look at the variables on your printf statements within your if/else statements.
    There are couple of mistakes in your code:-

    1. Don't use condition for both if and else
    if you want check multiple condition better to use if and else if combination.

    2. you trying to display the result in both main and subfunction.
    i don't see any usage of such kind of code.
    better you can display result either in main or subfunction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM