Untitled
unknown
plain_text
4 years ago
2.2 kB
5
Indexable
#include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { float num1, num2, geo_mean; char waste; int flag1, flag2, condition = 1; printf("<Input a non-numerical input to terminate the program>\n"); printf("<Input your two numbers separated by a space>\n"); while((flag1 = scanf("%f", &num1)) == 1 && (flag2 = scanf("%f", &num2)) == 1){ if(num1*num2 >= 0){ geo_mean = sqrt(num1*num2); printf("The geometric mean of %.2f and %.2f is %.2f.\n", num1, num2, geo_mean); printf("Please input your two numbers separated by a space: "); } else{ printf("Invalid input. \nPlease try again: "); } scanf("%s", &waste); } // float input_1, input_2, waste; // int number; // float geo_mean; // int flag1 = 0, flag2=0; // // printf("Please input your two number:"); // // while ((flag1 = scanf("%f", &input_1) == 1 )&&(flag2=(scanf("%f", &input_2)) == 1)) // { // if (input_1 * input_2 >= 0) // { // geo_mean = sqrt(input_1 * input_2); // printf("The geometric mean of %.2f and %.2f is %.2f\n", input_1, // input_2, geo_mean); // printf("Please input your two number:"); // } // else // printf("Your input is invalid,only numbers with the same sign !\nPlease input your number:"); // } // scanf("%s", &waste); // printf("SY2002103"); // printf("<Enter a non-numerical input to exit the program>\n"); // while(condition){ // printf("Please input your first number: "); // scanf("%f", &num1); // if(isdigit(num1) == 0){ // break; // } // printf("Please input your second number: "); // scanf("%f", &num2); // if(isdigit(num2) == 0){ // break; // } // if(num1*num2 >= 0){ // geo_mean = sqrt(num1*num2); // printf("The geometric mean is %.2f\n", geo_mean); // } // else{ // printf("Your input is invalid. Please try again.\n"); // } // } return 0; }
Editor is loading...