determine if 1 int is multiple of another - series of pairs - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# determine if 1 int is multiple of another - series of pairs

Posts: 17Member
Hello guys,

i have just compiled a C program that enters 2 numbers and established if the second one is multiple of the fiirst one.

I have 2 doubts:

1) the exercise ask to write a "function multiple that determines for a pair of integers whether the second integer is a multiple of the first".

2) i have to test it in a "program that inputs a series of pairs of integers"

I think that what i have done doesn't answer these 2 requirements.

Could you please have a look and correct my code?

Thanks a lot,

raeiko

[code]#include

int main( void ) /* function main begins program execution */
{
int num1, num2, counter; /* declare variables */

printf("Enter the first number:"); /* prompt for input */
scanf("%d", &num1 ); /* read number from user */

printf("Enter the secon number:"); /* prompt for input */
scanf("%d", &num2 ); /* read number from user */

for ( counter = 1; counter >= 2; counter++ );{
if (num2 % num1 == 0)
printf( "num2 is multiple of num1
" );/* tests if the first number is a multiple of the second number */
else {
printf( "num2 is not multiple of num1
" );
}
}

return 0;
}
[/code]

• Posts: 438Member
Only one comment. This program takes [red]num1 == 0[/red]as a trigger to terminate the program. This seems to be a good choice since if you proceed with the program when [red]num1 == 0[/red] you will get a "divide by zero" error.

[code]
#include

#define bool int
#define TRUE 1
#define FALSE 0

int main(void) /* function main begins program execution */
{
int num1, num2; /* declare variables */

while (TRUE) {
printf("
Enter the first number:"); /* prompt for input */
scanf("%d", &num1 ); /* read number from user */

if (num1 == 0) /* quit program if first number is zero */
break ;

printf("Enter the secon number:"); /* prompt for input */
scanf("%d", &num2 ); /* read number from user */

if (ismult(num1, num2))
printf( "num2 is multiple of num1
" ) ;
else
printf( "num2 is not multiple of num1
" ) ;
}

return 0;
}

/* test if the first number is a multiple of the second number */
bool ismult(int num1, int num2)
{
return (num2 % num1 == 0) ;
}
[/code]
• Posts: 17Member
Hello,

thank you very much for your help.

Just a quick question: i have copied your code to VisualStudio Express 2005 but i get the following message from the compiler (but the program works if i start it without debugging):

1>------ Build started: Project: mcastriota_assignment4_Q1, Configuration: Debug Win32 ------
1>Compiling...
1>mcastriota_assignment4_Q1.cpp
1>c:userssaphiradocumentsvisual studio 2005projectsmcastriota_assignment4_q1mcastriota_assignment4_q1mcastriota_assignment4_q1.cpp(29) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:program filesmicrosoft visual studio 8vcincludestdio.h(295) : see declaration of 'scanf'
1>c:userssaphiradocumentsvisual studio 2005projectsmcastriota_assignment4_q1mcastriota_assignment4_q1mcastriota_assignment4_q1.cpp(35) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:program filesmicrosoft visual studio 8vcincludestdio.h(295) : see declaration of 'scanf'
1>c:userssaphiradocumentsvisual studio 2005projectsmcastriota_assignment4_q1mcastriota_assignment4_q1mcastriota_assignment4_q1.cpp(48) : error C2365: 'ismult' : redefinition; previous definition was 'formerly unknown identifier'
1>Build log was saved at "file://c:UsersSaphiraDocumentsVisual Studio 2005Projectsmcastriota_assignment4_Q1mcastriota_assignment4_Q1DebugBuildLog.htm"
1>mcastriota_assignment4_Q1 - 2 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

is there something wrong?

Thanks again,

raeiko
• Posts: 438Member
: Hello,
:
: thank you very much for your help.
:
: Just a quick question: i have copied your code to VisualStudio
: Express 2005 but i get the following message from the compiler (but
: the program works if i start it without debugging):
:
: 1>------ Build started: Project: mcastriota_assignment4_Q1,
: Configuration: Debug Win32 ------
: 1>Compiling...
: 1>mcastriota_assignment4_Q1.cpp
: 2005projectsmcastriota_assignment4_q1mcastriota_assignment4_q1mca
: striota_assignment4_q1.cpp(29) : warning C4996: 'scanf': This
: function or variable may be unsafe. Consider using scanf_s instead.
: To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help
: for details.
: 1> c:program filesmicrosoft visual studio
: 8vcincludestdio.h(295) : see declaration of 'scanf'
: 2005projectsmcastriota_assignment4_q1mcastriota_assignment4_q1mca
: striota_assignment4_q1.cpp(35) : warning C4996: 'scanf': This
: function or variable may be unsafe. Consider using scanf_s instead.
: To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help
: for details.
: 1> c:program filesmicrosoft visual studio
: 8vcincludestdio.h(295) : see declaration of 'scanf'
: 2005projectsmcastriota_assignment4_q1mcastriota_assignment4_q1mca
: striota_assignment4_q1.cpp(37) : error C3861: 'ismult': identifier
: 2005projectsmcastriota_assignment4_q1mcastriota_assignment4_q1mca
: striota_assignment4_q1.cpp(48) : error C2365: 'ismult' :
: redefinition; previous definition was 'formerly unknown identifier'
: 1>Build log was saved at "file://c:UsersSaphiraDocumentsVisual
: Studio
: 2005Projectsmcastriota_assignment4_Q1mcastriota_assignment4_Q1Deb
: ugBuildLog.htm"
: 1>mcastriota_assignment4_Q1 - 2 error(s), 2 warning(s)
: ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
: ==========
:
: is there something wrong?
:
: Thanks again,
:
: raeiko
:
The program compiles and runs with my antique C compiler. My guess is that you are compiling it with a C++ compiler since it doesn't like [b]scanf[/b]. I really don't know that much about C++ but you could try the following:

1. See if there is some way to tell the compiler that the code is C, not C++.

2. Try adding [b]#define scanf scanf_s[/b] to the list of #defines.

3. Try putting all the code for [b]ismult(int num1, num2)[/b] before [b]main()[/b], or

4. Prototype [b]ismult(int num1, num2)[/b]

• Posts: 17Member
Hello again,

on the basis of your hints, i have revised my code. I cannot use the boolean functions because i haven't studied them yet...

Here is what i have done so far, but there are some errors in the second part because, to be honest, after reading and reading again from my book how to call a fuction, i still haven't it clear:-(
The program only return the result of the last couple of numbers.

I'm sure i'm making some stupid mistake...

[code]/* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */

#include
#include

int multiple ( int j, int z); /* function prototype */

int main( void ) /* function main begins program execution */
{
int num1, num2, x, y; /* 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){
break;
printf( "
Broke from loop because num1 must be greater than 0
" ); /* break loop if num1 == 0 */
}
else {
printf( "Enter the second number:" ); /* prompt for input */
scanf_s( "%d", &num2 ); /* read number from user */
}
}

for ( y = 1; y <= x ; y++ )
if( multiple == 0){
printf( "%d is multiple of %d
", num2, num1);
}
else {
printf( "%d is not multiple of %d
", num2, num1 );
}

return 0;
}

int multiple ( int j, int z ) /* copy of the argment to function */
{
return z % j;
} /* end of multiple function */[/code]

• Posts: 438Member
: The program only return the result of the last couple of numbers.
[blue]
Because you have two loops. The first reads input, the second writes output. But every iteration of the first loop overwrites the data from the previious iteration, meaning the only data the second loop sees is the data from the last iteration of the first loop. You need one loop that has both input and output.
[/blue]
[code]
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){
break; [red]{ the printf is unreachable because it is
preceded by the break.
Reverse the order of these two statements }[/red]
printf( "
Broke from loop because num1 must be greater than 0
" ); /* break loop if num1 == 0 */
}
else {
printf( "Enter the second number:" ); /* prompt for input */
scanf_s( "%d", &num2 ); /* read number from user */
}
}
[/code]
• Posts: 17Member
Thanks a lot for your precious help!
:-)

raeiko