Create an account on Neowin to contribute and support the site.

Newbie C help (I know its not C++ or C#)

Recommended Posts

Terabojin    0

Hello guys!

So I'm writing this program that takes 2 integers and determines if the first number is an integer multiple of the second number.  I'm using the % operator.  I'm also trying to determine if the second number is an integer multiple of the first number.  So far this is what I have:

#include "stdio.h"

int x, y;

void main()

{
printf("Please enter in the first whole number\n");
scanf("%d", &x);
printf("Please enter in the second whole number\n");
scanf("%d", &y);

if ((x % y) == 0)
printf("The number %d is a multiple of %d\n",x,y);
if ((y % x) == 0)
printf("The number %d is a multiple of %d\n",y,x);

}

So far I have gotten the program to run almost flawlessly until it comes to the 2nd if statement.  I cannot get this silly program to work correctly.  It tells me that the 1st number is a multiple of the 2nd number, but it will not tell me if the 2nd number is a multiple of the first.  Any ideas?  Any and all help will be greatly appreciated!

Tera

Share on other sites
+Andre S.    1,891
it will not tell me if the 2nd number is a multiple of the first.

Sample run:

Please enter in the first whole number
1
Please enter in the second whole number
2
The number 2 is a multiple of 1

The program tells you that the 2nd number is a multiple of the first as specified. What's wrong with it?

Share on other sites
Terabojin    0

Sample run:

The program tells you that the 2nd number is a multiple of the first as specified. What's wrong with it?

I need it to also tell me if the first number is a multiple of the second.  I know it is redundant....

Share on other sites
MFH    138
Terabojin, on 16 Sept 2013 - 21:09, said:

I need it to also tell me if the first number is a multiple of the second.  I know it is redundant....

1 % 2 == 1 !

Share on other sites
+Andre S.    1,891

I need it to also tell me if the first number is a multiple of the second.  I know it is redundant....

Your program only tells you that a number is a multiple of the other if it is. It cannot currently tell you that a number is not a multiple of another. You need a printf somewhere with "The number x is not a multiple...".

Share on other sites
bogas04    198

Computer only does what you ask it to do.

Share on other sites
Terabojin    0

OH!    Sheesh, I feel silly now.  Thank you so much for pointing out the obvious to me lol.  Just feel free to call me Captain Oblivious =)

Share on other sites
+Majesticmerc    871

OH!    Sheesh, I feel silly now.  Thank you so much for pointing out the obvious to me lol.  Just feel free to call me Captain Oblivious =)

We all do silly stuff like this, don't worry about it :)