Multiply and Divide Numbers in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to perform basic arithmetic operations, such as multiplication and division, using C programming. The lab covers the following steps:

Firstly, you will learn how to declare variables and input values from the user. You will create a simple program that allows users to enter two numbers, which will then be used for the multiplication and division operations.

Next, you will extend the program to perform the actual multiplication and division calculations using the input values. The program will display the results of these operations, and also handle the case of division by zero gracefully.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435355{{"`Multiply and Divide Numbers in C`"}} c/variables -.-> lab-435355{{"`Multiply and Divide Numbers in C`"}} c/operators -.-> lab-435355{{"`Multiply and Divide Numbers in C`"}} c/if_else -.-> lab-435355{{"`Multiply and Divide Numbers in C`"}} c/user_input -.-> lab-435355{{"`Multiply and Divide Numbers in C`"}} c/math_functions -.-> lab-435355{{"`Multiply and Divide Numbers in C`"}} end

Declare Variables and Input Values

In this step, you will learn how to declare variables and input values for performing arithmetic operations in C programming. We'll create a simple program that allows users to enter two numbers for multiplication and division.

First, let's create a new C file in the ~/project directory:

cd ~/project
nano arithmetic_operations.c

Now, enter the following code:

#include <stdio.h>

int main() {
    // Declare variables to store input numbers
    float num1, num2;

    // Prompt user to enter first number
    printf("Enter the first number: ");
    scanf("%f", &num1);

    // Prompt user to enter second number
    printf("Enter the second number: ");
    scanf("%f", &num2);

    // Print the entered numbers
    printf("First number: %.2f\n", num1);
    printf("Second number: %.2f\n", num2);

    return 0;
}

Let's break down the code:

  • We use float data type to allow decimal numbers
  • printf() is used to display prompts to the user
  • scanf() reads user input and stores it in variables
  • %.2f formats the output to display two decimal places

Compile the program:

gcc arithmetic_operations.c -o arithmetic_operations

Run the program and enter some sample numbers:

./arithmetic_operations

Example output:

Enter the first number: 10.5
Enter the second number: 3.2
First number: 10.50
Second number: 3.20

Perform Multiplication and Division

In this step, you will extend the previous program to perform multiplication and division operations using the input values.

Open the existing arithmetic_operations.c file:

cd ~/project
nano arithmetic_operations.c

Update the code with multiplication and division operations:

#include <stdio.h>

int main() {
    // Declare variables to store input numbers and results
    float num1, num2, multiply_result, divide_result;

    // Prompt user to enter first number
    printf("Enter the first number: ");
    scanf("%f", &num1);

    // Prompt user to enter second number
    printf("Enter the second number: ");
    scanf("%f", &num2);

    // Perform multiplication
    multiply_result = num1 * num2;
    printf("Multiplication: %.2f * %.2f = %.2f\n", num1, num2, multiply_result);

    // Perform division
    divide_result = num1 / num2;
    printf("Division: %.2f / %.2f = %.2f\n", num1, num2, divide_result);

    return 0;
}

Compile the updated program:

gcc arithmetic_operations.c -o arithmetic_operations

Run the program and test multiplication and division:

./arithmetic_operations

Example output:

Enter the first number: 10.5
Enter the second number: 3.2
Multiplication: 10.50 * 3.20 = 33.60
Division: 10.50 / 3.20 = 3.28

Key points:

  • * operator performs multiplication
  • / operator performs division
  • Results are stored in separate variables
  • %.2f formats the output to two decimal places

Handle Division by Zero Gracefully

In this step, you will learn how to handle division by zero, which is a common error in arithmetic operations that can cause program crashes.

Open the existing arithmetic_operations.c file:

cd ~/project
nano arithmetic_operations.c

Update the code to handle division by zero:

#include <stdio.h>

int main() {
    // Declare variables to store input numbers and results
    float num1, num2, multiply_result, divide_result;

    // Prompt user to enter first number
    printf("Enter the first number: ");
    scanf("%f", &num1);

    // Prompt user to enter second number
    printf("Enter the second number: ");
    scanf("%f", &num2);

    // Perform multiplication
    multiply_result = num1 * num2;
    printf("Multiplication: %.2f * %.2f = %.2f\n", num1, num2, multiply_result);

    // Check for division by zero before performing division
    if (num2 != 0) {
        divide_result = num1 / num2;
        printf("Division: %.2f / %.2f = %.2f\n", num1, num2, divide_result);
    } else {
        printf("Error: Division by zero is not allowed!\n");
    }

    return 0;
}

Compile the updated program:

gcc arithmetic_operations.c -o arithmetic_operations

Run the program and test different scenarios:

Test with a non-zero divisor:

./arithmetic_operations

Example output (non-zero divisor):

Enter the first number: 10.5
Enter the second number: 3.2
Multiplication: 10.50 * 3.20 = 33.60
Division: 10.50 / 3.20 = 3.28

Test with zero as divisor:

./arithmetic_operations

Example output (zero divisor):

Enter the first number: 10.5
Enter the second number: 0
Multiplication: 10.50 * 0.00 = 0.00
Error: Division by zero is not allowed!

Key points:

  • Use an if statement to check if the divisor is zero
  • Provide a user-friendly error message
  • Prevent program crash by handling the division by zero case

Summary

In this lab, you learned how to declare variables and input values for performing arithmetic operations in C programming. You created a simple program that allows users to enter two numbers for multiplication and division. You also learned how to perform multiplication and division operations using the input values, and how to handle division by zero gracefully by checking for zero divisor before performing the division operation.

The key learning points from this lab include:

  • Declaring variables to store input numbers and results
  • Using printf() to display prompts and scanf() to read user input
  • Performing multiplication and division operations on the input values
  • Handling division by zero error to prevent the program from crashing

Other C Tutorials you may like