## 10.20.2012

### Odd Loop

What is odd loop?
In real life programming, there are many times comes a situation when we don't know how many times the statements in the loop are to be executed.
There is comes concept of odd loop.
Execution of loop an unknown number of times can be done by - while, for and do...while loops.

/*A demonstration of odd loop using do...while*/
#include<stdio.h>
int main()
{
int n;
char answer;
do
{
printf("Enter any number : ");
scanf("%d", &n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c", &answer);
}while(answer=='y');
return 0;
}

The output of above program would be:
 Figure: Screen shot of odd loop (do...while) to calculate square of number C program

The above odd loop program we can write using for loop as:

/*odd loop using for loop of calculate square number C program*/

#include<stdio.h>
int main()
{
int n;
char ans='y';
for(; ans=='y' ; )
{
printf("Enter any number : ");
scanf("%d"&n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c"&ans);
}
return 0;
}

The output of above program would be:
 Figure: Screen shot of odd loop (for) to calculate square of number C program

The odd loop program we can write using while loop as:
/*odd loop using while loop of calculate square number C program*/

#include<stdio.h>
int main()
{
int n;
char ans='y';
while(ans=='y')
{
printf("Enter any number : ");
scanf("%d"&n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c"&ans);
}
return 0;
}

The output of above program would be:
 Figure: Screen shot of odd loop ( while ) to calculate square of number C program

#### 9 comments:

1. Alright sir....but can you tell me what is "fflush(stdin)"

1. fflush(stdin) function used to flush the remaining data in memory/compiler.

2. Not working Sir :(

1. @gurpreet,

Above programs working good without error.
What you find error or in which program.

3. wap to print
1
a
12
ab
123
abc
12345
adcd

4. pls tell me a program
wap to print
1
a
1 2
a b
1 2 3
a b c
1 2 3 4
a b c d

5. sir is there an another alternative of fflush(stdin) please kindly tell us .

6. wil it provide output without use of fflush(stdin)?

1. it's not giving bro..
I tried