Wednesday, 16 April 2014

For loop Basic Program- Using C++

#include<iostream>
using namespace std;
int main()
{
int i;

for (i = 0; i < 10; i++)
{
cout << "Hello" << "\n";
cout << "There" << "\n";
}
return 0;
}

Output:


For Loop- Counter Program- Using C++

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{

     // Variable Declaration
     int a;

     // Get Input Value
     cout<<"Enter the Number :";
     cin>>a;

     //for Loop Block
     for (int counter = 1; counter <= a; counter++)
     {
         cout<<"Execute "<<counter<<" time"<<endl;
     }

     // Wait For Output Screen
     getch();
     return 0;
 }

Output:



Factorial Using C++

#include<iostream>
using namespace std;
int main()
{
    int num,factorial=1;

    cout<<" Enter Number To Find Its Factorial:  ";

    cin>>num;

    for(int a=1;a<=num;a++)

    {

        factorial=factorial*a;

    }
cout<<"Factorial of Given Number is ="<<factorial<<endl;
    return 0;
}

Output:


Tuesday, 15 April 2014

Representation of a Linked List In a Memory

#include<iostream>
using namespace std;

struct node
{
   int data;
   node *link;
};
node *head;

main()
{ head= new node;
 head->data=405;
 head->link=NULL;
 cout<<"The Value in the starting node= "<<head->data<<endl;}

Output:

Saturday, 12 April 2014

Array Using C++

Array: An array is a variable that holds multiple values of the same type.
Arrays and pointers have a special relationship as arrays use pointers to reference memory locations. Syntax is :
datatype arrayname[array size];

#include <iostream.h>   
#include <iomanip.h>   //we use this header file for set w OR Width 

main()
{
   int arr[ 10 ]; // its an integer type array of size 10

/*initialize elements of (array) i.e start with 0 and n-1 means 0to9         
  for sum using for loop*/
  
   for ( int i = 0; i < 10; i++ )
   {
     arr[ i ] = i + 100;
   // i.e calculation 100 =0 + 100 to 109= 9+100

   }
   
cout << "Array Members" << setw( 12 ) << "Values" << endl;

   // again for loop for output/ display                      

   for ( int j = 0; j < 10; j++ )
   {
      cout << setw( 6 )<< j << setw( 17 ) << arr[ j ] << endl;
   }

   return 0;

}

Output:



Pointers Using C++ Program 1

//Pointers: A pointer is a variable that contains the address of a variable.

#include<iostream>
using namespace std;
main()
{
int x=100;   // initialize integer type variable name x
int *y;     // declare integer type pointer y

    cout<<x;  // print value of integer type variable x
    cout<<"\n";  // new line

  y=&x;        // giving address of variable x to y
*y=5000;    // without accessing x changing value of x=100 to 5000

cout<<x;     // after, poiting now, value of x is : 5000
cout<<"\n";  // new line

int **z;     // declare integer type pointer to pointer c
z=&y;        // giving address of y to z
**z=90;     // now, without disturbing x&y value of x is 90
cout<<x;    //value of x is 90

}

Output:


Nested For Loop Using C++ Program(2)

#include<iostream>  
using namespace std;
main()   
{

for(int i=1;i<5;i++)      
    {
cout<<i<<"\t";
for(int j=1;j<5;j++)  
cout<<j;
cout<<"\n";
  }
}

// پہلا لوپ ایک بار چلے گا تو دوسرا والا لوپ 4  بار چلے گا اور پھر پہلا والا لوپ دوسری بار چلے گا تو دوسرا 


//لوپ 4 بار چلے گا پھر اسی طرح چار بار پہلا لوپ چلے گا   

OutPut: