Challenge 4: Share the Candies! 🚀
Welcome to this challenge! Work with Robo 🤖 to solve this problem step-by-step using algorithms and flowcharts.
🎬 The Story
Robo starts with $8$ candies. He gives away $3$ candies. Help Robo read two numbers, subtract the second number from the first, and calculate the remaining candies.
📝 The Algorithm
- Start (Begin)
- Read Number1 and Number2 (Input)
- Subtract Number2 from Number1 and store result in
diff(Process) - Display diff (Output)
- Stop (End)
📊 The Flowchart
💻 From Flowchart to Code
Here is how to subtract two input numbers in Python, Java, and C:
- Python
- Java
- C
# Read two numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# Subtract
diff = num1 - num2
# Display result
print("Difference:", diff)
import java.util.Scanner;
public class SubtractNumbers {
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();
// Calculate difference
int diff = num1 - num2;
System.out.println("Difference: " + diff);
}
}
#include <stdio.h>
int main() {
int num1, num2, diff;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Calculate difference
diff = num1 - num2;
printf("Difference: %d\n", diff);
return 0;
}