C program to perform basic arithmetic addition, subtraction, multiplication and division

C program to perform basic arithmetic operations which are an addition, subtraction, multiplication, and division of two numbers. Numbers are assumed to be integers and will be entered by the user.

C programming code to perform basic arithmetic:

In this program calculate all basic arithmetic operation based on user’s input values and display the result of those operations as output.

#include <stdio.h>
int main()
{
   int a, b, add, subtract, multiply;
   float divide;                     //**divide variable declare as float because 
                                     //* the result may come in fraction.
   printf("Enter two integers\n");
   scanf("%d%d", &a, &b);
   add = a + b;
   subtract = a - b;
   multiply = a * b;
   divide = a / (float)b;   //typecasting
   printf("Sum = %d\n",add);
   printf("Difference = %d\n",subtract);
   printf("Multiplication = %d\n",multiply);
   printf("Division = %.2f\n",divide);
   return 0;
}

Output of program:

In C language when we divide two integers, we get integer result for example 5/2 evaluates to 2. As a general rule integer/integer = integer and float/integer = float or integer/float = float. So we convert denominator to float in our program, you may also write float in the numerator. This explicit conversion is known as typecasting.

C program that takes two number from the user then performs basic arithmetic as user’s choice.

In this program, we use conditional if function to perform basic arithmetic as user’s choice. After putting two values the user can choose one of four basic arithmetic operators to perform the operation.

#include <stdio.h>
int main()
{
   int a, b, add, subtract, multiply;
   float divide;                     //**divide variable declare as float because 
   char c;                                  //* the result may come in fraction.
   printf("Enter two integers\n");
   scanf("%d%d", &a, &b);
   printf("Enter your Choice (+,-,*,/) :");
   c=getch();
   if (c=='+')
   {
       add = a + b;
       printf("Sum = %d\n",add);
   }elseif(c=='-')
   {
       subtract = a - b;
       printf("Difference = %d\n",subtract);
   }elseif(c=='*')
   {
       multiply = a * b;
       printf("Multiplication = %d\n",multiply);
   }elseif(c=='/')
   {
       divide = a / (float)b;   //typecasting
       printf("Division = %.2f\n",divide);
   }
   getch();
   return 0;
}

output:

basic arithmetic

C Program to find area and circumference of circle:

In this program, we have to calculate the area and circumference of the circle. We have following 2 formulas for finding circumference and area of the circle. Area of Circle = PI * R * R and Circumference of Circle = 2 * PI * R

 #include<stdio.h>

int main() {

 int rad;
 float PI = 3.14, area, ci;
 clrscr();

 printf("\nEnter radius of circle: ");
 scanf("%d", &rad);

 area = PI * rad * rad;
 printf("\nArea of circle : %f ", area);

 ci = 2 * PI * rad;
 printf("\nCircumference : %f ", ci);
 getch();

 return (0);
}

Output:

area of circle

Program to convert temperature from degree centigrade to Fahrenheit:

We know that the temperature T in degrees Fahrenheit (°F) is equal to the temperature T in degrees Celsius (°C) times 9/5 plus 32:

T(°F) = T(°C) × 9/5 + 32

or

T(°F) = T(°C) × 1.8 + 32

#include<stdio.h>
 
int main() {
 float celsius, fahrenheit;
 clrscr();

 printf("\nEnter temp in Celsius : ");
 scanf("%f", &celsius);

 fahrenheit = (1.8 * celsius) + 32;
 printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
 getch();

 return (0);
}

Output: As we know that in -40° both Centigrade and Fahrenheit are equal.

centigrade to Fahrenheit

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.