Simple Calculator Application In C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input.

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

Simple Calculator Program In C


[youtube https://www.youtube.com/watch?v=QO6pCvM3xSQ]

YouTube Link: https://www.youtube.com/watch?v=QO6pCvM3xSQ [Watch the Video In Full Screen.]


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.

If the user enters wrong choice, we ask the user to enter proper choice.

Student Grade Calculation using else-if clause

 
#include < stdio.h >

int main()
{
    int a, b, choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n\n");
    scanf("%d", &choice);

    if(choice > 4)
    {
        printf("You entered wrong choice\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    if(choice == 1)
    {
        printf("Addition of %d and %d is %d\n", a, b, (a+b));
    }
    else if(choice == 2)
    {
        printf("Subtraction of %d and %d is %d\n", a, b, (a-b));
    }
    else if(choice == 3)
    {
        printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
    }
    else if(choice == 4)
    {
        if(b != 0)
            printf("Division of %d and %d is %d\n", a, b, (a/b));
        else
            printf("Number can not be divided by 0\n");
    }
    else
    {
        printf("Please enter any of these no 1, 2, 3, 4\n");
    }


    return 0;
}

Output 1:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

1
Enter 2 integer numbers
3
2
Addition of 3 and 2 is 5

Output 2:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

2
Enter 2 integer numbers
8
3
Subtraction of 8 and 3 is 5

Output 3:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

3
Enter 2 integer numbers
5
5
Multiplication of 5 and 5 is 25

Output 4:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
10
2
Division of 10 and 2 is 5

Output 5:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
0
12
Division of 0 and 12 is 0

Output 6:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

4
Enter 2 integer numbers
10
0
Number can not be divided by 0

Output 7:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division

50
You entered wrong choice
Please enter any of these no 1, 2, 3, 4

Note: In division section(that is, choice 4) you can see nesting of if else statement. You can know more about nested if-else statements in this program: Nested if else Statement In C

Note: We can write the same program within while loop and ask the user if he wants to continue working inside calculator application. This avoids the need to exit and re-run the program. We’ll publish the code and video tutorial for the same in coming days. Stay subscribed to our YouTube Channel and blog.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Addition of 2 Numbers: C

In this video tutorial you can learn the procedure followed in C programming to add two numbers.

Related Read:

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter value of a and b\n");
    scanf("%d%d", &a, &b);

    c = a + b;
    
    printf("Addition of %d and %d is %d\n", a, b, c);

    return 0;
}

Output:
Enter value of a and b
30
20
Addition of 30 and 20 is 50

You can write same program without using third variable to calculate addition of 2 numbers, as below:

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value of a and b\n");
    scanf("%d%d", &a, &b);

    printf("Addition of %d and %d is %d\n", a, b, (a+b));

    return 0;
}

Output:
Enter value of a and b
30
20
Addition of 30 and 20 is 50

Note: Instead of int you can take float variables too. That would help in taking both integer as well as real values from the user as input.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Addition of Two Numbers: C Programming


[youtube https://www.youtube.com/watch?v=YYRSBD7HJTQ]

YouTube Link: https://www.youtube.com/watch?v=YYRSBD7HJTQ [Watch the Video In Full Screen.]


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Swap 2 numbers using Addition and Subtraction: C

In this video tutorial we shall learn how to swap two integer numbers without using a temporary variable and by simply making use of addition and subtraction.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 integer numbers for, a and b\n");
    scanf("%d %d", &a, &b);

    printf("You entered a = %d and b = %d\n", a ,b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After swapping a = %d, b = %d\n", a, b);

    return 0;
}

Output:
Enter 2 integer numbers, for a and b
30
20
You entered a = 30 and b = 20
After swapping a = 20, b = 30

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Swap 2 numbers using only Addition and Subtraction: C


[youtube https://www.youtube.com/watch?v=mm4XJ2DEVS4]

YouTube Link: https://www.youtube.com/watch?v=mm4XJ2DEVS4 [Watch the Video In Full Screen.]


Swapping 2 Numbers In C using only addition and subtraction: Logic

In our above program we are asking user to enter integer value for a and b.
If the user enters 30 and 20 for a and b respectively. Then our program executes below logic to swap the values of variable a and b.

Step 1: Add values of a and b and store it in variable a.
i.e.,
a = a + b;
50 = 30 + 20;
So a = 50;

Step 2: Now subtract value of b from value of a and store it in variable b.
i.e.,
b = a – b;
30= 50 – 20; (value of a = 50 according to Step 1)
So value of variable b is now 30.

Step 3: Now subtract value of b from value of a and store it in variable a.
i.e.,
a = a – b;
20= 50 – 30; (Value of a = 50 from Step 1, and Value of b = 30 from Step 2)

Finally value of a = 20(from step 3) and value of b = 30(from step 2).

This is how we swap the value of variable a and b by just making use of addition and subtraction in C programming language.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Swap 2 Numbers Without Using a Temporary Variable: C

Today lets learn how to swap 2 integer numbers without using a temporary variable in C. We’re using arithmetic operation in our logic. We’re using addition and subtraction in this c program to swap the 2 numbers.

 
#include < stdio.h >

int main()
{
    int x = 10, y = 5;

    printf("Before swapping x = %d and y = %d\n", x, y);

    x = x + y - ( y = x );

    printf("After swapping x = %d and y = %d\n", x, y);

    return 0;
}


Output:
Before swapping x = 10 and y = 5
After swapping x = 5 and y = 10

Swap 2 Numbers Without Using a Temporary Variable: C


[youtube https://www.youtube.com/watch?v=tTzdGK5t-X4]

YouTube Link: https://www.youtube.com/watch?v=tTzdGK5t-X4 [Watch the Video In Full Screen.]


Swapping 2 Numbers In C using addition, subtraction and assignment operators: Logic

Here we take 2 variables, x and y; We store 10 in x and 5 in y.

We add the values of x(10) and y(5) and then subtract the value returned when we assign x value to y.

Note: When we assign a value to a variable, the same value is returned.
Ex: temp = (a = 10);
In this statement, when we assign 10 to a, the whole expression returns 10 and we can store it in variable temp, for our reference.

Basic Arithmetic Operations In C

Today lets learn about basic arithmetic operations in C programming language.

What we learn in this video tutorial?

We shall learn, addition, subtraction, multiplication, division and modular division in this video tutorial. And we’ll also learn how to use pow() method present in math.h library file.

 
#include < stdio.h >
#include < math.h >

int main()
{
    int   a = 10, b = 2;
    char  m = 'A', n = 'a';

    printf("\nAddition of a and b is %d\n", (a+b));
    printf("\nSubtraction of a and b is %d\n", (a-b));
    printf("\nMultiplication of a and b is %d\n", (a*b));
    printf("\nDivision of a and b is %d\n", (a/b));
    printf("\nModular Division of a and b is %d\n", (a%b));
    printf("\n3 to the power of 2 is %f\n", pow(3,2));

    printf("\nASIIC value of A and a is %d and %d\n\n", m, n);

    return 0;
}

Output:

Addition of a and b is 12
Subtraction of a and b is 8
Multiplication of a and b is 20
Division of a and b is 5
Modular Division of a and b is 0
3 to the power of 2 is 9.000000
ASIIC value of A and a is 65 and 97

Basic Arithmetic Operations In C


[youtube https://www.youtube.com/watch?v=4mjiL9uwPCg]

YouTube Link: https://www.youtube.com/watch?v=4mjiL9uwPCg [Watch the Video In Full Screen.]


Precedence and associativity of arithmetic operators

1st priority: * / %
2nd priority: + –
3rd priority: =

When there is tie between same priority operators then we check the associativity. For example, for * and /, associativity is same. i.e., left to right.

So in the expression: c = a * b + a / b;
a * b gets executed first.

ASCII values

When you try to apply Arithmetic Operation on characters(alphabets, character digits, special characters) its correspondent ASCII value gets operated on. ASCII value of A is 65 and ASCII value of a is 97. This is the reason why capital A and small letter a are treated differently in C programming language. Under the hood, they have different ASCII values.

Note 1: All the variables and constants are called operands. All the arithmetic symbols(+, -, /, *, %) are called operators. = is called assignment operator.

Note 2: Only one variable is allowed on the left hand side of the equation or expression.
Ex:
c = a + b is valid.
a + b = c is invalid.

Note 3: You can’t store a decimal value in a integer variable. So if the result of evaluation of an expression has decimal value and you’re assigning it to an integer variable, then only the integer part gets stored. Decimal part will be discarded. So be careful while choosing the data type of variables while performing arithmetic operation.

Note 4: We need to always, explicitly mention the arithematic operators.
Ex:
c = (a + b)(a – b) throws error.
c = (a + b) * (a – b); is the correct way in C programming.

Note 5: An integer value divided by an integer value gives back an integer value. A float value divided by a float value gives back a floating point value.

Note 6: Modular division gives reminder of division.
Ex:
c = 10 / 2; gives c = 5;
c = 10 % 2; give c = 0;

Also,
c = -3 % 2; gives c = -1
c = 3 % -2; gives c = 1

Modular division doesn’t take float values as its operands.
Ex: c = 3.0 % 2.0; gives error

Note 7: math.h library file has other mathematical operations related methods like: pow(), sin(), cos(), tan(), abs(), sqrt() etc.

We’ll look more arithmetic operators as and when we encounter them in programs.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert