Jump to content



Photo

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


  • Please log in to reply
7 replies to this topic

#1 Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 16 September 2013 - 18:54

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




#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 16 September 2013 - 19:07

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?



#3 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 16 September 2013 - 19:09

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....



#4 MFH

MFH

    RAII fanatic

  • Joined: 24-June 10

Posted 16 September 2013 - 19:12

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 !



#5 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 16 September 2013 - 19:14

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...".



#6 bogas04

bogas04

    This title is bogus

  • Joined: 15-May 09
  • Location: India
  • OS: Windows 8
  • Phone: Lumia 920

Posted 16 September 2013 - 19:22

http://codepad.org/QWhRw2I8

 

Computer only does what you ask it to do.



#7 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 16 September 2013 - 19:24

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 =)



#8 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 16 September 2013 - 21:20

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 :)