Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. While looping
2. Do ... while looping
3. For looping
while (<condition>)
statement;
while (<condition>)
{
Statement 1;
Statement 2;
.
.
Statement n;
}
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.
#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.
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();
while (i<=n)
{
printf(“%d\t”,i);
i++;
}
getch();
}
EXERCISE 1:
EXAMPLE 3:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1, n, m;
clrscr();
getch();
}
EXAMPLE 4:
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
. . .
. . .
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1, n, a=1;
clrscr();
while (i<=n)
{
printf(“%d\t”, a);
a = a + i;
i++;
}
getch();
}
EXERCISE 2:
1 2 6 15 31 ............................
1 2 5 10 17 ............................
1 3 7 13 19 ............................
EXAMPLE 5:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, m, n;
clrscr();
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.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, m, n, temp;
clrscr();
getch();
}
EXAMPLE 6:
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();
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:
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.
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.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, n, i;
clrscr();
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
#include<stdio.h>
#include<conio.h>
void main()
{
long int a, b, s=0;
clrscr();
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 :
}
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>);
EXERCISE: