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.
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.
π Practice Now: Basic Arithmetic Operations
Want to Learn More?
- π³ Learn the latest C Skill Trees
- π Read More C Tutorials
- π¬ Join our Discord or tweet us @WeAreLabEx
Top comments (0)