Sei sulla pagina 1di 17

LOOPING

There is a very important role of looping in the world of


programming. A loop is a structure in which there is a repetition
of one or more statements to prevent the length of the
program. The looping is the most important thing to learn. This
is a tough job to understand the looping if we are not in favour
of understanding it. To be a good programmer, someone should
be very good in looping and should know very well the flow of
the control of program through a loop. A loop is always used to
repeat the statements of the program to a required number of
times. We can repeat the statements as many times as we
want. In any programming language, looping can be done using
three type of controls :

1. While looping
2. Do ... while looping
3. For looping

We will go through them one by one.


WHILE LOOPING

While looping is very useful in those cases where there is a


need of repeating the statements up to a limit of the condition
i.e. the statements must be repeated till a condition is in true
state.

The syntax of the loop is given as :

while (<condition>)
statement;

or if we want to use the multiple statements to be repeated, we


can use –

while (<condition>)
{
Statement 1;
Statement 2;
.
.
Statement n;
}

So, the statements from 1 to n will be repeated until the


condition given after the while is in true state. Now, the
problem arises, what type of condition should be applied ???
The condition must be in such way that at a stage, it must
become false. If we don’t apply such condition then the loop
may run infinitely. Let me give you a small example of while
loop.

EXAMPLE 1:
In this program, a name will be printed 10 times. From now
onwards, I would like you to get the output. I am not providing
outputs of programs from now except some of them which may
be complicated to understand.

// Program to print a name ten times.

#include<stdio.h>
#include<conio.h>

void main()
{
int i=1;
clrscr();

while (i<=10)
{
printf(“\nAlpha”);
i++;
}

getch();
}

Now, the process goes somewhat like this: when we start our
program, we gave an initial value to a variable i. For the first
step, we checked whether i is less than or equal to 10. As the
condition is found true (1 is less than 10), compiler enters the
body of the loop and prints ‘Alpha’ for the first time. Now, we
have an increment in i so that the value of i becomes 2. Again
the check occurs in the condition of while loop and the
procedure goes on till i becomes 10.

Take the case of the last run of the loop i.e. when loop is
entered when value of i is 10. After printing the name, the
value of i further increases and becomes 11. The check for the
condition happens and condition is found in false state as 11 is
greater than 10. So, compiler bypasses the body of the loop
and proceeds further for the next statement. like this the
looping is completed.

Now, have an experiment by erasing the i++ line. The loops


runs infinitely as the condition is never in the false state due to
the constant value of i (1) and 1 is always less than 10. Hence
an infinite loop without an error read by the compiler.

EXAMPLE 2:

// Program2:
EXAMPLE to print counting from 1 to n.

In#include<stdio.h>
this program we need to print the counting from 1 to n. Here
n#include<conio.h>
is any number given by the user at run-time.

void main()
{
int i=1, n;
clrscr();

printf(“\nPlease enter a value : “);


scanf(“%d”,&n);

while (i<=n)
{
printf(“%d\t”,i);
i++;
}

getch();
}

I don’t think that there is any need of explaining this program.


Try the reverse of the program by your own.
n n-1 n-2 ................................. 4 3 2 1

EXERCISE 1:

Print the following series in given sequence as well as in


reverse sequence:
m m+1 m+2 ................................ n+2 n+1 n
1 4 9 16 25 36 ...................
1 8 27 64 125 216 .............

Print even number and odd number series.


Print the table of a given number given by the user.
Upto 10 multiplies
Upto n multiplies.
From m to n multiplies.

EXAMPLE 3:

In this example, we will print the table of a given number up to


a//given
Program to print
number the table of a given number up to a given
of terms.
number of terms.

#include<stdio.h>
#include<conio.h>

void main()
{
int i=1, n, m;
clrscr();

printf(“\nEnter the number : “);


scanf(“%d”,&n);

printf(“Enter the number of terms : “);


scanf(“%d”,&m);
while (i<=m)
{
printf(“\n%d * %d = %d”, n, i, (n*i));
i++;
}

getch();
}

EXAMPLE 4:

In this program we will print the following series:


1 2 4 7 11 16 ..............

