## C Programming Tutorial – While Loop

In C programming, we like to repeat some instructions over and over again with or without updated information. C language provide loops for this purpose. The type of loop you want to use depends on how you want to use the loop.

## Digital Design – Binary-Coded Decimal

In the previous lesson, we learned about binary codes and how they are used for representing a distinct discrete element of information using 1s and 0s.

#### Binary Coded Decimal Overview

People use decimal system for everything and it’s easier for them to use decimal numbers on computer than binary numbers. But binary is what computer understands.

So we need a system to convert decimal to binary and perform calculation using binary numbers and convert the output back to decimal for users.

This binary representation of decimal number is called the binary coded decimal code or BCD code.

#### Binary-Coded Decimal (BCD code)

The binary coded decimal is a 4-bit code and each 4-bit code is represents one decimal digit from 0 to 9. See the table below,

 Decimal Digit Binary Coded Decimal 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001

But there are 16 codes because 24 = 16 combinations. The codes from 1010 to 1111 does not matter and are not used in BCD.

For example

The binary and BCD value of 128 is given below for your understanding.

 Binary number for 12810 BCD Code for 12810 1000 0000 0001 0010 1000

#### Important points to remember

• Decimal number is 0, 1, 2, 3, so on, and BCD code is 0000, 0001, 0011… It means they are same.
• Binary number is not BCD, there is a difference.
• BCD coded decimal numbers need more bits. See the example above.

When two BCD numbers are added then two things happens

1. Sum is less than 1010 = decimal 10
2. Sum is equal to or more than decimal 10

Suppose you add following BCD numbers

 4 + 3 0100 0011 0111 = 7

The result is a BCD sum and less than 1010.

First Case – when BCD sum does not have a carry

Consider another example,

 8 + 4 1000 0100 1100 = 12

The BCD does not allow 1100 and we need 12 as answer in BCD. The first digit is 1 and second digit is 2.

There are 16 codes in BCD and we are using only 10 of them.

1010 + 0110 = 1 carry + 0000 = 0001 0000 = 10 in BCD

1011 + 0110 = 1 carry + 0001 = 0001 0001 = 11 in BCD

1100 + 0110 = 1 carry + 0010 = 0001 0010 = 12 in BCD

1101 + 0110 = 1 carry + 0011 = 0001 0011 = 13 in BCD

1110 + 0110 = 1 carry + 0100 = 0001 0100 = 14 in BCD

1111 + 0110 = 1 carry + 1000 = 0001 0101 = 15 in BCD

Then whenever, you get an invalid BCD sum, add a 0110 = 6 to the result and you will get the correct BCD sum.

Second Case – when BCD sum has a carry

We not look at another case of BCD sum when the result appears to be correct but it has a carry.

8 + 9 = 1000

+ 1001

============

1 0001

In this case, we still add 0110 = 6 to the 4 least significant bits.

1 0001

0110

============

1 0111 = 1 carry + 0111 = > 0001 0111 = 1710

In this example, we will add two decimal numbers,

214 + 389 = 603

0010 + 1 0001 + 1   0100 = 214

+0011        1000          1001= 389

======================================

0110        1010           1101

0110           0110

=====================================

0110        0000           0011 = 603

### Bibliography

John.F.Wakerly. 2008. Digital Design: Principles And Practices, 4/E. Pearson Education, India.

Mano, M. Morris. 1984. Digital Design. Pearson.

## Digital Design – Binary Codes

Binary codes are used to represent distinct discrete element of information. They are patterns of 1s and 0s for computer to understand information other than binary numbers.

The discrete elements of information is not only binary numbers, but also, other types of information such as decimal numbers, etc.

#### Binary Code

Suppose we have a n-bit code then there are 2n combination of codes consists of 1s and 0s.

For example,

3-bit code has 23 = 8 codes

 Bit Combination Codes 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111

There are 8 combinations, but the bit combination has value is between 0 to 2n – 1. A 3-bit code has bit combination from 0 to 23 – 1 = 7.

#### Minimum bits required

For 16 code combinations, you need minimum of 4 bits, n = 4 bits, so that 24 = 16.

For 8 codes, you need minimum 3 bits like in example above,

n = 3 bits so that 23 = 8.

#### Maximum bit for Binary Code

There is no restriction on number of bits that you can use for a binary code.

For example

10 decimal numbers – 0,1,2,3,4,5,6,7,8,9 can be represented using 4-bits and can also be represented using 10-bits. Let’s check this for first 5 decimal numbers.

 Decimal value 4-bit code 10-bit code 0 0000 0000000000 1 0001 0000000010 2 0010 0000000100 3 0011 0000001000 4 0100 0000010000

A 4-bit code is binary representation of decimal and 10-bit code uses placeholder for each decimal number. Here the code – 0000010000 does not mean 25 = 16, but it means 5th position from right is a 1, therefore, decimal number = 4.

### Bibliography

John.F.Wakerly. 2008. Digital Design: Principles And Practices, 4/E. Pearson Education, India.

Mano, M. Morris. 1984. Digital Design. Pearson.

NATARAJAN, ANANDA. 2015. Digital Design. PHI Learning Pvt. Ltd.

## Digital Design – Complements

In the previous lesson, we learned about number systems – binary, decimal, octal and hexadecimal number systems in detail.

Complements are very helpful in performing subtraction and save computing time and lead to simple circuits.

Before we move to subtraction, let’s understand what complement is all about. In simple words, if there is group with have R items, and you took S items from the group, then you need the complement R-S items to complete the group.

These R-S items are called the complements.

In a number system, there is a fixed range of numbers and when you pick any number from the system, you have a complement for that number in the number system.

### Types of Complements

There are two types of complements.

1. Diminished Radix Complement or r-1’s complement.
2. Radix Complement or r’s complement.

### Diminished Radix Complement [r-1’s complement]

Suppose a number N is given

N = Number

r = base of the number

n = Number of digits in the number.

The r-1’s complement of the number is given by the formula.

(rn – 1) – N

To find radix complement of a number N where

N = Number

r. = base of the number

n = Number of digits in the number.

The r’s complement of the number is given as

rn – N

or

r-1’s complement + 1 => [( rn -1) – N] + 1

### Examples – Diminished Radix Complements

Problem1:

Find 9’s complement of 258.

Solution:

N = 258

r = 10

n = 3

We use the formula for r-1’s complement.

(rn – 1) – N

(103 – 1) – 258

999 – 258

The 9’s complement of 258 is 741.

Problem2:

Find 1’s complement for 1101.

Solution:

In the given problem,

N = 1101

r = 2

n = 4

The formula for r-1’s complement where r = 2 is (rn – 1) – N.

(24 – 1) – 1101

Note: Binary equivalent for 24 = 10000.

(10000 – 1)1101

1111 – 1101

The 1’s complement for 1101 is 0010.

Method2

The second method to find the r-1’s complement is to subtract each digit in the number by r-1.

In our case, r = 2 so r – 1 = 1

Given that the 1’s complement of binary number is

1 – 1 = 0

1 – 1 = 0

1 – 0 = 1

1 – 1 = 0

Problem3:

Find 7’s complement of octal number 234.

Solution:

The range of digits for octal number is 0-to-7. Subtract each digit with 7 will give r-1’s complement for octal.

7 – 2 = 5

7 – 3 = 4

7 – 4 = 3

The r-1’s complement is 543.

There are two methods to find the r’s complement of a number.

Method 1: rn – N

Method 2: r-1’s complement + 1

Problem4:

Find 10’s complement for 432.

Solution:

Given that

N = 432

r = 10

n = 3

Method 1:

First we will find the 10’s complement using rn – N.

rn – N = 1000 – 432 = 568

Therefore,

10’s complement of 432 is 568.

Method 2:

By using method 2, you need to find the 9’s complement and then add 1 to the result to get 10’s complement.

Step1: Subtract each digit by 9 to get 9’s complement.

9 – 4 = 5

9 – 3 = 6

9 – 2 = 7

Step2: Add 1 to the result.

567 + 1

=568

568 is the 10’s complement of 432.

Problem5:

Find 2’s complement of the binary number 1001.

Solution:

Method 1:

To find 2’s complement using method 1 use the formula rn – N.

Given that

N = 1001

r = 2

n = 4

rn – N = 10000 – 1001

10000

– 1001

0111

The 2’s complement for 1001 is 0111.

Method 2:

To use second method, find the 1’s complement for 1001 and then add 1 to the result.

Step1: find the 1’s complement of 1001.

1001 -> 0110 (1’s complement)

0110 + 1 => 0111

Therefore,

0111 is the 2’s complement of 0110.

### Bibliography

John.F.Wakerly. 2008. Digital Design: Principles And Practices, 4/E. Pearson Education, India.

Mano, M. Morris. 1984. Digital Design. Pearson.

## Digital Design – Number Systems

Numbers are everywhere and used in many places – financial institution, statistics, engineering and so on. Without number numbers our daily life would be difficult and difficult to imagine.

Humans use a number system called the decimal number system and everyone can easily understand this system. You go to a store to buy something, you need a number system to count your money. Even a small thing such as making a list of its need numbers.

### What is a number system?

A number belong to a number system with

• A base ‘r’
• Coefficient ‘a’ that is between 0 to r-1.

Examples

Decimal with r=10 and coefficient a = 0 to 9

Binary with r=2 and coefficient a = 0 and 1

Octal with r= 8 and coefficient a = 0 to 7

Hexadecimal with r=16 and coefficient a = 0 to 9, A, B, C, D, E, F

### Converting from any number system to decimal system

If the base ‘r’ and coefficients are given then any number can be converted to decimal system using following

an * rn + an-1 * rn-1 + … + a1 * r + a0 + a-1 * r-1 + a-2 * r-2 + a-n-1 * r-n-1 + rn

Example

To convert binary number 1101 into decimal number, do following

1011 = 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 2^0

= 8 + 0 + 2 + 1

= 11

11 is decimal equivalent of 1101

Example

Convert 1101.0112 into decimal equivalent.

Solution:

Integer part -> 1101

1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0

8 + 4 + 0 + 1

13

Decimal part -> .011

0 * 2^-1 + 1 * 2^-2 + 1 * 2^-3

0 + 1/4 + 1/8

3/8

Answer = 13 + 3/8 = (13 * 8 + 3)/8

= 107/8

= 13.37

### Conversions from Decimal to Other Number Systems

The decimal number system have a base r = 10 and coefficients ranging from 0 –to- 9.

Decimal to Binary

To convert from decimal from binary, divide the decimal number repeatedly until no longer possible to divide. Each time you get a quotient and remainder, divide the quotient and note the remainder. The remainders of this division put together in reverse order is the answer. See the example below.

For fractional part you need to multiply the decimal fraction with base r repeatedly until you reach 0 or close to zero for the fractional part. When you multiply a fraction with base r, you get two thing – an integer and a fraction.

If the fraction has not reached zero or real close to zero, multiply it with base in next iteration and check the integer and fraction part.

The integer part put together is the binary equivalent for the decimal fraction. See the example below

For example

Problem:

Convert 25.13 to binary equivalent number.

Solution:
25/2 quotient = 12, remainder = 1

2/2 quotient = 6, remainder = 0

6/2 quotient = 3, remainder = 0

3/2 quotient = 1, remainder = 1

1/2 quotient = 0, remainder = 1
Now we find the fractional part binary equivalent.

0.13 * 2 = 0.26 => 0 + .26

0.26 * 2 = 0.52 => 0 + .52

0.52 * 2 = 1.04 => 1 + .04

Answer: – 11001 + .001 = 11001.001
Decimal to Octal

The decimal to octal conversion is done in the same way as decimal to binary except the base r = 8.

For example

Problem:

Convert 23510 to its octal equivalent.

Solution:
235/8 quotient = 29, remainder = 3

29/8 quotient = 3, remainder = 5

3/8 quotient = 0, remainder = 3

The hexadecimal number has a base r = 16 and we need to divide decimal number with base r to get the hexadecimal equivalent, but hexadecimal digits 10 – 15 are A – F.

2344/16 quotient = 146, remainder = 8

146/16 quotient = 9, remainder = 2

2/16 quotient = 0, remainder = 2

### Conversions to Binary

Octal to Binary

To convert octal to binary, convert each digit of octal number to its binary equivalent, but you are only allowed to use 3 bit positions because 111 = 7 which is the maximum range of octal coefficient.

For example

Problem:

Convert 256 into its binary equivalent.

Solution:

2 -> 010

5 -> 101

6 -> 110

Answer: The binary equivalent is 010 101 110.

We use 4 bits to convert each digit in a hexadecimal number to get the binary equivalent.

This is because 1111 = 15 = F in decimal, the maximal value allowed in hexadecimal number.

For example

Problem:

Convert 2CD3 into binary equivalent.

Solution:

2 C D 3

0010 1100 1101 0011

The binary equivalent of 2CD3 is 0010 1100 1101 0011.

### Binary to Other Number Systems

Binary to Octal

To convert from binary to octal use 3 bit position and convert it into its decimal equivalent.

1. For integer part 3 bit from left to right
2. For fractional part 3 bit from right to left

This will give you the octal equivalent of binary number.

For example,

1001.0101 => 001 110 . 010 100

1 6 . 2 1

Answer: – The octal equivalent is 16.21.

Group the given binary number into group of four and then convert the each of the group into decimal equivalent to get the hexadecimal digit and group all the digits found to get the hexadecimal number.

For example

Problem:

Convert the given binary number 110101011100 into hexadecimal equivalent.

Solution:
Given binary number can be separated into group of four.

110101011100 => 1101 0101 1100

13 5 12

D 5 C

Therefore, the hexadecimal equivalent number is D5C.

### Other Number Systems

We use a very simple procedure to convert octal to hexadecimal number. You can do this two easy steps.

1. Convert Octal to Binary number.
2. Convert the Binary number obtained into hexadecimal.

In the previous section, we learned about converting octal to binary and binary into hexadecimal. In this section, you have to use that knowledge in converting octal to hexadecimal.

For example,

Problem:

Convert the given octal number 234 into Hexadecimal equivalent.

Solution:

Step1: Convert the octal into binary number.

2 3 4

010 011 100

Step2: Convert the binary number into hexadecimal number.

010011100

Group the binary number into groups of four bits from left to right.

0000 1001 1100

0 9 12

Convert the result into hexadecimal number.

Therefore, the hexadecimal equivalent is 09C.

If you reverse the process given in octal to hexadecimal conversion, you will get an octal equivalent for hexadecimal number.

### Bibliography

John.F.Wakerly. 2008. Digital Design: Principles And Practices, 4/E. Pearson Education, India.

Mano, M. Morris. 1984. Digital Design. Pearson.

NATARAJAN, ANANDA. 2015. Digital Design. PHI Learning Pvt. Ltd.

## C Programming Tutorial – Flow Control – II

In the previous lesson, we learned about the flow control structure. We learned about the if-else structures and nested if structures. In this lesson, we will learn about another flow control feature – switch-case.

### If-else Construct

Consider a C program for calculator, the user is given a menu with options to choose from.

2. Subtract
3. Multiply
4. Divide

We can write a program using if-else construct for the above problem.

``````#include <stdio.h>
int main()
{
int choice;
int a,b,c;
a = 10;
b = 20;
if (choice == 1)
c = a+b;
if (choice == 2)
c = a – b;
if (choice == 3)
c = a * b;
else if(choice == 4)
c = a/b;
else
{
printf(“Wrong input, Try again!”);
}
getch();
return 0;
}``````

The user enter a number and it is matched with if-else blocks and whenever there is a match that block is executed.

The problem with these kind of construct is that when the menu if large, then the if-else construct will be difficult to manage. Also, there are many comparisons – each if block is tested.

### Switch-Case

The switch-case is suitable for menu driven C programs. Whenever the program is executed, a menu with number of choices like the example below is presented to the user.

/* Calculator Application in C */

