Challenge 6: Share Pizza Safely! 🚀
Welcome to this challenge! Work with Robo 🤖 to solve this problem step-by-step using algorithms and flowcharts.
🎬 The Story
Robo wants to share $4$ pizzas equally between $2$ children. Help Robo divide them, but make sure to check if the number of children is $0$ first, as dividing by zero is impossible!
📝 The Algorithm
- Start (Begin)
- Read Number1 and Number2 (Input)
- Check if Number2 is 0 (Decision)
- If Yes: Display "Cannot divide by zero" (Output), then go to step 6 (Stop)
- If No: Proceed to step 4
- Divide Number1 by Number2 and store in
quotient(Process) - Display quotient (Output)
- Stop (End)
📊 The Flowchart
Here, the pink Decision Diamond is introduced.
💻 From Flowchart to Code
Here is how to divide two input numbers with zero check safety in Python, Java, and C:
- Python
- Java
- C
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# Check for division by zero
if num2 == 0:
print("Cannot divide by zero")
else:
ans = num1 / num2
print("Quotient:", ans)
import java.util.Scanner;
public class DivideNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
// Check for division by zero
if (num2 == 0) {
System.out.println("Cannot divide by zero");
} else {
double ans = (double) num1 / num2;
System.out.println("Quotient: " + ans);
}
}
}
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Check for division by zero
if (num2 == 0) {
printf("Cannot divide by zero\n");
} else {
float ans = (float)num1 / num2;
printf("Quotient: %.2f\n", ans);
}
return 0;
}