Loop Control Statements 145
21 22 23 24 25 26 27 28 29
30
31 32 33
34
35 36
37
38 39
40
41 42
43
44 45
46
47
48
49
50
51 52 53
54
55 56 57 58 59
60
61
62
63
64
65 66
67
68
69
70
71 72 73 74 75 76 77 78 79
81 82 83
84
85
86 87
88 89
91
92
93
94 95
96
97 98 99 100
Explanation The first for loop is varying from 48 to 57. These numbers are used for printing first
digit of every number from 1 to 1Q0. 48 to 57 are ASCII codes for 0 to 9. The second for loop is
varying from ASCII 49 to 57 whose equivalent numerical numbers are 1 to 9. These ASCII codes are
used for printing second digit. In each output line the common digit like 0 in the first line is decided
by variable of the outer loop. It varies from 0 to9. The second digit of each line is decided by the
variable of inner loop, which varies for all lines from 1 to 9. The value of k =-9 for displaying 10
rows. For displaying numbers from 01 to 99 i & j are printed without using space in oneprint f ()
statement.
The first i f statement is used for printing 10,20,30 upto 90 numbers. The second i f is used to
print 100.
6.311 Write a program to count number of votes secured by 'A' & 'B Assume three voters
are voting them. Also count the invalid votes.
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void xnain()
{
int a * 0 ,b a 0 ,o « 0,i;
char v;
c l r s c r () ;
printf (" Press A or Bn");
fo r (i=l/i<=3/*i++>
{
printf ("nnVoter no. %d”,i);
printf (" Enter Vote
v=getche();
v=toupper(v);
if (v=='A')
a++;
else if ( v ~ ’Br)
b++;
else
o++;
/
146 Programming and Data Structures
printf <* nn Stattts ofvote(s) n");
printf ("nA secures %d vote(s).",a);
printf ("nB secures %d vote(s).",b);
printf (" lnvalid votes %d.",o);
I
QUTEUT;
Press A or B
Voter no. 1 Enter Vote : A
Voter no. 2 Enter Vote : B
Voter no. 3 Enter Vote : X
Status of votes
A secures 1 vote(s).
B secures 1 vote(s).
Invalid vote(s) 1.
Explanation In the above program the for loop is used for the number of voters who will be
voting to either 'A ' or ' B '. The body of the loop is executed for three times. The voter can give his/
her choice either by pressing %A' or %B '. Apart from these two characters if the user enters any
other character the vote will be invalid. The results are displayed at the output.
6.32 Write a program to simulate a digital clock.
# include <stdio.h># include <conio.h>
# include <dos.h>
void main()
{
in t h,m ,s;
c l r s c r ();
for (h=l;h<=12;h++)
{
clrscrO;
fo r (m=2;w<=59;w++)
/
clrscrO;
for (s=l;s<=59;s++)
I
gotoxy(8,8);
printf ("hit mm ss);
gotoxy(8,9);
printf ("%d %d %d”,h,m,s);
sleep(l);
Loop Control Statements 147
I
i
I
I
aUTPUT;
hh mm ss
1 1 1
Explanation The above program uses three fo r loops for hour, minute and second. The inner
most loop is for second's operation, followed by middle loop for minutes and the outer most is for
hours. When the second's loop executes 60 times, the minutes loop increases by 1. Similarly, the
hours loop increases after completing minute's loop. The hours loop increments only up to 12.
There after it resets from 1. The go t oxy () function is used for selecting the position where hh mm
ss is to be marked.
6.33 Write a Program to count occurrence of 0 to 9 digits between 1 and given decimal number.
# in clu d e < std io .h >
# in clu d e <conio.h>
void mainO
{
in t t,k,n,l,i;
s t a t i c in t s t [ 1 0 ] ;
c l r s c r ( ) ;
printf ("n Enter a Decimal Number
scanf (%d",&n);
for (l=l;l<=n;l++)
I
t=l;
while (t!=0)
I
k=t%10;
t=tll0;
st[k]++;
I
I
printf (“occurrence o f 0-9 digits between 1 to %d Numbers.",n);
printf <" =-==-== = == === ====== ======= = ======== ");
for (i=0;i<10;i++)
if(st[i]>0)
printf ("n %d Occurs %8d Times.",I,st[i]);
148 Programming and Data Structures
getchO;
QUTP.UT;
Enter a Decimal Number 15
Occurrence of 0-9 digits between 1 to 15 Numbers.
0 Occurs 1 Times.
1 Occurs 8 Times.
2 Occurs 2 limes.
3 Occurs 2 Times.
4 Occurs 2 Times.
5 Occurs 2 Times.
6 Occurs 1 Times.
7 Occurs 1 Times.
8 Occurs 1 Times.
9 Occurs 1 Times.
Explanation A decimal number is entered through a keyboard with the variable n. The for loop
is used from 1 to n (up to the given number). The value of % 1 ' is copied to another variable % t '. In
the body of while loop mod operation is performed on variable x t ' for separating individual
digits and separated digit is stored in variable %k'. Further the 11 ' is divided by 10 for obtaining
remainders and assigned to 11'. The array st [10] is initialized to 0 using static storage class.
The variable % k' is used as an argument with s t [ ] and it is increased. The value of 1 k' whatever
it contains when used in arrays st [] that element number will be increased. Thus, we get the
occurrence of digits from 0 to 9 by printing this array.
634 Write a program to accept a number and find sum of its individual digits repeatedly till
the result is a single digit.
# include <stdio.h>
# include <conio.h>
# include <process.h>
void mainO
{
int n,s«0;
clrscrO ;
printf ("ti Enter a Number :");
scanf ("%d",tm);
printf ("n Sum o f Digits till a single digit %d",n);
for ( ; n!=0;)
Loop Control Statements 149
I
s=s+n%10;
n=n/10;
if (n==0 && s>9)
/
printf ("n %2d",s);
n=s;
s-0;
I
I
printf <n %2d"j);
)
OUTPUT:
Enter a Number :4687
Sum of Digits till a single digit
4687
25
7
Explanation In the above program a number is entered through the keyboard. The fo r loop
executes till the value of variable ' n ' is non-zero. The sum of digits obtained is assigned to variable
'n ' . If 'n ' is found zero and sum is greater than 9 the f o r loop continues. Thus, repeatedly the
sum of the digits are calculated till the result of sum the becomes less than 10.
6.35 Write a program to display octal numbers in binaiy. Attach a parity bit with "1" if
number of Is are even otherwise "0".
OR
Generate odd parity to octal numbers 0 to 7. Express each number in binaiy and attach the
parity bit
# inclu d e < std io .h >
# in clu d e <conio.h>
void main()
int x #y ,a ,b ,c * 0 , j - 1 2 ,k«2;
c l r s c r () ;
printf (" Binary Bits Parity Bitsn");
printf (" =========== ===========");
..................Content has been hidden....................

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