Thread: If else if stament help

  1. #16
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    ArunS
    I run program then I input x=10,y=5,z=15
    output = 10
    hmmmm double digits and single digit numbers do not work.
    it seems that i was so happy with the program working with 1 digit xyz 2digits xyz but not 1 and 2 digits xyz.

  2. #17
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by langamer101 View Post
    ArunS
    I run program then I input x=10,y=5,z=15
    output = 10
    hmmmm double digits and single digit numbers do not work.
    it seems that i was so happy with the program working with 1 digit xyz 2digits xyz but not 1 and 2 digits xyz.
    Its not a matter of either single digit or double digit?
    Ok! Try to check for the following inputs.
    i/p: x=5; y=4; z=6;

  3. #18
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    It is not a problem about single digit or double digit, it is just about the code logic. Again, what you want to check in your if and if else statement is for example x > y AND x > z which is the logic translation for testing "is x bigger than y and bigger than z?". Try to use that condition in your code for each if and if else, and you'll see that it should work.
    HomePort : A C Web Service API for heterogeneous home automation systems

  4. #19
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by langamer101 View Post
    Its looks exactly the same but its not in logic.
    so basically i started looking where would the large number go if true or false
    if x > y it would print x if false goes to next else if
    if z > y it would print z if false goes to next else if
    if y > x it would print y if false goes to x. but thats how I solved it.
    the program now works.
    if x > y it would print x if false goes to next else if
    you are printing x value even if z is greater the x.
    you have to print the value of x only if it is greater than both y and z.

  5. #20
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    omg, what sorcery it this.
    output is 5??????????????????????

  6. #21
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by langamer101 View Post
    omg, what sorcery it this.
    output is 5??????????????????????
    Use logical operator, as per post#18

  7. #22
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    lol that was all. x>y>z on the first if
    OH I SEE. omg I look so ..............
    thanks guys.
    edit
    more problems...............omg ill try to figure it out.
    Last edited by langamer101; 01-18-2012 at 08:03 AM.

  8. #23
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    we have concentrated only for x, you have to do it in a same way for y and z conditions, thats all.

  9. #24
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    it this code correct now?. I run it, put aruns x=10,y=5,z=15 and x=5,y=4,z=6 and works.
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
        clrscr();
        int x,y,z,l;
        printf("Enter value for x: ");
        scanf("%d",&x);
        printf("Enter value for y: ");
        scanf("%d",&y);
        printf("Enter value for z: ");
        scanf("%d",&z);
        if (x>y>z)
            printf("The large number is %d", x);
        else if (z>y)
            printf("The large number is %d", z);
        else if (y>x)
            printf("The large number is %d", y);
        else if (x>z)
            printf("The large number is %d", x);
    
    
        else
            printf("all values are the same");
            getch();
            return(0);
    }
    Last edited by langamer101; 01-18-2012 at 08:15 AM.

  10. #25
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    First, I did not notice from the beginning, but take some time to read this page.

    Then "x>y>z" is not good, that is not the proper way to do it. You have to compare if x>y AND x>z, by using the AND logic operator, which is "&&" in C.

    Also, as ArunS mentioned, you have to apply this logic for all of the other statement.

    At that point, and without trying to be mean, I think following a C tutorial would be a good idea for you. You will then understand much better about the theory and the syntax of the C language, and you will be able to solve this kind of problems in 5 minutes.
    HomePort : A C Web Service API for heterogeneous home automation systems

  11. #26
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    ummm, hmm, thanks.
    time to start reading and quiz myself on C tutorial.

  12. #27
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by langamer101 View Post
    it this code correct now?. I run it, put aruns x=10,y=5,z=15 and x=5,y=4,z=6 and works.
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
        clrscr();
        int x,y,z,l;
        printf("Enter value for x: ");
        scanf("%d",&x);
        printf("Enter value for y: ");
        scanf("%d",&y);
        printf("Enter value for z: ");
        scanf("%d",&z);
        if (x>y>z)
            printf("The large number is %d", x);
        else if (z>y)
            printf("The large number is %d", z);
        else if (y>x)
            printf("The large number is %d", y);
        else if (x>z)
            printf("The large number is %d", x);
    
    
        else
            printf("all values are the same");
            getch();
            return(0);
    }
    Why don't you consider with logical operators for your implementation?

  13. #28
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    my teacher has not taught me about "&&, ||, !" logical operators yet.
    only these ">,<,=,==,>=,<=" relational operators.
    Last edited by langamer101; 01-18-2012 at 08:38 AM.

  14. #29
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    It is actually possible to make it without the "&&", but then the logic will be different.

    I usually don't like to give so detailed answer, but you seem like you thought quite a lot, so I will make an exception .

    Here is what you could do :

    -Assign the x value to l.
    -Test if l is lesser than y.
    -If yes
    -Assign the y value to l.
    -Test if l is lesser than z.
    -If yes, then z is the biggest.
    -If no y is the bigest.
    -If no ...

    I think you can find the rest by yourself.
    Last edited by Tibo-88; 01-18-2012 at 08:45 AM. Reason: Bad indent
    HomePort : A C Web Service API for heterogeneous home automation systems

  15. #30
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    here is code now with && logical operator
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	clrscr();
    	int x,y,z,l;
    	printf("Enter value for x: ");
    	scanf("%d",&x);
    	printf("Enter value for y: ");
    	scanf("%d",&y);
    	printf("Enter value for z: ");
    	scanf("%d",&z);
    	if (x>y && x>z)
    		printf("The large number is %d", x);
    	else if (z>y && z>x)
    		printf("The large number is %d", z);
    	else if (y>x && y>z)
    		printf("The large number is %d", y);
    	else
    		printf("all values are the same");
    		getch();
    		return(0);
    }
    tibo Ill try this x value to l and so on what you posted

Popular pages Recent additions subscribe to a feed

Tags for this Thread