While Loop:-
Program:
Void main()
{
Int t=0, t21,nextterm-0,n;
Clrscr ();
Printf (“Enter a positive number”);
Scanf (“%d”,&n);
Printf(“Fibonacci series:%d,%d,”,t1,t2”);
nextterm=t1+t2;
while(nextterm<=n);
{
Print (“%d,”,nextterm);
t 1=t2;
t2=nextterm;
nextterm=t1+t2;
}
getch();
}
OUTPUT:
Enter a Positive number: |
5 |
Fibonacci series : |
0,1,1,2,3,5 |
Do-While:-
A Do-while loop is similar
to while loop except the fact it is guranted to
execute at one time
Unlike for while loop
which test the loop condition at the loop do while loop in c.
Program:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int i=1;
Clrscr();
do
{
Printf (“%d\n”,I );
I=1+2;
}
While(i<10);
getch();
}
OUTPUT:
1 3 5 7 9 |
For loop:-
Executes a sequence of
statement multiple times and abbreviate {s} the code that
Manages the loop
variable.
Program:
Void main()
{
Int I,h;
Clrscr();
Printf(“Enter the vale
of n:”);
Scanf(“%d ”,&n);
For(i=1;i<n;i++)
{
Print(“%&d ”,i);
}
getch();
}
OUTPUT:
Enter the value of n=8 |
1 2 3 4 5 6 7 8 |
No comments:
Post a Comment