DevelopersSociety

Tuesday, December 9, 2014

C++ Tutorial- Program- 13- Structures


Code:

#include<iostream>
using namespace std; 
struct studentpersonalinformation{
string name;
string fathersname;
int dob;
string rollno;
char character;
};
main()
{
studentpersonalinformation studentpersonalinformation1;
    cout<<"Enter your Name:";
    cin>>studentpersonalinformation1.name;
    cout<<"Enter Fathers Name:";
    cin>>studentpersonalinformation1.fathersname;
    cout<<"Enter your date of birth:";
    cin>>studentpersonalinformation1.dob; 
    cout<<"Enter your roll no:";
    cin>>studentpersonalinformation1.rollno;
    
cout<<"Select Gender(M/F)"<<endl;
    cin>>studentpersonalinformation1.character;
    
 if(studentpersonalinformation1.character=='m'||studentpersonalinformation1.character=='M')
 cout<<"Your Gender: Male"<<endl;
 else if (studentpersonalinformation1.character=='f'||studentpersonalinformation1.character=='F')
 cout<<"Your Gender: Female"<<endl;
 else
 cout<<"Must Select Gender"<<endl;
 }

Output:


C++ Tutorial- Program- 11- Arrays- One Dimention


Code:

#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:


C++ Tutorial- Program- 17- Pointers


Code:

//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:


C++ Tutorial- Program- 10- Switch Statement

//developerssociety.blogspot.com via- Programmarsarmy.blogspot.com

Code:

#include<iostream>  
using namespace std;
main()
{
int n;
cout<<"Enter any number between 1-6:";
cin>>n;

cout<<"You entered:"<<n<<endl;

cout<<"Name of Students\n"
    <<"================\n"
    <<" 1- Usaid\n 2- Rahim\n 3- Rida\n 4- Aysha\n 5- Jannat\n 6- Imtiaz"
    <<"\n================"<<endl<<endl;
  if(n>0&&n<=10)
  
 switch(n)
{
 case 1:
        cout<<n<<"= usaid "<<endl;
        cout<<"================"<<endl;
  break;
 case 2:
        cout<<n<<"= rahim"<<endl;
        cout<<"================"<<endl;
  break;
 case 3:
        cout<<n<<"= rida "<<endl;
        cout<<"================"<<endl;
  break;
 case 4:
        cout<<n<<"= ayesha "<<endl;
        cout<<"================"<<endl;
  break;
 case 5:
       cout<<n<<"= Jannat "<<endl;
        cout<<"================"<<endl;
  break;
 case 6:
        cout<<n<<"= Imtiaz "<<endl;
        cout<<"================"<<endl;
  break;
}

}

Output:

C++ Tutorial- Program- 9- do while loop


Code:

#include <iostream>
using namespace std;
main()
{
int count=0, times;
    cout<<"How many times loop:";
cin >> times;
do
{
count++;
cout <<count<<endl;
}
while (count<times);
}

Output:


C++ Tutorial- Program- 8- While loop


Code:

#include<iostream>
using namespace std; 
main()
{   
    int a=0;
while(a<3)
{   
   cout<<"a="<<a<<endl;
    a++;
}
}

Output:


C++ Tutorial- Program- 7- Nested for loop


Code:

#include<iostream>
using namespace std; 
main()
{
for(int a=0; a<=2; a++)
for(int b=0; b<=2; b++)
cout<<"a="<<a<<endl;
}

Output: