Thread: Simple if/else statement problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Simple if/else statement problem

    I have to create a program that will read integer numbers from the user, one at a time. It will print these numbers out after reading them. It will continue to read numbers until it receives the number 7 or 11. If the number is both even and a multiple of 5, then it will print the number multiplied by itself. If the number is instead a multiple of 6, but is not a multiple of 5, then print twice the number (instead of the number itself).

    given the following inputs, the outputs should be: (these are just examples, the number should be printed out right after receiving the input number
    Input Output
    3 3
    10 100
    12 24
    15 15
    7

    I wrote the following code:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int x, y;
        
        printf("Enter in a number: \n");
        scanf("%d", &x);
        
        
        if (x % 2 == 0)
        
        {
        printf("%d\n", y = x * x);
        }
        
        else
        {
            printf("%d\n", y);
        }
        
        return 0;
    }
    But the problem I am having is if the number is instead a multiple of 6, but is not a multiple of 5, then print twice the number (instead of the number itself). If it's better to use while loops can someone please let me know how. I have not learned while loops yet.

    Any help is appreciated, but please explain.
    Last edited by benrogers; 02-06-2011 at 02:42 PM.

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    >> "If the number is both even and a multiple of 5"

    ok

    Code:
    if(num%2==0 && num%5==0)
    {
      cout num*num
    }
    else  if(!(num%6) && num%5)
    {
        cout << num*2 //?
    }

    //will try it now

    EDIT:

    anyways to read the number..you need a loop

    so while(num!=7 && num !=11)
    you need to read control structures
    Last edited by Eman; 02-06-2011 at 02:45 PM.
    You ended that sentence with a preposition...Bastard!

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can nest if statements:
    Code:
    if(x is even)
    {
      if(x is multiple of 5)
        print x * x
      else
      {
        if(x is multiple of 6)
          print x * 2
      }
    }
    Last edited by itsme86; 02-06-2011 at 02:43 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Eman View Post
    >> "If the number is both even and a multiple of 5"

    ok
    Not divisible by 5, a multiple of 5 (e.g. 10, 20, etc.)
    If you understand what you're doing, you're not learning anything.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by itsme86 View Post
    Not divisible by 5, a multiple of 5 (e.g. 10, 20, etc.)
    yeah..they're divisible by 5? (and even).. :S
    You ended that sentence with a preposition...Bastard!

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Eman View Post
    >> "If the number is both even and a multiple of 5"

    ok

    Code:
    if(num%2==0 && num%5==0)
    {
      cout num*num
    }
    else  if(!(num%6) && num%5)
    {
        cout << num*2 //?
    }

    //will try it now

    EDIT:

    anyways to read the number..you need a loop

    so while(num!=7 && num !=11)
    you need to read control structures
    Can you please explain the while loop?

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    the while loop is basically the same as an if..except it is repetitive..and always as long as the condition is true...
    so
    Code:
    while(num!=7 && num!=11)
    {
       cin >> num ;
    }
    if num is not equals to 7 && not equals to 11 is the same as
    while(1 && 1)

    if it is was 7 and 10 say..
    it would be while(1 && 0) and the loop would end


    if it was while(num!=7 || num!=11)
    and num was 7
    it would be while(0 || 1)
    same as while(1)...

    EDIT:

    to read: http://www.learncpp.com/cpp-tutorial...ays-and-loops/
    You ended that sentence with a preposition...Bastard!

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    It sounds like you're going to have to use operators && (boolean AND operator) and || (boolean OR operator). This should do the trick.

    Code:
    #include <stdio.h>
    
    int main() {
    	int x;
    	while (x != 7 && x != 11) {
    		/* While x is not equal to 7 and x is not equal to 11... */
    		printf("Enter a number: ");
    		scanf("%d", &x);
    		if ((x % 2 == 0) && (x % 5 == 0))
    			/* If x is an even number and x is a multiple of 5... */
    			printf("%d\n", (x * x));
    		else if (x % 6 == 0)
    			/* If x is a multiple of 6... */
    			printf("%d\n", (x * 2));
    		else
    			printf("%d\n", x);
    	}
    	return 0;
    }
    Last edited by Babkockdood; 02-06-2011 at 02:59 PM.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh crap!..just realized I was using C++ code...sorry!
    You ended that sentence with a preposition...Bastard!

  10. #10
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Babkockdood View Post
    It sounds like you're going to have to use operators && (boolean AND operator) and || (boolean OR operator). This should do the trick.

    Code:
    #include <stdio.h>
    
    int main() {
    	int x;
    	while (x != 7 && x != 11) {
    		/* While x is not equal to 7 and x is not equal to 11... */
    		printf("Enter a number: ");
    		scanf("%d", &x);
    		if ((x % 2 == 0) && (x % 5 == 0))
    			/* If x is an even number and x is a multiple of 5... */
    			printf("%d\n", (x * x));
    		else if (x % 6 == 0)
    			/* If x is a multiple of 6... */
    			printf("%d\n", (x * 2));
    		else
    			printf("%d\n", x);
    	}
    	return 0;
    }
    lol you had to answer it for him ..lol I'm sure he "learnt" it! :P

    btw i think the else if is wrong
    if x = 30? then it is a multiple of 6 and 5

    Code:
    else if (x % 6 == 0 && (x%5))
    			/* If x is a multiple of 6... */
    			printf("%d\n", (x * 2));
    i think should be it...
    You ended that sentence with a preposition...Bastard!

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    The OP didn't mention a condition for if a number is a multiple of six and a multiple of five. Just one for if the number is even and a multiple of five, and one for if the number is a multiple of six.

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Babkockdood View Post
    The OP didn't mention a condition for if a number is a multiple of six and a multiple of five. Just one for if the number is even and a multiple of five, and one for if the number is a multiple of six.
    post by OP:
    If the number is instead a multiple of 6, but is not a multiple of 5,
    I dunno..
    You ended that sentence with a preposition...Bastard!

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Babkockdood View Post
    It sounds like you're going to have to use operators && (boolean AND operator) and || (boolean OR operator). This should do the trick.

    Code:
    #include <stdio.h>
    
    int main() {
    	int x;
    	while (x != 7 && x != 11) {
    		/* While x is not equal to 7 and x is not equal to 11... */
    		printf("Enter a number: ");
    		scanf("%d", &x);
    		if ((x % 2 == 0) && (x % 5 == 0))
    			/* If x is an even number and x is a multiple of 5... */
    			printf("%d\n", (x * x));
    		else if (x % 6 == 0)
    			/* If x is a multiple of 6... */
    			printf("%d\n", (x * 2));
    		else
    			printf("%d\n", x);
    	}
    	return 0;
    }
    Thank you for for this. I appreciate the comments.

  14. #14
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    No problem.

    Have a look at other logical operators. Logical Operators (&&, ||, !)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 07:59 AM
  4. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM