1) Base of hexadecimal number system? Answer : 16 2) Universal gate in digital logic? Answer : NAND 3) Memory type that is non-volatile? Answer : ROM 4) Basic building block of digital circuits? Answer : Gate 5) Device used for data storage in sequential circuits? Answer : Flip-flop 6) Architecture with shared memory for instructions and data? Answer : von Neumann 7) The smallest unit of data in computing? Answer : Bit 8) Unit that performs arithmetic operations in a CPU? Answer : ALU 9) Memory faster than main memory but smaller in size? Answer : Cache 10) System cycle that includes fetch, decode, and execute? Answer : Instruction 11) Type of circuit where output depends on present input only? Answer : Combinational 12) The binary equivalent of decimal 10? Answer : 1010 13) Memory used for high-speed temporary storage in a CPU? Answer : Register 14) Method of representing negative numbers in binary? Answer : Two's complement 15) Gate that inverts its input signal? Answer : NOT 16)...
Iterative Statements
Iterative statements are used to repeat the implementation of a series of statements until the specified expression becomes false. C supports three types of repetitive statements also known as looping
statements. They are
* while loop
* do–while loop
* for loop
In this part, we will discuss all these statements.
while loop
The while loop supports a mechanism to repeat one or more statements while a specific condition is true. Figure below shows the syntax and general form of a while loop.
Note that in the while loop, the condition is verified before any of the statements in the statement block is implemented. If the condition is true, only then the statements will be implemented, otherwise if the condition is false, the control will jump to statement y, that is the next statement outside the while loop block.
In the flow diagram it is clear that we require to constantly update the condition of the while loop. It is this condition which defines when the loop will end. The while loop will implement as long as the condition is true. Note that if the condition is not at all updated and the condition not at all becomes false, then the computer will run into an infinite loop which is not at allSensible. For example, the following code prints the first 10 numbers using a while loop.
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("\n %d", i);
i = i + 1; // condition updated
}
return 0;
}
Note that initially i = 1 and is less than 10, i.e., the condition is true, so in the while loop the value of i is printed and its value is incremented by 1. When i=11, the condition becomes false and the loop ends.
Write a program to calculate the sum of numbers from m to n.
#include <stdio.h>
int main()
{
int n, m, i, sum =0;
printf("\n Enter the value of m : ");
scanf("%d", &m);
i=m;
printf("\n Enter the value of n : ");
scanf("%d", &n);
while(i<=n)
{
sum = sum + i;
i = i + 1;
}
printf("\n The sum of numbers from %d to %d = %d", m, n, sum);
return 0;
}
Output
Enter the value of m : 2
Enter the value of n : 10
The sum of numbers from 2 to 10 = 54
do–while Loop
The do–while loop is same as the while loop. The only difference is that in a do–while loop, at the end of the loop condition is tested. As the test condition is calculated at the end, this means that the body of the loop gets implemented at least one time (even if the condition is false). Below figure shows the syntax and the general form of a do–while loop.
Note that the test condition is encompass in parentheses and followed by a semi-colon. The statements in the statement block are enclosed within curly brackets. The curly brackets are voluntery if there is only one statement in the body of the do–while loop.
The do–while loop continues to implement while the condition is true and when the condition becomes false, the control moves to the statement following the do–while loop.
The main disadvantage of using a do–while loop is that it always implements at least once, so even if the user gives some invalid data, the loop will implement. However, do–while loops are broadly used to print a list of options for menu-driven programs. For example, consider the following code.
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("\n %d", i);
i = i + 1;
} while(i<=10);
return 0;
}
What do you think will be the output? Yes, the code will print numbers from 1 to 10.
Write a program to calculate the average of first n numbers.
#include <stdio.h>
int main()
{
int n, i = 0, sum =0;
float avg = 0.0;
printf("\n Enter the value of n : ");
scanf("%d", &n);
do
{
sum = sum + i;
i = i + 1;
} while(i<=n);
avg = (float)sum/n;
printf("\n The sum of first %d numbers = %d",n, sum);
printf("\n The average of first %d numbers = %.2f", n, avg);
return 0;
}
Output
Enter the value of n : 20
The sum of first 20 numbers = 210
The average of first 20 numbers = 10.05
for Loop
Like the while and do–while loops, the for loop gives a method to repeat a task till a specific condition is true. The synax and general form of a for loop is given in Fig. below.
When a for loop is used, the loop variable is initialized only once. With every repetitive, the value of the loop variable is updated and the condition is verified. If the condition is true, the statement block of the loop is implemented, else the statements comprising the statement block of the for loop are skipped and the control moves to the statement following the for loop body.
In the syntax of the for loop, initialization of the loop variable permits the programmer to give it a value. Second, the condition defines that while the conditional expression is true, the loop
should continue to repeat itself. Every repetition of the loop must make the condition to exit the loop approachable. So, with every iteration, the loop variable must be updated. Updating the loop
variable may include incrementing the loop variable, decrementing the loop variable or setting it to some other value like, i +=2, where i is the loop variable.
Note that every section of the for loop is divided from the other with a semi-colon. It is possible that one of the divisions may be empty, though the semi-colons still have to be there. However,
if the condition is empty, it is calculated as true and the loop will repeat until something else stops it.
The for loop is mostly used to implement a single or a group of statements for a limited number of times. The following code shows how to print the first n numbers using a for loop.
#include <stdio.h>
int main()
{
int i, n;
printf("\n Enter the value of n :");
scanf("%d", &n);
for(i=1;i<=n;i++)
printf("\n %d", i);
return 0;
}
In the code, i is the loop variable. Initially, it is initialized with 1. Suppose the user enters 10 as the value of n. Then the condition is checked, since the condition is true as i is less than n, the statement in the for loop is executed and the value of i is printed. After every iteration, the value
of i is incremented. When i exceeds the value of n, the control jumps to the return 0 statement.
Programming Example
Write a program to determine whether a given number is a prime or a composite number.
#include <stdio.h>
#include <conio.h>
int main()
{
int flag = 0, i, num;
clrscr();
printf("\n Enter any number : ");
scanf("%d", &num);
for(i=2; i<num/2;i++)
{
if(num%i == 0)
{
flag =1;
break;
}
}
if(flag == 1)
printf("\n %d is a composite number", num);
else
printf("\n %d is a prime number", num);
return 0;
}
Output
Enter any number : 37
37 is a prime number