2. Subtract
3. Multiply
4. Divide

The switch block look like the following in program source code. The switch block accepts a number as user choice.

When the user enters a number next to an option and the Switch accepts the choice(number).

``````int choice;
Switch (choice)
{
}``````

The switch () matches the user choice with a list of cases. Each of the cases have number associated with them followed by a colon.

``````Case 2:
Statement1;
break;
Case 3:
Statement2;
Statement3;
break;``````

When the user choice and the case number matches, the statements from the case is executed and terminated by a break; statement.

The entire switch-case block look like following

``````int choice;
scanf(“%d”,&choice);
Switch(choice)
{
Case 1:
Do something;
break;
Case 2:
Do something;
break;
default:
Do something;
break;
}``````

Let’s create an example program using the Switch-Case. We shall write the Calculator program using the Switch-Case.

### Program Code

``````/* C Program for a Calculator using Switch-Case */
#include <stdio.h>
int main()
{
int choice;
int a, b, c;
a = 10;
b = 20;
scanf (“%d”, &choice);
Switch (choice)
{
case 1:
c = a + b;
break;
case 2:
c = a - b;
break;
case 3:
c = a * b;
break;
case 4:
c = a / b;
break;
default:
printf (“Wrong choice, try again!”);
break;
}
printf (“Result = %d\n”, c);
getch();
return 0;
}``````

Output

The output of the program is given below.

### Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.

## HTML Text

The HTML text elements are to display text information in an HTML page. Every text document (e.g., MS Word) have headings, subheadings, and text contents in the form of sentence, paragraphs.

HTML has the following headings and sub-headings where <h1> being the largest and <h6> being the smallest heading. The size and font type of each can be modified with CSS.

<h2>

<h3>

<h4>

<h5>

### Text Contents

The text contents are paragraphs with sentences and quotes. In HTML, following tags are used

```<p> - for paragraph
<blockquote> or <q> - for quote```

### Formatting Text Contents

HTML elements other than headings and paragraphs are used to format the text contents inside an HTML documents.

Acronyms and Abbreviations

The text content sometimes contains acronyms or abbreviations. It is short form of a longer name or title. The HTML uses two tags to display the full title of the abbreviation.

<acronym> …. <acronym>

<abbr> … </abbr>

Example-1

```<acronym title=”United Nations Environment Programme”>UNEP</acronym>
<abbr title=”United Nations Environment Programme”>UNEP</abbr>```

Output

UNEPUNEP

### Italic Text Contents

There are many HTML tags that shows text contents in italic, but their purpose are different. Here is a list of such HTML tags.

 HTML Tags Description Shows the text in italic. Put emphasis to a text content Definition of a term (the term in italic)
Address of the author in italic Shows a reference to a piece of work – a book, web page, journal or any text content.

Example-2

Italic text (<i>)

`<p> This paragraph contains <i>Italic </i> word.</p>`

Output

This paragraph contains italic word.

Emphasis (<em>)

`<p>In this paragraph, a word is <em>important</em> so it is emphasized.</p>`

Output

In this paragraph, a word is important so it is emphasized.

Definition (<dfn>)

`<p> <dfn>Orange<dfn> is a citrus fruit.</p>`

Output

Orange is a citrus fruit.

```<address>
<p>First Name : John</p>
<p>Last Name: Peterson</p>
<p>City: London</p>
<p>Street: 101, Baker street</p>
<p>Phone: 111-3344<p>
<p>Email: <a href=”mailto:[email protected]”> john</a></p>

Output

First Name : John

Last Name: Peterson

City: London

Street: 101, Baker street

Phone: 111-3344

Email: john

Citation (<cite>)

`<p> Mark Twain was an American writer. He wrote <cite>The Adventures of Tom Sawyer</cite> in 1876.</p>`

Output

Mark Twain was an American writer. He wrote The Adventures of Tom Sawyer in 1876.

### Bold Text Contents

Some text contents are shown in bold to put more emphasis or to show importance of the text. The HTML tags used to do this are

 HTML Tags Description Show a text in bold. Bold a text to show strong importance.

Example-3

Bold (<b>)

`<p>This is a <b>bold text</b> content.</p>`

Output

This is a bold text content.

Strong (<strong>)

`<p> This text has certain <strong>words</strong> that has strong importance.</p>`

Output

This text has certain words that has strong importance.

### Superscript & Subscript

The superscript and the subscript are very important when you want to write a scientific or mathematical equation.

To write “A squared” which is A2 and “A subscript 2” is A2 use following tags

 HTML Tags Description For subscript For superscript

Example-4

Subscript (<sub>)

`<p>A<sub>2</sub>`

Output

A2

Superscript (<sup>)

`<p>A<sup>2</sup>`

Output

A2

### Computer Code Tag

There is a tag that can be used to show computer codes.

 HTML Tag Description To display computer codes

Example-5

Computer Code (<code>)

```<p>The following is computer code.</p>
<code>#include “stdio.h”</code>```

Output

The following is computer code.

#include “stdio.h”

### Inserting and Deleting text

We want to show that a text form the document is inserted or deleted. This is possible with two HTML tags given below.

 HTML Tags Description Shows inserted text Shows deleted text

Example-6

Insert (<ins>)

`<p> This text is inserted <ins>globe</ins>and it is underlined.</p>`

Output

This text is inserted- globe and it is underlined.

Delete (<del>)

`<p>The line has a word which is <del>removed </del> or deleted and it is strike out.</p>`

Output

The line has a word which is removed or deleted and it is strike out.

### Tags not supported in HTML 5

There are text related tags which are not supported in HTML 5 and you can use them on HTML elements from CSS (Cascading Style sheet)

 HTML Tag HTML 4.01 HTML 5 Yes No Yes No
Yes No

Note: There are other HTML standards and browsers for which these tags are used differently.

## C Programming Tutorial – Logical Operators

C programming language has logical operators for logical expressions. The output of a logical operation is a Boolean value – true or false. In a logical expression you have one or more operands and each has a truth value, depending on the truth value of each operand the logical operator decides the final truth value of the logical expression.

Logical Operators

A list of logical operators in C language is given below. The minimum numbers of operands required by each of the operators is also given in the same table.

1. AND (&&)
2. OR (||)
3. NOT (!)

We shall discuss each of them in more detail now.

### AND (&&) operator

The AND operation is denoted by two ‘ampersand’ symbol. To understand the AND operation, consider the following expression,

``````A && B
Where A = (3 < 4) and B = (10 < 20)``````

The final output of the expression depends on following conditions.

A is true && B is true = A && B is true

A is false && B is true = A && B is false.

A is true && B is false = A && B is false.

A is false && B is false = A && B is false.

So, the AND is only true when both A and B are true.

### OR (||) operator

The OR operation is also useful logical operator in C language. The value of expression is true under following conditions.

A is true || B is true = A || B is true

A is false || B is true = A || B is  true

A is true || B is false = A || B is true

A is false || B is false = A || B is false

From the above example, it is clear that the OR is only true when at least A or B is true.

### NOT (!) operator

The NOT is called the negation operation. It negates the truth value of the existing expression.

For example,

if A is true , then !A is false

If A is false, then !A is true.

Note:- The !(!A) == A.

### Example – AND, OR, NOT

``````/* C Program to demonstrate use of Logical Operators */
#include <stdio.h>
int main()
{
int A, B ;
A = (5 < 10); /* True */
B = (10 < 20); /* True */
printf("\n\n\n\n\n");
/* Working of AND operation - both A and B must be true */
if(A && B)
{
printf("\tOutput AND - both A and B are true\n\n");
}
/* Operation of Not operator */
A = (2 > 10); /* False */
B = (4 < 8); /* True */
/* A && B is False */
if(!(A && B))
{
printf("\tOutput NOT - (A && B) is False so negation !(A && B) is True\n\n");
}
/* Working of OR operator - any one of A or B must be true */
if (A || B)
{
printf("\tOutput OR - Any one of A or B is true\n\n");
}
getch();
return 0;
}``````

### Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.

