// Demonstrating using IF statements // with logical operators // Sasha Vassar, Week 2 Lecture 3 // AND && // OR || #include // Problem 1: Let the user provide you a number, that you then check // and decide how many digits that number has. // Problem 2: Let's extend the problem, the user now provides you two // numbers, that you add together and then check and decide how many // digits the sum has. int main (void) { // 1. Let the user provide number - scanf() // 2. Check for number of digits - // -10 < x < 10 1 digit // 10 <= x < 100 2 (digits) -100 < x <= -10 // 3. Print out the number of digits - printf() int sum; int number_one; int number_two; int scanf_return; printf("Please enter two numbers: "); // scanf has a return value - the number of things it has scanned in. scanf_return = scanf("%d %d", &number_one, &number_two); printf("scanf_return: %d\n", scanf_return); if (scanf_return != 2) { printf("Error, a number was not scanned in\n"); return 1; } sum = number_one + number_two; if (-10 < sum && sum < 10) { printf("%d has 1 digit\n", sum); } else if ((10 <= sum && sum < 100) || (-10 >= sum && sum > -100)) { printf("%d has 2 digits.\n", sum); } else if (sum >= 100 || sum <= -100) { printf("%d has more than 2 digits.\n", sum); } return 0; }