C Program to do all Arithmetic Operations between two Variables

C Program to do all Arithmetic Operations between two Variables

Today we are going to see how to write a program by which user can do basic arithmetic operations i.e Addition, Subtraction, Multiplication and Division of two variables. This is the most basic and easy program by which you can understand fundamentals of C language. So lets start with it.

Program:

// Arithmetic Operations between two Variables By SlashMyCode.com 
#include <stdio.h>
#include <conio.h>
//Arithmetic Operations between two Variables By SlashMyCode.com 
void main()
{
    int a,b,add,substract,multiply;
    float divide;
    clrscr();
    printf("Enter 2 Numbers:\t");
    scanf("%d%d",&a,&b);
    
    add=a+b;
    substract=a-b;
    multiply=a*b;
    divide=a/(float)b;
    
    printf("Sum=%d \n",add);
    printf("Subtraction=%d \n",substract);
    printf("Multiplication=%d \n",multiply);
    printf("Division=%f \n",divide);
    
// Arithmetic Operations between two Variables By SlashMyCode.com 
    getch();
}


Output:
C Program to do all Arithmetic Operations between two Variables

How It Works:
      First we will introduce all the elements in 'int' (Line 7) To do all operations we need two variables to be accepted, so we labeled them as 'a' and 'b' then we need to do operations like add, subtract, multiply and divide, these all things we added in 'int' except divide because the answer of division can be in decimal point therefore we added divide in 'float'(Line 8) rather then adding it in 'int'.
      Then comes 'printf'(Line 10) to display instructions for user on screen, Then to accept those 2 values entered by user we will use 'scanf'(Line 11), In scanf %d Means We want to accept integers, Here we want 2 Integers so we added %d%d and then after accepting these two values we have to store it somewhere so we use '&a' because we want that accepted value to be stored in 'a' similarly for second value in 'b'.
       We have accepted all values, now we can do the operations. Line 18 19 20 and 21 are very simple to understand. for addition we have written add=a+b, both the values will be added and stored in 'add' and then similarly for other operations. Just bit different in division, we add 'float' before 'b' because answer can be decimal point.
       We have done operations, Now we have to Display it on screen. To do so again we will use 'printf''. Whatever we add between " " that will be displayed on screen so to show answer of addition we added ("Sum=%d",add);  here %d means "Take The Value" it will take value of "add' (Which is our answer of addition), It will take value stored in 'add' because after comma (,) we wrote 'add'. Suppose if we must have written 'subtract' then it would be showing answer of subtraction in addition. We do the same to display other answers except Division, the difference is just we replace '%d' with '%f'' because answer can be in decimal point.
      Here we conclude our program, Hope you understood the working of these simple C program to do all arithmetic operations.  

Flowchart: 
arithmatic operation flowchart in c

No comments:

Post a Comment

Feel free to ask us any question regarding this post