if else statement in C

In our previous video tutorial we saw how to use the decision control instruction if. In this tutorial we shall see how we can use if else statement in division of 2 numbers example.

We illustrate the use of if else statement in this video tutorial with the help of a division of 2 numbers example program.

Division of 2 Numbers: if else construct in C

Single statement in both if and else block

 
#include < stdio.h >

int main()
{
    int a, b;

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

    if( b != 0)
        printf("Division of %d and %d is %d\n", a, b, a/b);
    else
        printf("You can't divide a number by 0\n");

    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0

Note: When condition for if is true, if block gets executed. When condition in if is false, else block code gets executed.

Multiple statements in both if and else block

 
#include < stdio.h >

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

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

    if( b != 0)
    {
        c = a / b;
        printf("Division of %d and %d is %d\n", a, b, c);        
    }
    else
    {
       printf("You can't divide a number by 0\n");
       printf("Please run the program once again and give proper value\n");
    }


    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0
Please run the program once again and give proper value

In above example we are enclosing if and else block code in curly braces as both if and else has multiple lines of code or block of code to be executed.

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

if else Construct in C: Division of 2 numbers logic


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

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

Division of 2 Numbers: C

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

 
#include < stdio.h >

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

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

    c = a / b;

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

    return 0;
}

Output:
Enter 2 numbers for division
10
5
Division of 10 and 5 is 2

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

 
#include < stdio.h >

int main()
{
    int a, b;

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

    printf("Division of %d and %d is %d\n", a, b, (a/b));

    return 0;
}

Output:
Enter 2 numbers for division
10
5
Division of 10 and 5 is 2

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.

Division by 0

What if a user enters 0 for variable b. Any number divided by 0 will give undefined return. So lets handle the scenario using Decision Control Instruction In C: IF and inequality operator.

 
#include < stdio.h >

int main()
{
    int a, b;

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

    if( b != 0 )
       printf("Division of %d and %d is %d\n", a, b, a/b);

    if( b == 0 )
       printf("You can't divide a number by 0\n");

    return 0;
}

Output 1:
Enter value for a and b, for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value for a and b, for division (a/b)
10
0
You can’t divide a number by 0

Output 3:
Enter value for a and b, for division (a/b)
0
5
Division of 0 and 5 is 0

In above program, != is inequality operator. It returns true(non-zero number) if the operands are not equal. == is equality operator. It returns true(non-zero number) if the operands are equal.

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

Division of Two Numbers: C Programming


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

YouTube Link: https://www.youtube.com/watch?v=QOhDxu0xbY8 [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 Multiplication and Division: 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 multiplication and division.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C
Swap 2 numbers using Addition and Subtraction: C

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 integer numbers\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 and b = %d\n", a ,b);

    return 0;
}

Output:
Enter 2 integer numbers
3
5
You entered a = 3 and b = 5
After swapping a = 5 and b = 3

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 Multiplication and Division: C


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

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


Swapping 2 Numbers In C using only Multiplication and Division: Logic

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

Step 1: Multiply values of a and b and store it in variable a.
i.e.,
a = a * b;
50= 10 * 5;
So a = 50;

Step 2: Now divide value of b from value of a and store it in variable b.
i.e.,
b = a / b;
10= 50 / 5; (value of a = 50 according to Step 1)
So value of variable b is now 10.

Step 3: Now divide value of b from value of a and store it in variable a.
i.e.,
a = a / b;
5= 50 / 10; (Value of a = 50 from Step 1, and Value of b = 10 from Step 2)
Now the value of a = 5;

Finally value of a = 5(from step 3) and value of b = 10(from step 2).

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

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

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