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:
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:
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:
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 |
Alright sir....but can you tell me what is "fflush(stdin)"
ReplyDeletefflush(stdin) function used to flush the remaining data in memory/compiler.
DeleteNot working Sir :(
ReplyDelete@gurpreet,
DeleteAbove programs working good without error.
What you find error or in which program.
wap to print
ReplyDelete1
a
12
ab
123
abc
12345
adcd
pls tell me a program
ReplyDeletewap to print
1
a
1 2
a b
1 2 3
a b c
1 2 3 4
a b c d
sir is there an another alternative of fflush(stdin) please kindly tell us .
ReplyDeletewil it provide output without use of fflush(stdin)?
ReplyDeleteit's not giving bro..
DeleteI tried