Odd Loop

What is odd loop?
In real life programming, there are many times comes a situation when we dont 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("
Want to calculate more square y/n: "
);

  scanf("%c", &answer);
 }while(answer==y);
 return 0;
}

The output of above program would be:
Output of odd loop ( do..while) square C program
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("
Want to calculate more square y/n: "
);

  scanf("%c"&ans);
 }
 return 0;
}

The output of above program would be:
Output of odd loop ( for ) square C program
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("
Want to calculate more square y/n: "
);

  scanf("%c"&ans);
 }
 return 0;
}



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

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.