Loop Control Statements 191
EXERCISES
A] Answer the following questions.
1) What happens if you create a loop that never ends?
2) Is it possible to create a fo r loop that is never executed?
3) What is a loop? Why it is necessary in the program?
4) Is it possible tones t while loop within fo r loops?
5) How do you choose between while and fo r loop?
6) What is the difference between (10) and (! 1). Howwhileloop works with these values?
7) What is the difference between (1= =! l)and(l! =1)?
8) What is the difference between (I 0) and (! 0) ?
9) What are the values of NULL and! NULL?
10) Is it possible to use multiple w hile statement with do statement?
B) Answer the following by selecting the appropriate option.
1) What will be the value of c after the execution of following program?
# include <stdio.h>
# include <conio.h>
void main ()
{
int c=l,d=0;
clrscrO ;
while (d<=9)
I
printf (" %d %d",++d,++c);
I
I
a) 11 b) 10 c) 12 d) 9
2) What will be the value of ' x ' after the execution of following program?
# include <stdio.h>
# include <conio.h>
void mainO
{
in t k;
flo a t x=0;
clrscrO ;
for (k=0;k<10;k++)
x+=.l;
printf (" x=%g",x);
}
a) x=l b) x=0
c) x=l.l d) none of the above
3) What will be the value of ' f ' after the execution of following program?
# include <stdio.h>
# include <conio.h>
void main ()
{
192 Programming and Data Structures
char k;
float f=65;
clrscrO ;
for (k=l;k<=10;k++)
{
/-= i;
}
printf ("nf=%g",fi;
}
a) f=64 b) f=-65
c) f=66 d) none of the above
4) What would be the final value o f' x ' after the execution of the following program?
# include <stdio.h>
# include <cdnio.h>
void main ()
{
int x=l;
clrscrO ;
do while (x<=10)
i
x++;
I while (x<=5);
printf ("tt x=%d",x);
}
a) x=ll b) x=6
c) x=2 d) none of the above
5) How many while statements are possible in do. . whi 1 e loop?
a) 2 b) 1
c) 3 d) none of the above
6) What will be the final values of x and y?
# include <stdio.h>
# include <conio.h>
void main ()
{
int x=l,y=l;
clr s c r ();
do while (x<=8)
I
x++,y++)
} while(y<=5);
printf (" x=%d y=%d"rxfy);
}
a) x=9 y=9 b) x=9 y=6
c) x=6 y= 6 d) none of the above
C] Attempt the following programs.
1) Write a program to display alphabets as given below.
Az by cx dw ev fu g t hs Ir jq kp lo mn nm ol pk qj r i sh tg uf ve wd xc yb za.
2) Write a program to display count values from 0 to 100 and flash each digit for one second. Reset
the counter after it reaches to hundred. The procedure is to be repeated. Use for loop.
Loop Control Statements 193
3) Develop a program to simulate seconds clock. Put the 60 dots on the circle with equal distance
between each other and mark them 0 to 59. A second's pointer is to be shown with any symbol.
Also print the total number of revolution made by second's pointer.
4) Write a program to simulate analog watch (1 to 12 numbers to be arranged in circular fashion
with all the three pointers for seconds, minutes, and hours) on the screen. Use nested fo r loops.
a) Use (.) dot for second's pointer.
b) Use (*) star for minute's pointer.
c) Use (#) hash for hour's pointer.
5) Write a program to calculate sum of the first and the last number from 1 to 10.
(Example 1+10,2+9,3+8 sums should be always 11)
6) Write a program to find the total number of votes in favor of persons 1A' and % B'. Assume 100
voters will be casting their votes to these persons. Count the number of votes gained b y% A' and
' B'. User can enter his/her choices by pressing only 'A7 or 1B'.
7) Write a program to pass the resolution in a meeting comprising of five members. If three or more
votes are obtained the resolution is passed otherwise rejected.
8) Assume that there are 99 voters voting to a person for selecting chairman's candidature. If he
secures more than 2/3 votes he should be declared as chairman otherwise his candidature will
be rejected.
9) Write a program to display the numbers of a series 1,3,9,27,81... .n, by using fo r loop.
10) Write a program to check the entered input data for the following. Whenever input is non-zero
or positive display numbers from 1 to that number otherwise display message "Negative or
zero". The program is to be performed for 10 numbers.
11) Write a program to check entered data types for 10 times. If character is entered print "Character
is entered" otherwise "Numeric is entered" for numerical values.
12) Write a program to find the sum of first hundred natural numbers. (1+2+3+..100).
13) Write a program to display numbers 11,22,33
.....
99 using ASCII values from 48 to 57 in loops.
14) Create an infinite fo r loop. Check each value of the fo r loop. If the value is odd, display it
otherwise continue with iterations. Print even numbers from 1 to 100. Use break statement to
terminate the program.
15) Write a program to show the display as a rectangle of characters as shown below.
Z
YZY
XYZYX
RXYZYXR
XYZYX
YZY
Z
16) Write a program to read ten numbers through the keyboard and count number of positive,
negative and zero numbers?
17) Write a nested fo r loop that prints a 5 X 10 pattern of 0s.
18) Is it possible to create a loop using goto statement? If yes write a code for it.
19) Write a program to find the triangular number of a given integer. Fox example triangular of 5 is
(1+2+3+4+5) 15. Usedo-whileloop.
20) Write a program to display all ASCII numbers and their equivalent characters numbers and
symbols. Use do-while loop. User should be prompt every time to press %y ' or 'N'.Iftheuser
presses %y ' display the next alphabet otherwise terminate the program.
21) Accept any five two numbers. If the first number is smaller than the second then display the sum
of their squares, otherwise sum of the cubes.
194 Programming and Data Structures
22) Evaluate the following series. Use do - whi le loop.
a) l+3+5+7...,n
b) 1+4+25+36..H
c) x+x2/2! +x3/31 +. .n
d) 1+X+3C2 +X3 +... xf1
23) Write a program to display the following, using do-while loop.
a) a+l+b+2+c+3
..
n , where n ii an integer.
b) z+y+x...+a.
c) za+yb+xa..+az.
24) Enter 10 numbers through the keyboard and sort them in ascending and descending order,
using do-while loop.
25) Enter text through the keyboard and display it in reverse order. Use do - whi le loop.
26) Print multiplication of digits of any number. Fox example number 235, multiplication to be
5*3*2 = 30. Usedo-while loop.
27) Print square roots of each digit of any number. Consider each digit as perfect square. For example
494 the square roots to be printed should be 2 3 2.
28) Write a program to read a positive integer number 1 n' and generate the numbers in the following
way. If the entered number is 3 the output will be as follows.
9410149.
9 41012 3.
29) Write a program to enter two integer values through the keyboard. Usingwhile loop, perform
the product of the two integers. In case product is zero (0), loop should be terminated^ otherwise
loop will continue.
30) Write a program to enter a single character either in lower or capital case. Display its
corresponding ASCII equivalent number. Use the while loop for checking ASCII equivalent
numbers for cufferent characters. When capital 'E' is pressed, program should terminate.
31) Write a program to read a positive integer number 'n' and generate the numbers in the following
way. If the entered number is 4 the output will be as follows. OUTPUT: 4! 3! 2! 1! 01! 2! 3! 4! 5L
32) Write a program to read a positive integer number ' n ' and generate the numbers in different
ways as given below. If the entered number is 4 the output will be as follows.
a) 2 4 6 8 10 ,.ji (provided n is even). b) 1 3 5 7 9.... n (provided n is odd).
33) Write a program to read a positive integer number ' n' and perform the squares of the individual
digits. For example n=205 then the output will be 25 0 4.
34) Write a program to enter intger number and find the largest and the smallest digit of the number?
D] Execute the following program and write your observation as to whether the program is
executed or not with its output.
1) What would be the output of the given below programs?
a)
mainO
{
while (0)
{
printf (" Hello" ) ;
}
}
b)
main()
Loop Control Statements 195
{
while (!0)
{
printf (" Hello" );
}
}
c)
mcin()
{
while (11)
{
printf (" Hello" );
}
}
d)
main()
{
while (! NULL)
{
printf ("n %s", "Hello" );
}
}
e)
main()
{
while (" " )
{
printf (" %s","Hello" );
}
}
2) What will be the output of the program below given? Attempt this program with other keywords
and functions. List the names of the keywords and the functions, which can be used as arguments
in thewhile loop.
mainO
{
while (main)
{
printf ("n %d",main ) ;
>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.154.252