## C Programming Tutorial – Arithmetic Operators

The arithmetic operators in C language are used to perform basic mathematical operations. It is used in a C arithmetic expression involving at least two operands and an operator.

An expression is C statement that evaluates to a single value. You can use this value in another C expression or output to console.

For example,

``````A = 10;
B = 20;
C = A + B; /* This is an arithmetic expression involving two
Two operands and + operator */``````

In the above example, the expression A and B will be replaced with 10 + 20, when the program executes and the expression will evaluate to 30. This is assigned to variable C.

### Arithmetic Operators

A list of arithmetic operators is given below. The mathematical symbol is actual mathematical operation perform in real world. The C operator is equivalent of that mathematical operation.

 Math Symbol C Operator Description + + Addition – – Subtraction x * Multiplication ÷ / Division mod % Modulo

The addition operator perform simple addition on two numbers. It requires two operands of type – integers or real numbers.

For example,

``````sum = number1 + number2 ;
sum = 10 + 23;
sum = 12.4 + 53.33;``````

The above are valid C expressions involving addition (+) operator.

Subtraction

The subtraction is similar to addition and perform simple subtraction on integers or real numbers.

For example,

``````result = number2 – number1;
result = 23.4 – 33.55;
result = 32 – 2;``````

These are some valid examples of C expression involving the subtraction (-) operator.

Multiplication

The multiplication is the first operation performed when an arithmetic expression is evaluated. We will learn more about operator precedence in future lessons.

The multiplication operator requires at least two operands to perform the multiplication and the result can be output to console and used in another expression.

For example,

``````C = A * B;
C = 34 * 100;
result = 23.44 * 34.545;``````

The above expressions are some valid expressions involving multiplication (*) operator.

Division operator

The division requires two operands – a dividend and a divisor. The dividend is divided using divisor and result is a quotient and remainder.

It is assumed that the dividend is always greater than the divisor, but it’s not necessarily true. Sometimes, dividend is a smaller value than divisor, but this does not produce wrong results.

For example,

``````result = number2 / number1;
result = 56.33/6.55;
result = 32 /22;``````

These are some valid examples of C expression involving the division (/) operator.

The third example will result in quotient = 1 and remainder = 10, but C division operator during an integer division only output quotient, not remainder.

Modulo Operator

Since, the division operator cannot produce remainder, the modulo operator solve this problem. The modulo operator only produce the remainder when performed on a dividend and a divisor.

For example,

``````result = number2 % number1;
result = 10 % 3 ; /* result = 1 */
result = 12.44 % 2.20 /* result = 1.44 */``````

The mod operation will produce only remainder, there is no quotient or any other outputs.

### Example – Arithmetic Operators

``````/* C Program to demonstrate the Arithmetic Operators */
#include <stdio.h>
int main()
{
int number1, number2, result;
printf("\n\n\n\n\n\n");
printf(" \t\t\tEnter two numbers\n\n");
printf("\t\t\tNumber1:");
scanf("%d",&number1);
printf("\t\t\tNumber2:");
scanf("%d",&number2);
printf("\n\n");
result = number1 + number2;
/* Subtraction */
result = number1 - number2;
printf("\t\t\tSubtraction = %d\n", result);
/* Multiplication */
result = number1 * number2;
printf("\t\t\tMultiplication = %d\n", result);
/* Division */
result = number1 / number2;
printf("\t\t\tDivision = %d\n", result);
/* Modulo */
result = number1 % number2;
printf("\t\t\tModulo = %d\n", result);
getch();
return 0;
}``````

Output- Arithmetic Operators

### Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.

## C Programming Tutorial – Flow Control Structures – I

In a C program, the statements are executed sequentially – one at a time. But, not all C programs are executed in the same way. In some programs, we want to execute a part of code and ignore others.

The flow of the program must change when we meet certain criteria or not meet the criteria. This is can be achieved using flow control structure in C language. A flow control structure in a C program can change the direction or flow of the program depending on the conditions. In this way certain section of code is ignored with in a C program.

A list of flow control structure is given below.

1. If statements
2. If-else statements
3. Switch-case statements
4. Conditional statements

Relational Operators are used for setting conditions within flow control structures.

The list of relational operator with brief description is given below.

 Operator Description == Is equal to != Is not equal to <= Less than or equal to >= Greater than or equal to < Less than > Greater than

If Statements

The ‘if’ block will execute a one or more C statements when a condition is true.

The syntax for if block is as follows

``````if (condition)
{
statement 1;
}``````

There will not be any execution if the condition is false. The relational operator with the operands will evaluate to true or false.

For example,

``````if ( 5 < 10)
{
printf (“ The statement is true”);
}``````

In the above example, 5 < 10 is true by default and evaluate to true. Hence, the output will be “The statement is true”.

### Types of if blocks

In this section we will discuss types of if block that you can use without getting errors in a C program.

Single if block with no curly brackets

``````if ( condition)
statement 1;``````

Single if block with curly brackets

``````if ( condition)
{
statement 1;
}``````

Single if – else block without curly brackets

``````if( condition)
Statement 1;
else
statement 2;``````

Single if – else block with curly brackets

``````if (condition)
{
statement 1;
statement 2;
}
else
{
statement 3;
statement 4;
}``````

Single if – else if – else block with curly brackets

``````if ( condition)
{
statement 1;
statement 2;
}
else if ( condition 2)
{
statement 3;
statement 4;
}
else {
statement 5;
}``````

In the above block there are two conditions that the program need to meet but it starts from the condition 1 and if the condition 1 is not met , checks for condition 2 and if that is not met either then execute the statement 5.

It is a good practice to use the curly bracket to avoid confusion. The only time curly brackets are allowed is when the number of statement is one.

The brackets show the boundaries for each block and brings clarity and also very helpful in managing codes when the program become very large.

Nested If Blocks

The nested block are blocks within another block and they are executed first whenever the block is executed.

The following is an example of nested block.

``````if ( condition 1)
{
statement 1;
if( condition 2)
{
statement 2;
statement 3;
}
}``````

### Nested Blocks

In the example above, statement 2 and statement 3 are executed first when the condition 2 is true.

The nested block will not executed if the condition 1 is false in the parent block.

The types of nested block is given below.

Nested if block with curly brackets

``````if ( condition 1)
{
if( condition 2)
{
statement 1;
statement 2;
}
}``````

Nested if block with no curly brackets

``````if ( condition 1)
{
if ( condition 2)
statement 1;
statement 2;
}``````

Nested if – else block with curly brackets

``````if (condition 1)
{
if (condition 2)
{
statement 1;
statement 2;
}
else
{
statement 3;
statement 4;
}
}``````

Nested if-else without curly brackets

``````if ( condition 1)
{
if( condition 2)
statement 1;
statement 2;
else
statement 3;
statement 4;
}``````

Second Level nested ‘if’ block with curly brackets

``````if( condition 1)
{
if (condition)
{
statement 1;
if (condition 3)
{
statement 2;
statement 3;
}
}
else
{
statement 4;
}``````

### Flowchart – Symbol

In a C program flowchart, the flow control such as if block is denoted by a diamond shape.

### Examples – if – else and nested if blocks

In this section, an example of simple if block, if-else block and nested if block is given to demonstrate the usage of flow control structure in a C programs.

The example is written using Dev-C++ 4.9.9.2 compiler installed on a Windows 7 64-bit PC.

``````#include <stdio.h>
int main()
{
int number1, number2, number3, sum;
number1 = 20;
number2 = 40;
number3 = 50;
sum = 0;
sum = number1 + number2;
/* The ‘if’ block */
if(number3 < 60)
{
printf(“The sum is %d\n”, sum);
}
/* The ‘if-else’ block */
if(sum == 70)
{
printf(“The sum is correct\n”);
}
else
{
printf(“The sum is incorrect, try again!\n”);
}
/* nested if block */
if ( sum > 50)
{
if( number3 > 40)
{
sum = sum + number3;
printf(“The new sum is %d\n”,sum);
}
}
system(“pause”);
return 0;
}``````

### Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.