Basic Arithmetic Operations

CCBeginner
Practice Now

Introduction

Arithmetic operations such as addition, subtraction, multiplication, and division are fundamental operations in programming. In this lab, we will show you how to write a C program to perform basic arithmetic operations and how the C language handles typecasting.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123207{{"`Basic Arithmetic Operations`"}} c/variables -.-> lab-123207{{"`Basic Arithmetic Operations`"}} c/data_types -.-> lab-123207{{"`Basic Arithmetic Operations`"}} c/operators -.-> lab-123207{{"`Basic Arithmetic Operations`"}} c/user_input -.-> lab-123207{{"`Basic Arithmetic Operations`"}} c/memory_address -.-> lab-123207{{"`Basic Arithmetic Operations`"}} c/pointers -.-> lab-123207{{"`Basic Arithmetic Operations`"}} c/function_declaration -.-> lab-123207{{"`Basic Arithmetic Operations`"}} end

Set up the Basic Structure

Before we start, make sure you have a C compiler installed on your machine. Open your text editor and create a new file named "main.c" in the ~/project/ directory.

Let's start by including the stdio.h header file and writing the main function:

#include<stdio.h>
int main()
{
    return 0;
}

Ask for User Input

Ask the user to input two integers using the scanf() function. Declare the variables to store these integers as int a and int b.

#include<stdio.h>
int main()
{
    int a, b;
    printf("Enter two integers: ");
    scanf("%d%d", &a, &b);

    return 0;
}

Perform Basic Arithmetic Operations Without Typecasting

Now, let's perform basic arithmetic operations without typecasting. Declare variables to store the results of the arithmetic operations as add, subtract, multiply, and divide.

#include<stdio.h>
int main()
{
    int a, b, add, subtract, multiply;
    float divide;
    printf("Enter two integers: ");
    scanf("%d%d", &a, &b);

    add = a + b;
    subtract = a - b;
    multiply = a * b;
    divide = a / b;

    printf("Addition of the numbers = %d\n", add);
    printf("Subtraction of 2nd number from 1st = %d\n", subtract);
    printf("Multiplication of the numbers = %d\n", multiply);
    printf("Dividing 1st number from 2nd = %f\n", divide);
    return 0;
}

Perform Basic Arithmetic Operations With Typecasting

C language handles typecasting implicitly. However, we can handle it explicitly in our programs too. Let's write a C program that performs basic arithmetic operations with typecasting.

Declare variables to store the results of the arithmetic operations as add, subtract, multiply, divide, and remainder.

#include<stdio.h>
int main()
{
    int a, b, add, subtract, multiply, remainder;
    float divide;
    printf("Enter two integers: ");
    scanf("%d%d", &a, &b);

    add = a + b;
    subtract = a - b;
    multiply = a * b;
    divide = a / (float)b;
    remainder = a % b;

    printf("Addition of the numbers = %d\n", add);
    printf("Subtraction of 2nd number from 1st = %d\n", subtract);
    printf("Multiplication of the numbers = %d\n", multiply);
    printf("Dividing 1st number from 2nd = %f\n", divide);
    printf("Remainder on Dividing 1st number by 2nd is %d\n", remainder);
    return 0;
}

Compile and Run the Program

Save the main.c file. Open your terminal in the ~/project/ directory where you saved your file and compile the program using the following command:

gcc -o main main.c

This will create an executable file named main. Run the program using the following command:

./main

Test the Program

Test the program by inputting two integers and check if the program is performing the arithmetic operations as expected.

Full Code

#include<stdio.h>
int main()
{
    int a, b, add, subtract, multiply, remainder;
    float divide;
    printf("Enter two integers: ");
    scanf("%d%d", &a, &b);

    add = a + b;
    subtract = a - b;
    multiply = a * b;
    divide = a / (float)b;
    remainder = a % b;

    printf("Addition of the numbers = %d\n", add);
    printf("Subtraction of 2nd number from 1st = %d\n", subtract);
    printf("Multiplication of the numbers = %d\n", multiply);
    printf("Dividing 1st number from 2nd = %f\n", divide);
    printf("Remainder on Dividing 1st number by 2nd is %d\n", remainder);
    return 0;
}

Summary

In this lab, we have learned how to write a C program to perform basic arithmetic operations and how the C language handles typecasting. We have demonstrated how to perform basic arithmetic operations with and without typecasting. Finally, we have compiled and run the program to test its functionalities.

Other C Tutorials you may like