Day 2: Simple Math

Objective

In today’s task, you will create a program that performs a simple mathematical operation: adding two numbers and printing the result. This exercise will introduce you to basic arithmetic and how to handle variables in your code.

Performing mathematical operations is a fundamental aspect of programming, as most real-world applications rely on calculations in some form, whether it’s for handling user data, processing financial figures, or manipulating other types of information.

Task Breakdown

  1. Ask the user to input two numbers.
  2. Add the two numbers together.
  3. Display the result of the addition.

Helpful Tips to Solve the Task

1. Understanding Variables

  • A variable is a place in memory where data can be stored and later used or manipulated.
  • You will need to store the two numbers the user inputs in variables, perform the addition operation, and then store the result in another variable.

2. Getting User Input

Python: Use the input() function to get user input. By default, input() returns data as a string, so you’ll need to convert it to an integer using the int() function.

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

Java: Use the Scanner class to take input. The nextInt() method will help you capture the numbers as integers.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();
        System.out.print("Enter the second number: ");
        int num2 = scanner.nextInt();
        int sum = num1 + num2;
        System.out.println("The sum is: " + sum);
    }
}

JavaScript: Use prompt() to gather user input, and convert the input to numbers using parseInt() or parseFloat() (if working with decimals).

let num1 = parseInt(prompt("Enter the first number: "));
let num2 = parseInt(prompt("Enter the second number: "));
let sum = num1 + num2;
console.log("The sum is: " + sum);

3. Performing the Addition

After you have stored the user input in variables, simply add the two variables together. The result will be stored in a new variable.

Example (Python):

result = num1 + num2
print("The sum is:", result)

Example (Java):

int sum = num1 + num2;
System.out.println("The sum is: " + sum);

Example (JavaScript):

let sum = num1 + num2;
console.log("The sum is: " + sum);

4. Edge Cases to Consider

  • Invalid Input: Ensure that the user inputs valid numbers. You might want to add error handling to deal with non-numeric inputs.
  • Decimals: If you’re working with decimal numbers, you can use float() (Python), nextFloat() (Java), or parseFloat() (JavaScript) instead of int() or nextInt().

5. Testing Your Program

  • After running the program, test it by entering different numbers to ensure it correctly calculates the sum. Try using both small and large numbers to see if the program works as expected.

What You’ve Learned

  • Variables: You’ve learned how to store and work with variables.
  • User Input: You’ve become familiar with capturing input from users and performing operations on it.
  • Basic Arithmetic: You’ve used basic addition in your code, a fundamental operation in programming.

Next Steps

Once you’ve successfully written the program, try extending it. For example, you can:

  • Allow the user to perform subtraction, multiplication, or division.
  • Handle decimal values instead of only integers.

This exercise is just the beginning. Keep practicing basic operations and experiment with different kinds of input to gain more comfort with coding.