Home > Mobile >  How to loop conditions in do-while
How to loop conditions in do-while

Time:10-06

Here is my code. I am trying to find different things for a triangle and I have a few conditions.

I am new in c programming and I would like to know how I can loop these conditions. If, for example, the first condition is not met, then it displays the appropriate message and asks you to enter again and so on constantly until the entered numbers are correct.

do {  
    printf("Enter the first side of the triangle: ");
    scanf("%f", &a);
    printf("Enter the second side of the triangle: ");
    scanf("%f", &b);
    printf("Enter the third side of the triangle: ");
    scanf("%f", &c);

    if (a > (b c)) {
        printf("Invalid value a");
    }
    else if (b > (a c)) {
        printf("Invalid value b");
    }
    else if (c > (a b)) {
        printf("Invalid value c");
    }
    else if (a < 0) {
        printf("The number a must be positive");
    }
    else if (b < 0) {
        printf("The number b must be positive");
    }
    else if (c < 0) {
        printf("The number c must be positive");
    } 
    else {
        perimeter = a   b   c;
        half_p = perimeter / 2;
        printf("\nPerimeter of triangle: %f\n", perimeter);

        height = ((2 * sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c))) / a);
        printf("\nHeight of triangle: %f\n", height);

        square = sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c));
        printf("\nSquare of triangle: %f\n", square);

        median = 0.5 * sqrt(2 * pow(b, 2)   2 * pow(c, 2) - pow(a, 2));
        printf("\nMedian of triangle: %f\n", median);

        bisector = ((2 / (b   c))) * sqrt(b * c * half_p * (half_p - a));
        printf("\nBisector of triangle: %f\n", bisector);
    }
} while ()

I think I should add something here to make the program work perfectly, but I don't understand what. Thank you!

CodePudding user response:

#include <math.h>
#include <stdio.h>

int main()
{
    
    float a,b,c;
    float median, perimeter, half_p, height, square, bisector;
do {  
    printf("Enter the first side of the triangle: ");
    scanf("%f", &a);
    printf("Enter the second side of the triangle: ");
    scanf("%f", &b);
    printf("Enter the third side of the triangle: ");
    scanf("%f", &c);

    if(a > (b c)) {
        printf("Invalid value a");
        while(a > (b c)){
              printf("Enter the first side of the triangle: ");
    scanf("%f", &a);  
        }
    }
    
    else if (b > (a c)) {
        printf("Invalid value b");
                while(b > (a c)){
    printf("Enter the second side of the triangle: ");
    scanf("%f", &b);
        }
    }

    else if (c > (a b)) {
                while(c > (a b)){
    printf("Enter the third side of the triangle: ");
    scanf("%f", &c);
        }    }

    else if (a < 0) {
        printf("The number a must be positive");
        while(a<0){
                printf("Enter the first side of the triangle: ");
    scanf("%f", &a);
        }
    }

    else if (b < 0) {
        printf("The number b must be positive");
                while(b<0){
                printf("Enter the second side of the triangle: ");
    scanf("%f", &b);
        }
    }

    else if (c < 0) {
        printf("The number c must be positive");
                        while(c<0){
                printf("Enter the third side of the triangle: ");
    scanf("%f", &c);
        }
    } else {
        perimeter = a   b   c;
        half_p = perimeter / 2;
        printf("\nPerimeter of triangle: %f\n", perimeter);

        height = ((2 * sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c))) / a);
        printf("\nHeight of triangle: %f\n", height);

        square = sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c));
        printf("\nSquare of triangle: %f\n", square);

        median = 0.5 * sqrt(2 * pow(b, 2)   2 * pow(c, 2) - pow(a, 2));
        printf("\nMedian of triangle: %f\n", median);

        bisector = ((2 / (b   c))) * sqrt(b * c * half_p * (half_p - a));
        printf("\nBisector of triangle: %f\n", bisector);
    }

} while (1);
}

CodePudding user response:

_Bool invalid = 1; //Assume invalid

do{
    printf("Enter the first side of the triangle: ");
    scanf("%f", &a);
    printf("Enter the second side of the triangle: ");
    scanf("%f", &b);
    printf("Enter the third side of the triangle: ");
    scanf("%f", &c);

    if (a > (b c)) {
        printf("Invalid value a");
    }
    else if (b > (a c)) {
        printf("Invalid value b");
    }
    else if (c > (a b)) {
        printf("Invalid value c");
    }
    else if (a < 0) {
        printf("The number a must be positive");
    }
    else if (b < 0) {
        printf("The number b must be positive");
    }
    else if (c < 0) {
        printf("The number c must be positive");
    }
    else
      invalid = 0; //Change to valid if passes all tests

}while(invalid); //If invalid loop back to the scanf calls

//This stuff shouldn't happen until the input is valid so it should be outside the loop
perimeter = a   b   c;
half_p = perimeter / 2;
printf("\nPerimeter of triangle: %f\n", perimeter);

height = ((2 * sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c))) / a);
        printf("\nHeight of triangle: %f\n", height);

square = sqrt(half_p * (half_p - a) * (half_p - b) * (half_p - c));
printf("\nSquare of triangle: %f\n", square);

median = 0.5 * sqrt(2 * pow(b, 2)   2 * pow(c, 2) - pow(a, 2));
printf("\nMedian of triangle: %f\n", median);

bisector = ((2 / (b   c))) * sqrt(b * c * half_p * (half_p - a));
printf("\nBisector of triangle: %f\n", bisector);

  • Related