while-loop

In this page we create two program using do-while loop,

 

Print a fibonacci  series using while loop

before starting program, what is fibonacci series?

In Fibonacci series, the first number and the second number are total, is the third number.

first number + second number = third number

such as,

fibonacci

Here is the program,

#include<iostream.h>
#include<conio.h>
void main()
{
  int first=0,second=1,next;
  int num;
  clrscr();
 
 cout<<"Enter the number of terms in series:"; cin>>num;

  cout<<first<<"\t"<<second;

  int i=3;
  while(i<=num)
  {
     next=first+second;
     cout<<"\t"<<next; 
     first=second; // now first number is second
     second=next; // and second number is next
     i++;        
  }
 getch();
}
OUTPUT:-
Enter the number of terms in series: 8
0       1       1       2       3       5       8       13

explanation:

In the program, we have declared the following variables.

int num, first,second,next;

In which variable first and wearable seconds have been initialized and variable numbers have been used to store user input.

Suppose user input is 8

num =8;

First of all we have printed the value of variable first and wearable seconds.

After this the loop starts.

We have assigned the value 3 to the variable i here because we have already printed in two terms(variable first and second) of the series.

in the program loop will be execute like below,

in first first = 0 second = 1 i =3

while(3<=8) {
   next = first+second; //0+1;
   cout<<next; // 1
   first = second; //1
   second = next//1
   3++; // 4
}

now first =1 , second =1 i =4

while(4<=8) {
   next = first+second;//1+1
   cout<<next;// 2
   first = second; // 1
   second = next//2
   4++; // 5
}

now first =1 second =2 i =5

while(5<=8) {
   next = first+second;//1+2
   cout<<next;// 3
   first = second; // 2
   second = next//3
   5++; // 6
}

now first = 2 second =3 i =6

while(6<=8) {
   next = first+second;//2+3
   cout<<next;// 5
   first = second; // 3
   second = next//5
   6++; // 7
}

now first = 3 second =3 i =7

while(7<=8) {
   next = first+second;//3+5
   cout<<next;// 8
   first = second; // 5
   second = next//8
   7++; // 8
}

now first = 5 second = 8 i =8 and in last execution 13 will be print as above manner

in the value of variable i increase by 1 so its become 9 so condition will be such as,

while( 9<=8)

which becoming false so loop will be terminat and we get the following output,

0  1  1  2  3  5  8  13

Generate a large form of table of given number using while and for loop

#include
#include

void main()
{
  clrscr();
  int first,last;

  cout<<"Enter first number: ";
  cin>>first;

  cout<<"Enter last number: ";
  cin>>last;

  int i =1;
 while(i<=10)
  {
     for(int j=f; j<=l;j++)
     {
        cout<<i*j<<"\t";
      }
    cout<<endl;
    i++;
  } 

 getch();
}
OUTPUT:-
Enter first number: 5
Enter last number: 15

5	6	7	8	9	10	11	12	13	14	15	
10	12	14	16	18	20	22	24	26	28	30	
15	18	21	24	27	30	33	36	39	42	45	
20	24	28	32	36	40	44	48	52	56	60	
25	30	35	40	45	50	55	60	65	70	75	
30	36	42	48	54	60	66	72	78	84	90	
35	42	49	56	63	70	77	84	91	98	105	
40	48	56	64	72	80	88	96	104	112	120	
45	54	63	72	81	90	99	108	117	126	135	
50	60	70	80	90	100	110	120	130	140	150