Here, note that the difference between the numbers increases


as we moves forward in the series. In these type of problems,
we can use an extra variable which adds the value of i in its
previous value. This series can also be taken as:

1 (1+1)=2 (2+2)=4 (4+3)=7 (7+4)=11


(11+5)=16 ............

So, previous value ads in the ascending value of i to make the


value for the next step. Here, the procedure foes on as:

I A Value to print

1 1 1
1 (1+1)=2 2
2 (2+2)=4 4
3 (4+3)=7 7
4 (7+4)=11 11
5 (11+5)=16 16
. . .
. . .

The program is:

// Program to print the series 1 2 4 7 11 16 ........

#include<stdio.h>
#include<conio.h>

void main()
{
int i=1, n, a=1;
clrscr();

printf(“\nEnter the number of terms : “);


scanf(“%d”,&n);

while (i<=n)
{
printf(“%d\t”, a);
a = a + i;
i++;
}

getch();
}

EXERCISE 2:

Try the following series in both orders :

1 2 6 15 31 ............................
1 2 5 10 17 ............................
1 3 7 13 19 ............................

EXAMPLE 5:

Here is the program in which we need to print a series from any


number m to n. Now, the situation arises, if we need to print
the series from m to n in ascending order then it must be
considered that m is always less than n. However if m is greater
than n then the loop may run infinitely.

To overcome this, we can use two methods: check for the


greater condition before the while loop, if m is found greater
then run the loop from n to m. The second method may be: if m
is found greater than n then swap the values of m and n and
then run the loop in the older manners. I am writing both the
programs so that you may get help for other programs also.
// Program to print the series from m to n in both cases (m>n
and m<n)
// Method 1:

#include<stdio.h>
#include<conio.h>

void main()
{
int i, m, n;
clrscr();

printf(“\nEnter two numbers : “);


scanf(“%d %d”, &m, &n);

if (m<n)
{
i = m;
while (i<=n)
{
printf(“%d\t”, i);
i++;
}
}

else
{
i = n;
while (i<=m)
{
printf(“%d\t”, i);
i++;
}
}

getch();
}
There is second method of doing the same program.

// Program to print the series from m to n in both cases (m>n


and m<n)
// Method 2:

#include<stdio.h>
#include<conio.h>

void main()
{
int i, m, n, temp;
clrscr();

printf(“\nEnter two numbers : “);


scanf(“%d %d”, &m, &n);
if (m>n)
{
temp = m;
m = n;
n = temp;
}
i=m;
while (i<=n)
{
printf(“%d\t”, i);
i++;
}

getch();
}

EXAMPLE 6:

Palindrome number

In this program we are checking whether a given number is


palindrome or not. A palindrome number is the number which
remains same while reading from left to right or right to left.
For example: 12321 is a palindrome number as it is same in
both side reading. 1223 is not palindrome as it is 1223 when we
read it from left to right and 3221 when we read it from right to
left. As both are not equal, it is not a palindrome number.

Here is the program for the palindrome check for any number.
Its better to give the description later after the program. Hence
I will describe the process after the program.
// Program to check whether a given number is palindrome
or not.

#include<stdio.h>
#include<conio.h>

