c program that inputs 5 pairs of integers and determine if the second one is multiple of the first one
-
Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko
/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */
-
Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko
/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */
You need to put the
multiple( num1, num2);
call in thefor
loop - it's not in the loop in the code you've posted.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
You need to put the
multiple( num1, num2);
call in thefor
loop - it's not in the loop in the code you've posted.Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko
/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */
IMHO your code has several flaws.
-
you're treating
num1
andnum2
inmain
(as well asj
andz
inmultiple
) like they where array elements, but, in fact, they aren't. -
in the array-like scenario, the
break
onnum1==0
is inappropriate, that 'exceptional' input would be better handled inside themultiple
function (possibly with acontinue
). -
the line
raeiko wrote:
printf( "\nBroke from loop because num1 must be greater than 0\n" );
is wrong: you're simply rejecting
num1==0
, i.e. you code accepts negativenum1
values.
As already suggested by Stuart Dootson, would be probably better abandoning the array-like design: makemultiply
function acting on just two operands and call it inside the input loop. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
-
If i don't ask too much,could you please show me? This thing has been driving me crazy for one week...and i guess this is still nothing in comparison to advanced C...I feel a bit stupid... Thanks again, raeiko
I see what you've done. You've got the loop in main and multiple, but you're passing single integers into multiple. There are two solutions (both built & tested under darwin-gcc-4.0.1):
-
Call
multiple
in the loop and remove the loop inmultiple
:/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */
#include <stdio.h>
int multiple ( int j, int z); /* function prototype */
int main( void ) /* function main begins program execution */
{
int num1, num2, x, i; /* declare variables */for ( x = 1; x <= 5; x++ )
{
printf( "Enter the first number:" ); /* prompt for input */
scanf("%d", &num1 ); /* read number from user */if ( num1 != 0) { printf( "Enter the second number:" ); /\* prompt for input \*/ scanf("%d", &num2 ); /\* read number from user \*/ } else { break; printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/ } multiple( num1, num2);
}
return 0;
}int multiple ( int j, int z ) /* copy of the argment to function */
{
int result;result = z % j;
if( result == 0)
{
printf( "%d is multiple of %d\n", z, j );
}
else
{
printf( "%d is not multiple of %d\n", z, j );
}
return result;
} /* end of multiple function */ -
Gather the numbers into two arrays and process them in one call to
multiple
:/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */
#include <stdio.h>
int multiple ( int* j, int* z, int n); /* function prototype */
int main( void ) /* function main begins program execution */
{
int num1[5] = {0};
int num2[5] = {0};
int x, i; /* declare variables */for ( x = 1; x <= 5; x++ )
{
printf( "Enter the first number:" ); /* prompt for input */
scanf("%d", &(num1[x-1]) ); /* read number from user */if ( num1\[x-1\] != 0) { printf( "Enter the second number:" ); /\* prompt for input \*/ scanf("%d", &(num2\[x-1\]) ); /\* read number from user \*/ } else { break; printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/ }
}
multiple( num1, num2, x-1);
return 0;
}int multip
-
-
I see what you've done. You've got the loop in main and multiple, but you're passing single integers into multiple. There are two solutions (both built & tested under darwin-gcc-4.0.1):
-
Call
multiple
in the loop and remove the loop inmultiple
:/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */
#include <stdio.h>
int multiple ( int j, int z); /* function prototype */
int main( void ) /* function main begins program execution */
{
int num1, num2, x, i; /* declare variables */for ( x = 1; x <= 5; x++ )
{
printf( "Enter the first number:" ); /* prompt for input */
scanf("%d", &num1 ); /* read number from user */if ( num1 != 0) { printf( "Enter the second number:" ); /\* prompt for input \*/ scanf("%d", &num2 ); /\* read number from user \*/ } else { break; printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/ } multiple( num1, num2);
}
return 0;
}int multiple ( int j, int z ) /* copy of the argment to function */
{
int result;result = z % j;
if( result == 0)
{
printf( "%d is multiple of %d\n", z, j );
}
else
{
printf( "%d is not multiple of %d\n", z, j );
}
return result;
} /* end of multiple function */ -
Gather the numbers into two arrays and process them in one call to
multiple
:/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */
#include <stdio.h>
int multiple ( int* j, int* z, int n); /* function prototype */
int main( void ) /* function main begins program execution */
{
int num1[5] = {0};
int num2[5] = {0};
int x, i; /* declare variables */for ( x = 1; x <= 5; x++ )
{
printf( "Enter the first number:" ); /* prompt for input */
scanf("%d", &(num1[x-1]) ); /* read number from user */if ( num1\[x-1\] != 0) { printf( "Enter the second number:" ); /\* prompt for input \*/ scanf("%d", &(num2\[x-1\]) ); /\* read number from user \*/ } else { break; printf( "\\nBroke from loop because num1 must be greater than 0\\n" ); /\* break loop if num1 = = 0 \*/ }
}
multiple( num1, num2, x-1);
return 0;
}int multip
-
-
Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko
/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */
Check out your
multiple()
function. Notice that it is called outside of the loop, so it is only going to work on the last two values ofnum1
andnum2
. Also, why does themultiple()
function have afor
loop?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons