Loop Control Statements 127
void mainO
{
in t i=0;
c l r s c r ();
fo r ( ; ; )
I
printf ("%5d",i++);
if (i--1l)
goto stop;
i
stop:;
I
QJJJFUJ-i
0123456789 10
Explanation The for ( ; ; ) statement is used in the above program. This statement allows
execution of the body of the loop infinite times. To exit from the infinite for loop, goto statement
is used. After satisfying the test condition goto statement is executed which terminates the program.
6.8 Write a program to count numbers between 1 to 100 not divisible by 2,3 and 5.
# include <stdio.h>
# include <conio.h>
void mainO
{
in t x ,ca 0 ;
c l r s c r ( );
printf ("n Numbers from 1 to 100 not divisible by 2,3 & 5nn");
fo r ( x=0 pc<=100;x++)
/
if (x%2!=0 &&x%3!^0 && x%5!=0)
I
printf (%d ",x);
C + + ;
II
printf (“ Total Numbers : %d",c);
/
OUTPUT:
Numbers from 1 to 100 not divisible by 2,3 & 5
1 7 11 13 17 19 23 29 31 37
41 43 47 49 53 59 61 67 71 73
77 79 83 89 91 97
Total Numbers : 26
128 Programming and Data Structures
Explanation In the above program the for loop executes from 1 to 100. Each time mod operation
is performed with 2,3 and 5 with value of loop variable. If remainder is zero then the counter ' c ' is
increased. Thus, 1 to 100 numbers are checked. At the end the variable ' c ' displays the total number.
6.9 Write a program to display the numbers in increasing and decreasing order using infinite
for loop.
# include <stdio.h>
# include <conio.h>
void main()
{
int n ,a ,b ;
c l r s c r ();
printf ("Enter a number
scanf (“%d",&:n);
a=b=n;
printf ("(++) (~)*");
printf ("============");
fo r ( ; ; (a++,b~))
I
printf (" %d %d",a,b);
if (b~ 0)
break;
I
OUTPUT;
Enter a number : 5
(++> (~)
5 5
6 4
7 3
8 2
9 1
10 0
Explanation The infinite for loop can also be specified as shown in the above program. The
initial counter value, which is set to a given number, can be continuously increased or decreased.
In order to terminate from the infinite for loop a condition (b==0) is tested. After satisfying
break statement allows to exit from the loop.
Loop Control Statements 129
6.10 Create an infinite for loop. Check each value of the for loop. If the value is even, display
it otherwise continue with iterations. Print even numbers from 1 to 21. Use break
statement to terminate the program.
# include <stdio.h>
# include <conio.h>
void main()
{
int i = l ;
c l r s c r ();
printf (" t Table o f Even numbers from 1 to 20")}
printf (" ===== == ==== ======= ==== = == ==n");
fo r ( ;;)
{
if (i~21)
break;
else if (i%2~0)
{
printf ("%dt",i);
i++;
continue;
}
else
I
/++;
continue;
I
I
I
qu j zu ii
Table of Even numbers from 1 to 20
2 4 6 8 10 12 14 16 18
Explanation In the above programbreak and continue both statements are in use. The program
displays only even numbers from 1 to 21. An infinite loop is created and in the loop variable ' i ' is
increased. The value of ' i ' is checked every time. If it is even, it is printed otherwise continue
statement is executed, which passes control at the beginning of the for loop. When the value o f ' i '
reaches to 21 the break statement is executed and it terminates the program.
6.11 Calculate the sum of first five numbers and their squares. Display their results,
m ain()
{
in t i , sum=0, sqsum =0;
c l r s c r ( );
130 Programming and Data Structures
for 'i=2/i<=5/*i++)
/
i
sum+=i;
sqsum+=i*i;
printf (" umber: %5d it's Square: %8d",i,i*i);
1
printf (" =-------------=— = -=
-----
=_======_=======
-----
=
printf ("The Sum %6d Sum o f Squares %9d", sum,sqsum);
1
OUTPUT:
Number: 1 it's Square: 1
Number: 2 it's Square: 4
Nu^nben 3 it's Square: 9
Number. 4 it's Square: 16
Nutnben 5 it's Square: 25
The Sum 15 Sum of Squares 55
Explanation In the above program variable ' sum' and ' sqsum' are initially set to zero. The for
loop is set from 1 to 5. Every time value of loop variable 1 i ' is added to variable 1 sum' and its
square to variable 1 sqsum'. At the end both variables are printed.
6.12 Write a program to display numbers from 1 to 9 and their square roots.
#include <math.h>
main()
{
int i ;
flo at a;
c l r s c r ();
for (i= 1;i< =9;i++)
printf ("nt %d %.2f “,i,a=sqrt(i));
1
OUTPVJT:
1 1.00
2 1.41
3 1.73
4 2.00
5 2.24
6 2.45
7 2.65
8 2.83
9 3.00
Loop Control Statements 131
Explanation In the above program the f o r loop executes nine times from 1 to 9. The s q r t ()
function returns square root of a number. Each time value of variable ' i ' is passed to s q r t () it
returns the square root of that number.
6.13 Write a program to find the number in between 7 and 100 which is exactly divisible by
4 and if divided by 5 and 6 remainders obtained should be 4.
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
int x;
clrscr();
fo r (x=7;x<100;x++)
I
if(x%4==0 && x%5==4 && x%6~4)
I
printf ("tt Minimum Number : %d",x);
I
I
I
OUTPUT:
Minimum Number : 64
Explanation In the above program the fo r loop is iniatlized from 7 to 100. The i f statement
is used for dividing the modular division operations by 4 ,5 and 6. The remainders obtained are
checked. If they are 0 ,4 and 4 respectively then the number is displayed.
6.14 Write a program to evaluate the series given in comments.
/* x=l/l+l/4+l/9...1/n2 */
/* y=1/1+1/8+1/27...l/n3 */
# include <stdio.h>
# include <conio.h>
# include <math.h>
void m a i n ()
{
int i,n;
float x=0,y=0;
clrscr();
printf ("Enter Value of n:”);
..................Content has been hidden....................

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