void main()
{
long int a, b, c;
clrscr();

printf(“\nPlease enter a number to check for palindrome


: “);
scanf(“%ld”, &a);

b = a;
c = 0;

while (b!=0)
{
c = c*10 + b%10;
b = b/10;
}

if (a==c)
printf(“\nThe given number %ld is palindrome.”,
a);
else
printf(“\nThe given number %ld is not
palindrome.”, a);

getch();
Now, let us understand the procedure of the program. In this
program, I have taken three variables as long integers. I have
not used int here because if someone is checking for a number,
it may easily exceed the limit of integer i.e. 32767. Hence long
int is used instead of int. In the second step, I have taken the
value of a from user and this value is copied in the second
variable b. Also, the third variable c is made to be 0 in initial
state. Using the while loop, I have taken each digit of b and
then it is made to be in c in such manner that when b becomes
0, c will contain the opposite number of b. As b was a copy of a,
I have compared a with its opposite number c to check whether
the number is palindrome or not. If they both match, the
number is said to be palindrome else it is not a palindrome. The
process of while loop can be easily understood using a
example: let it be 1234. Now, the process goes like this:

1. As b is not 0, the loop body is entered by the compiler.


c=c*10 + b%10;
As b%10 gives the last digit of b i.e. 4 in this case and the
value of c is already 0 in the first step, c = 0*10 +
1234%10 gives the value of c as 4. Note that this is the
last digit of b. Now, b is reduced by b=b/10; by this b
takes the value as 123. This value is again checked for the
next step of the loop.

2. In the second run of the loop, b have the value as 123 and
c is 4. In the first line c = 4*10 + 123%10; gives the value
of c as 43. In the next line, b is divided by 10 to have the
value as 12.

Like this the process goes on and at last, b is found as 0 and c


contains the reverse number of b. This is compared with a to
check for the palindrome.

EXAMPLE 7:

FIBONNACI SERIES

0 1 1 2 3 5 8 13 21 34 ...
This is a very important program in the term of exams as well
as in higher programming. This series have this concept : there
are two numbers 0 and 1 at the beginning. Now, the next
number will be the sum of previous two numbers as 0+1=1,
1+1=2, 2+1=3, 3+2=5 and so on.

To do this, we’ll take two numbers at the beginning and a


counter will count the number of steps. Parallel, process of
summing up previous numbers and transfer of values will
continue. Here is the program :

// Program to print the fibonnaci series.

#include<stdio.h>
#include<conio.h>

void main()
{
int a, b, c, n, i;
clrscr();

printf(“\nPlease enter the number of steps : “);


scanf(“%d”, &n);

a = 0;
b = 1;
i = 3;
printf(“Here is the series :\n\n%d\t%d\t”,a,b);
while (i<=n)
{
c = a + b;
printf(“%d\t”,c);
a = b;
b = c;
i++;
}
getch();
OUTPUT :
}
Please enter the number of steps : 8
Here is the series :

0 1 1 2 3 5 8 13
Here is the table of values :
a b c i n
0 1 1 3 8
1 1 2 4
1 2 3 5
2 3 5 6
3 5 8 7
5 8 13 8

EXAMPLE 8:

ARMSTRONG NUMBER

Here is one more important program as Armstrong number. A


number is said to be Armstrong number is the sum of cube of
individual digits of the number is equal to the number itself.

Consider a number abc then it will be Armstrong iff


a*a*a + b*b*b + c*c*c = abc
or a^3 + b^3 + c^3 = abc

Example : 153 is a Armstrong number because


1*1*1 = 1
5*5*5 = 125
3*3*3 = 27
And 1 + 125 + 27 = 153.
Here is the program to check whether a given number is
Armstrong or not : it is somewhat similar to the palindrome
program.

// Program to check whether a given number is Armstrong or


not.

#include<stdio.h>
#include<conio.h>

void main()
{
long int a, b, s=0;
clrscr();

printf(“\nPlease enter the number to check : “);


scanf(“%ld”, &a);

b = a;

while (b != 0)
{
s = s + pow(b%10, 3);
b /= 10;
}

if (s==a)
printf(“\n\nThe given number is Armstrong.”);
else
printf(“\n\nThe given number is not an Armstrong
number.”);

getch();
OUTPUT :
}

Please enter the number to check : 153

The given number is Armstrong.


While loop can also be implemented over the characters. It is
easy to do this using for loop hence I’ll do this in for loop.
DO...WHILE LOOP

Do...while loop is very similar to the while loop but there is only
one slight change in the syntax of the loop.

Syntax :

do
{
Statement 1;
Statement 2;
.
.
Statement n;
}while(<condition>);

All of the condition and statements are same as in while loop.

NOTE : There is a semicolon(;) at the end of the while.

DIFFERENCE BETWEEN WHILE AND DO...WHILE

It is not necessary for a while loop to run if the condition given


is false but in case of do...while loop, it will run at least once
and then the condition will be checked.

EXERCISE:

Implement all of the programs used in while loop by do...while


loop.

Potrebbero piacerti anche