The simplest of all the loop . This looping structure is known as while loop, the basic format of while loop in C++ language....
Code:
while(test condition)
{
//Body of the loop
program statement
}
The following points should be remembered while using while loop :---
1. (1)The loop will not get executed if the given condition becomes false.
2. (1)The loop will be executed till the condition is false the result will be presented as soon as the condition becomes false.
3. There must be some looping termination inside the loop to avoid abnormal output..
Here are some e.g. To clear ur doubts........
Note:-- Plz download the attached documents to know better programming through while loop and all
* Program to show no. From 1-100 using while loop..
Code:
#include
#include
void main()
{
clrscr();
int i;
i=1;
while(1<=100)
{
cout<< i<<",";
i++;
}
getch();
}
Here the condition is that the loop should run till the value of i becomes 100 as soon as it becomes 100 the loop ends and the output will be shown.
Firstly the value is 1 then the condition is fulfiled that 1 is less than 100 prog. Will go inside the loop the 1'st statement is cout<< i the initial value of i is 1 so 1 will be the 1'st output then [b]acc/que[/B] the second value should be 2 then 3 and so on upto 100 so we have to write i++ so there will be an increment of 1 in the value of i and the loop goes upto 100......
* Program to add value upto 100..
I am directly starting from void main() so dont get confused ....
Code:
void main()
{
clrscr();
int i,s;
i=1;
s=0;
while(i<=100)
{
s=s+i;
i++;
}
cout<<"Sum =" <<" "<< s;
getch();
}
Here I had taken 2 variables i for taking the values from 1-100 and sum for adding those values... I had asssigned the value of s=0 because 0 is an additive properties of add.. If u will add some no.. to 0 the no. Remains same ....Similarly 1 is the multiplicative prop. Of Multi.. . Here first the value of i is 1 then we add the value into s. The initila value of s is 0 so after add.. the value become 1 then i got incremented again loop will run and again there will be addition. And the vaue of s was 1 after adding 2 it becomes 3 ad the loop goes on till the value of i becomes 100...
* To find out the factorial of any no..
Code:
void main()
{
int n,f;
f=1;
cout<<"Enter the value of i to find the factorial\n";
cin>>n;
while(n!=0) or (n>0)or (n>=1)
{
f=f*i;
i--;
}
cout<<"Factorial ="<<" " << f;
getch();
}
Here the user will in enter the no.. and we have to find the factorial. For ur info.. telling as most of u had forgotten what is factorial, the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive.
For e.g. :- Fact. Of 5 :--- 5*4*3*2*1= 120.
So to find the factotorial the prdecessor no. Includin the no should be mulitiplied... To find this i had taken a variable in which the entered value will be stored. And variable f to hold fac. Value.. First I had given the value of f 1 as i had told 1 is the multiplicative prop. Of Multi.... SO multiplying an no with 1 will give same no.. Suppose user enters th value 5 first the condition will be checked. I had given 3 cond. All 3 are correct but u had to use any one of em.. So prog. Will enter the loop then the no will be multi . With f and the value of n will get decremented again it will get multi. Till the condition becomes false . And finally the output wil be given
No comments:
Post a Comment