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 |
No comments:
Post a Comment