Tuesday, 31 March 2015

while loop

while loop in C++ 





 

Loops are basically use in C++ and other programing language for repetition. Loop repeats the statement or a group of statement again and again until its condition is become wrong then the execution will stop. For example if we want to display the world Pakistan on the screen five time so we will use to write the word Pakistan five time using cout statement like this
cout<<"Pakistan";
cout<<"Pakistan"; 
cout<<"Pakistan";
cout<<"Pakistan";
cout<<"Pakistan";
But to display the word Pakistan using the loop so we use maximum of three line even if we want to display the world Pakistan 100 time or 10000 time in C++ program.
Syntax
int a=1;                    
while (a<10) 
{
cout<<"Pakistan"<<endl; 
a=a+1;                                     
};

The only different with for loop and while loop is that for loop in memory efficient and the while is not memory efficient t because the variable use in the for loop is deleted from the memory when the loop is executed but in the while loop the variable use in the loop is still remain in the memory when the loop is executed.

There are three main parts in while  loop.

1: Starting point Starting point is use to tell the compiler that from where the loop starts. For example start loop form 2 or 0 or 1 basically an integer or character is used for Starting point. For example int a=1; maybe a starting point or char a=’A’; be a starting point.
2: Condition Condition is use to tell the compiler that where the loop ends. For example end loop when 10 or 20 value reaches basically an integer or character is used for ending condition. For example
a<20;
a>30;
3: Increment or Decrement
It is used to increment to increment or decrement the variable used in the loop basically in for loop it is increment by 1 on each time of execution. But we can incremented it by 2 times or 3 time or what ever we want.
For Example
a++;    
0r  
a=a+1;
This will increment the variable by adding 1 to its original value on each time of exaction. We can use the increment operator to increase the value of variable by adding 3 to its original value. a=a+3; This will increment the variable by adding 3 to its original value on each time of exaction.
Example 1
#include<iostream.h>
Int main()
{
int a=1; 
while(a<10)
{
cout<<”Pakistan “<<endl;
};
a++;
system(“pause”);
return 0;
};
Output :
Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan

Example 2
#include<iostream.h>
Int main()
{
int a=1; 
while(a<10)
{
cout<<a<<endl;
};
a++;
system(“pause”);
return 0;
};
Output :1 2 3 4 5 6 7 8 9

Example 3
#include<iostream.h>
Int main()
{
int a=0; 
while(a<10)
{
cout<<a<<endl;
};
a=a+2;
system(“pause”);
return 0;
};
Output : 0 2 4 6 8
Example 4
#include<iostream.h>
Int main()
{
int a=1; 
while(a<10)
{
cout<<a<<endl;
};
a++;
system(“pause”);
return 0;
};

output: 0 3 6 9

Example 5  
#include<iostream.h>
Int main()
{
int a=5; 
while(a<10)
{
cout<<a<<endl;
};
a++;
system(“pause”);
return 0;
};
Output : 5 6 7 8 9 

For loop

For loop in C++





 

For loop in C++ Loops are basically use in C++ and other programing language for repetition. Loop repeats the statement or a group of statement again and again until its condition is become wrong then the execution will stop. For example if we want to display the world Pakistan on the screen five time so we will use to write the word Pakistan five time using cout statement like this
 
cout<<"Pakistan";
cout<<"Pakistan"; 
cout<<"Pakistan";
cout<<"Pakistan";
cout<<"Pakistan";
But to display the word Pakistan using the loop so we use maximum of three line even if we want to display the world Pakistan 100 time or 10000 time in C++ program.
Syntax
 
for(staring point ; condition; increment or decrement)
statement or a group of statements
There are three main parts for loop. 1 Starting point Starting point is use to tell the compiler that from where the loop starts. For example start loop form 2 or 0 or 1 basically an integer or character is used for Starting point. For example int a=1; maybe a starting point or char a=’A’; be a starting point. 2 Condition Condition is use to tell the compiler that where the loop ends. For example end loop when 10 or 20 value reaches basically an integer or character is used for ending condition. For example
 
a<20;
a>30;
 

3 Increment or Decrement It is used to increment to increment or decrement the variable used in the loop basically in for loop it is increment by 1 on each time of execution. But we can incremented it by 2 times or 3 time or what ever we want For Example
 
a++;    
0r
a=a+1;


This will increment the variable by adding 1 to its original value on each time of exaction. We can use the increment operator to increase the value of variable by adding 3 to its original value. a=a+3; This will increment the variable by adding 3 to its original value on each time of exaction.
 Example 1
 
#include<iostream.h>
Int main()
{
for(int a=1; a<10;a++)
cout<<”Pakistan “<<endl;
system(“pause”);
return 0;
};
Example 2
 
#include<iostream.h>
int main()
{
for(int a=5; a<10;a=a+1)
cout<<”Pakistan “<<endl;
system(“pause”);
return 0;
};
Output Pakistan Pakistan Pakistan Pakistan Pakistan 
Example 3
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a++)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 0,1,2,3,4,5,6,7,8,9 
Example 4
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a=a+2)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 0,2,4,6,8 
Example 5
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a=a+3)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 0,3,,6,9
 Example 6
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a=a+2)
cout<<”masterofcplusplus<<endl;
system(“pause”);
return 0;
};
Output Masterofcplusplus Masterofcplusplus Masterofcplusplus Masterofcplusplus Masterofcplusplus 

Example 7
 
#include<iostream.h>
Int main()
{
for(int a=3; a<10;a++)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 3,4,5,6,7,8,9

if statement

if statement in C++


if is the conditional statement in C++ it use to check the condition for example if we have two condition pass and fail so if student marks is less than 33 so then fail must be displayed on the screen and if the student marks in greater the 33 so then the pass must be displayed on the screen. It syntax is given blew.
 Syntax
if (condition)
{
a statement or a group of statement
}

Example 1
#include<iostream.h>
int main()
{
int marks=60;
if(marks>33)
{
cout<<"You are Passed in your examination"<<endl;
}
system("pause");
return 0;
};
This is the simple example which show you that how if statement works. In if statement first the condition is checked and if the condition is true so then the program is executed but if condition is false then program skip the statement which is inside the if statement and then jump to the next line.
Example 2
#include<iostream.h>
int main()
{
int a=5;
int b=6;
if(a==b)
cout<<"a and b are equal"<<endl;
if(a>b)
cout<<"a is greater than b"<<endl;
if(a<b)
cout<<"a is less  than b"<<endl;
system ("pause");
return 0;
};



Example 3
#include<iostream.h>
int main()
{
float GPA;
cout<<"Enter your GPA"<<endl;
cin>>GPA;
if(GPA>2.9)
{
cout<<" You are eligible for Laptop scheme."<<endl;
}
if(GPA<2.9)
{
cout<<"You are not eligible for Laptop sachem."<<endl;
}
return 0;
system("pause");
};

How to Find Factorial of a Number

How to Find Factorial of a Number in C++

 

To find factorial of a number we need two variables and a for loop which is give blew.

#include<iostream>
  #include<iostream.h>
   int main()
   {
   int b;
   int a=1;
   cout<<"enter number to finds its factorial"<<endl;
   cin>>b;
   for(int c=b;c>1;c--)
   {
   a=a*b;
   }
   cout<<a<<endl;
   system("pause");
   return 0;
}
To find Factorial of given number in C++
   #include<iostream>
  using namespace std;
   int main()
   {
   int b=4;
   int a=1;
  
   for(int c=b;c>1;c--)
   {
   a=a*b;
   }
   cout<<a<<endl;
   system("pause");
   return 0;
   };