Simple Calculator Program using Switch Case: C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).

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

We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program.
Simple Calculator Application In C

Simple Calculator using Switch Case: C Program


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

YouTube Link: https://www.youtube.com/watch?v=HHuaYKKoMR4 [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.

Calculator Program using Switch Case: C Program

#include < stdio.h >

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

    printf("Enter your choice\n");
    printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
    scanf("%c", &choice);


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


    switch(choice)
    {
        case 'a': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 'b': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 'c': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 'd': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output
Enter your choice
a. Addition
b. Subtraction
c. Multiplication
d. Division
c
Enter 2 integer numbers
5
10
5 x 10 = 50

In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.

#include < stdio.h >

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

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

    if( choice > 4 )
    {
        printf("Select with in the range!\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    switch(choice)
    {
        case 1: printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 2: printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 3: printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 4: if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
10
2
10 / 2 = 5

Here we are asking user to enter 1 or 2 or 3 or 4 to choose the arithmetic operation.

#include

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

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

    printf("Enter 2 integer numbers. Format: a + b\n");
    scanf("%d %c %d", &a, &choice, &b);



    switch(choice)
    {
        case '+': printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case '-': printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case '*': printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case '/': if( b != 0)
                    printf("%d / %d = %d\n", a, b, (a/b));
                else
                    printf("Number can't be divided by 0\n");
                break;

        default: printf("You entered wrong choice\n");
                 break;
    }

    return 0;
}

Output:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Enter 2 integer numbers. Format: a + b
50 + 60
50 + 60 = 110

Here we ask the user to enter values as well as the operation to be performed. Operands and operator.

Note 1: There is no need of curly braces inside case.
Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed.
Note 3: default case is optional. And the code inside it executes only when non of the cases match.
Note 4: Here switch, case, break, default are all keywords / reserve words.

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

C Program To Check Leap Year

C Program to Find if a given Year is a Leap Year or Not.

Leap Year Logic

Leap Year
1. If a year is a century year(years ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Not Leap Year
1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year.
2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.

Leap Year Program: C Programming


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

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


In this video tutorial: inside if block we first check if the entered number is a century year. If yes, then we see if it’s perfectly divisible by 400. If true, then its a leap year. If not, its not a leap year.

If the control shifts to else block then the entered year is not a century year. So we check if the year is perfectly divisible by 4. If yes, then it’s a leap year. If not, it’s not a leap year.

Source Code For Leap Year Program: C Programming

 
#include < stdio.h >

int main()
{
    int year;

    printf("Enter the year\n");
    scanf("%d", &year);

    if(year % 100 == 0)
    {
        if(year % 400 == 0)
        {
            printf("%d is leap year!\n", year);
        }
        else
        {
            printf("%d is not leap year!\n", year);
        }
    }
    else
    {
        if(year % 4 == 0)
        {
            printf("%d is leap year!\n", year);
        }
        else
        {
            printf("%d is not leap year!\n", year);
        }
    }

    return 0;
}

Output 1:
Enter the year
2019
2019 is not leap year!

Output 2:
Enter the year
2020
2020 is leap year!

Output 3:
Enter the year
2000
2000 is leap year!

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

You can run the program and input any of the above leap year and check for the output.

This program is also a perfect example for nested if else.

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

Roots of Quadratic Equation: C

Quadratic Equations are of the form ax2 + bx + c = 0. To find roots(root1 and root2) of such an equation, we need to use the formula

quadratic-equation


Find Roots of Quadratic Equation: C Program


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

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


To calculate the roots of a quadratic equation in a C program, we need to break down the formula and calculate smaller parts of it and then combine to get the actual solution.

So lets calculate square root of b2 – 4 * a * c and store it in variable root_part. Also store 2 * a in variable denom. Now calculate ( – b + root_part ) / denom and store it in root1 and ( – b – root_part ) / denom in root2. Output the values of root1 and root2 to the console window.

Calculating Roots of Quadratic Equation In C

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

int main()
{
    float a, b, c;
    float root1, root2;
    float root_part, denom;

    printf("Enter values of a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if(a == 0)
    {
        printf("If a is zero, equation becomes linear and not quadratic\n");
        printf("Please enter non-zero number for a\n");
    }
    else
    {
        root_part = sqrt(b * b - 4 * a * c);
        denom     = 2 * a;

        root1     = ( - b + root_part ) / denom;
        root2     = ( - b - root_part ) / denom;

        printf("Root1 = %f\nRoot2 = %f", root1, root2);
    }

    return 0;
}

Output 1
Enter values of a, b and c
1
4
4
Root1 = -2.000000
Root2 = -2.000000

Output 2
Enter values of a, b and c
0
4
4
If a is zero, equation becomes linear and not quadratic
Please enter non-zero number for a

Work Space: Cross Verification of Root values
Quadratic Equation: ax2 + bx + c = 0
Let,
a = 1
b = 4
c = 4
i.e., 1x2 + 4x + 4 =0
=> 1x2 + 2x + 2x + 4 = 0
=> x ( x + 2 ) + 2 ( x + 2 ) = 0
=> ( x + 2 ) + ( x + 2 ) = 0
=> x + 2 = 0 AND x + 2 = 0
=> x = -2 AND x = -2

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

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

else if statement in C

In this program we’ll show you the usage of else-if clause in C programming language. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Related Read:
Nested if else Statement In C

Simple Logic: Student Grade

First in if‘s condition we check if the user entered percentage is above 100%. If true – in that case we let the user know that he entered wrong marks and he / she needs to re-enter the marks. Next in first else if we check if the percentage is greater than or equal to 60. Here we need not check if the percentage is less than 100, because in if clause itself we’ve checked if percentage is greater than 100, and we are checking the else if condition only because the condition in if is false. So percentage will be less than 100. Same logic goes to consecutive Grades.

Student Grade Calculation using else if clause: C Program


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

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


Note: When the condition is true, it executes its corresponding block of code and then skips checking remaining else if or else conditions.

Note: Its optional to have else in if or else if clause.

Note: We can have any number of else if statements after if.

Student Grade Calculation using else-if clause

 
#include < stdio.h >

int main()
{
    int s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subjects\n");
    scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per > 100)
        printf("You've entered wrong marks, kindly re-enter\n");
    else if(per >= 60)
        printf("Grade A\n");
    else if(per >= 50)
        printf("Grade B\n");
    else if(per >= 40)
        printf("Grade C\n");
    else
        printf("Failed!\n");

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

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