Add and Subtract Numbers in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to perform basic arithmetic operations, such as addition and subtraction, using C programming. The lab covers the steps to declare variables, input values, and execute the arithmetic operations. You will then learn how to print the results of these calculations.

The lab consists of two main steps. First, you will declare variables and prompt the user to enter two numbers. Then, you will perform the addition and subtraction operations on the input values and display the results.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435335{{"`Add and Subtract Numbers in C`"}} c/variables -.-> lab-435335{{"`Add and Subtract Numbers in C`"}} c/operators -.-> lab-435335{{"`Add and Subtract Numbers in C`"}} c/user_input -.-> lab-435335{{"`Add and Subtract Numbers in C`"}} c/math_functions -.-> lab-435335{{"`Add and Subtract Numbers in C`"}} end

Declare Variables and Input Values

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

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

cd ~/project
nano arithmetic.c

Now, enter the following C code:

#include <stdio.h>

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

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

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

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

    return 0;
}

Let's break down the code:

  • int num1, num2; declares two integer variables to store the input numbers
  • printf() is used to display prompts to the user
  • scanf() reads integer input from the user
  • &num1 and &num2 pass the memory addresses to store the input values

Compile and run the program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Enter the first number: 10
Enter the second number: 5
First number: 10
Second number: 5

Perform Addition and Subtraction

In this step, you'll build upon the previous step by adding arithmetic operations to your C program. We'll modify the existing arithmetic.c file to perform addition and subtraction with the input numbers.

Open the arithmetic.c file:

cd ~/project
nano arithmetic.c

Update the code with addition and subtraction operations:

#include <stdio.h>

int main() {
    // Declare variables to store two numbers and results
    int num1, num2, sum, difference;

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

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

    // Perform addition
    sum = num1 + num2;

    // Perform subtraction
    difference = num1 - num2;

    // Print the results
    printf("Addition: %d + %d = %d\n", num1, num2, sum);
    printf("Subtraction: %d - %d = %d\n", num1, num2, difference);

    return 0;
}

Compile and run the updated program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Enter the first number: 10
Enter the second number: 5
Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5

Key changes in the code:

  • Added sum and difference variables to store arithmetic results
  • Used + operator for addition
  • Used - operator for subtraction
  • Added print statements to display arithmetic operations and results

Print the Results

In this step, you'll enhance the output formatting of your arithmetic program to make the results more readable and informative. We'll modify the arithmetic.c file to improve result presentation.

Open the arithmetic.c file:

cd ~/project
nano arithmetic.c

Update the code with improved result printing:

#include <stdio.h>

int main() {
    // Declare variables to store two numbers and results
    int num1, num2, sum, difference;

    // Print a welcome message
    printf("Simple Arithmetic Calculator\n");
    printf("----------------------------\n");

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

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

    // Perform addition
    sum = num1 + num2;

    // Perform subtraction
    difference = num1 - num2;

    // Print formatted results
    printf("\nCalculation Results:\n");
    printf("-------------------\n");
    printf("First Number:  %d\n", num1);
    printf("Second Number: %d\n", num2);
    printf("Addition:      %d + %d = %d\n", num1, num2, sum);
    printf("Subtraction:   %d - %d = %d\n", num1, num2, difference);

    return 0;
}

Compile and run the updated program:

gcc arithmetic.c -o arithmetic
./arithmetic

Example output:

Simple Arithmetic Calculator
----------------------------
Enter the first number: 15
Enter the second number: 7

Calculation Results:
-------------------
First Number:  15
Second Number: 7
Addition:      15 + 7 = 22
Subtraction:   15 - 7 = 8

Key improvements:

  • Added welcome and results headers
  • Improved formatting with aligned output
  • Included more descriptive labels
  • Added blank lines for better readability

Summary

In this lab, you learned how to declare variables and input values to perform arithmetic operations in C. You created a simple program that allows users to enter two numbers and then performed addition and subtraction on those numbers. The key steps covered were: 1) declaring variables to store the input numbers, 2) prompting the user to enter the numbers, and 3) implementing the arithmetic operations and printing the results.

The program demonstrates the basic syntax for declaring variables, using printf() and scanf() functions, and performing basic arithmetic in C. By following these steps, you gained hands-on experience with the fundamental concepts of variable declaration, user input, and arithmetic operations in C programming.

Other C Tutorials you may like