Welcome

Hi! This blog is for beginners of c++ and java .You can check and pick codes for your programming assignments. This will also help you to understand the concepts of these programming languages. Its all about programming.

BASIC PRACTISE C++(SHOW PASSWORD AS STARS)

Saturday, May 28, 2011

#include<iostream>
#include<conio.h>
#include<windows.h>
#include<string>
using namespace std;
void main()
{

string name="bahria";
string check;
system("cls");
cout<<"\n enter the paswwod\n";
for(int i=0;i<name.length();i++)
{
check+=getch();
cout<<"*";
}
if(name == check)
{
cout<< "\n Corrected !!";
}
else
cout<<"\n Not Correct !!";
getch();
}

Template Practice (Fabonicci Series)

Wednesday, May 25, 2011

#include <iostream.h>
template <class t>
class fs
{
public:
void fab()
{
    t a=0;
    t b=1;
    t c=0;
    t numbers = 0;
    t counter = 1;
    cout << "How many Fibonacci number you need ? : " ;
    cin >> numbers;
    do {
        counter++;
        c=a+b;
        cout<<"\t"<<c;
        a=b;
        b=c;   
    } while (counter <= numbers);
    cout << endl;  
}
};
void main()
{
fs <int> a;
a.fab();
}

Template Practice (Factorial Program)

#include<iostream>
using namespace std;
template<class T>
class fac
{
private:
  T a,f;
public:
 void getdata()
  {
  cout<<"\n Enter the number = ";
  cin>>a;
  }
void fact()
  {
   f=1;
   for (int i=a;i>0;i--)
   {
    f=f*i;
   }
   cout<<"Factorial is: " <<f;
  }
};
void main()
{
fac<int>s;
s.getdata();
s.fact();
}

Templates practice (Averege Of three arrays)

#include<iostream>
using namespace std;
template<class T>
T calc(T a[],int size)
{
 T sum=0,res=0;
 for(int i=0;i<size;i++)
 {
  sum=sum+a[i];
 }
 res=(sum/size);
 return res;
}

void main()
{
 int a[]={2,7,6,8,7,8};
 cout<<"The average is "<<calc(a,6)<<endl;
 float b[]={2.09,7.14,8.7,7.9,8.2};
 cout<<"The average is "<<calc(b,5)<<endl;
 long c[]={200, 600, 8010, 950, 7890, 6788};
 cout<<"The average is "<<calc(c,6)<<endl;

}

Template Practice (Give absolute value)

#include<iostream.h>
#include<conio.h>
template<class T>
T abs(T a)
{
 cout<<"\nabs <"<<a<<">= ";
 return a>0?a:a*(-1);
}
void main()
{
 int a=0;
 float b=0;
 double c=0;

 cout<<"\n Enter integer number ";
 cin>>a;
 cout<<"\n Enter floating point number ";
 cin>>b;
 cout<<"\n Enter double number ";
 cin>>c;
 cout<<abs(a);
 cout<<abs(b);
 cout<<abs(c);
}

 

Template Practice (Sum of three numbers)

#include<iostream.h>
template<class T>
class Add
{
private:
T a,b,c,res;
public:
void getdata()
{
cout<<"\n Enter the ist value = ";
cin>>a;
cout<<"\n Enter the second value = ";
cin>>b;
cout<<"\n Enter the Third value = ";
cin>>c;
}
void display()
{
res = (a+b+c);
cout<<"\n\n Sum is = "<<res<<endl;
}
};
void main()
{
Add<int>s;
s.getdata();
s.display();
Add<float>s1;
s1.getdata();
s1.display();
}

Function Practice(Defination of home function)

Friday, May 20, 2011

#include <iostream.h>
void roof();
void base();
void walk();
int main()
{
    roof();
    base();
    walk();
    return 0;
}
void roof()
{
    cout<<"  /\\"<<endl;
    cout<<" /  \\"<<endl;
    cout<<"/____\\"<<endl;
    return;
}
void base()
{
    cout <<"|    |"<<endl;
    cout <<"|____|"<<endl;
    return;
}
void walk()
{
    cout <<"  **"<<endl;
    cout <<"  **********";
    return;
}

Function Practice(Defination of time difference function)

#include <iostream.h>
int hour(int,int,int);
int minits(int,int,int);
int seconds(int,int,int);
int main()
{
    int hours=0,hours1=0;
    int mints=0,mints1=0;
    int second=0,second1=0;
    char chang=' ';
    cout <<"Enter Hours Of The First Time : ";
    cin >>hours;
    cout <<"\n\nEnter minits of first time : ";
    cin>>mints;
    cout<<"\n\nEnter seconds of the first time : ";
    cin>>second;
    cout <<"Enter Hours Of The second Time : ";
    cin >>hours1;
    cout <<"\n\nEnter minits of second time : ";
    cin>>mints1;
    cout<<"\n\nEnter seconds of the second time : ";
    cin>>second1;
    hours=hours-hours1;
    mints=mints-mints1;
    second=second-second1;
    cout <<"\n\nnow enter how you want the difference \n\th for difference in hours \n\tm for difference in minuts \n\ts for difference in seconds \n\t = ";
    cin >>chang;
    switch (chang)
    {
        case 'h':
            cout <<hour(hours,mints,second);
            break;
        case 'm':
            cout <<minits(hours,mints,second);
            break;
        case 's':
            cout <<seconds(hours,mints,second);
            break;
        default :
            cout <<"\n\n wrong entry ";
    }
    return 0;
}
int hour(int h,int m,int s)
{
    m=m/60;
    s=s/(60*60);
    h=h+m+s;
    return h;
}
int minits(int h,int m,int s)
{
    h=h*60;
    s=s/60;
    m=m+h+s;
    return m;
}
int seconds(int h,int m,int s)
{
    h=h*60*60;
    m=m*60;
    s=s+h+m;
    return s;
}

Functions Basic 3

#include <iostream.h>

float sphere (float);
int main ()
{
    int a=0;
    cout <<"Enter The Radius Of The Sphere : ";
    cin >> a;
    cout <<"\n\nRadius of sphere is " << sphere(a);
    return 0;
}
float sphere (float x)
{
    x=4*3.14*(x*x);
    return x;
}

Functions Basic with Switch statement

Thursday, May 19, 2011

#include <iostream.h>

float f_to_c(float);
float c_to_f(float);
int main ()
{
    int a=0;
    char x=' ';
    cout <<"Enter  What You Want To Do 'f for FAHRENHEIT TO CELSIUS and c for CELCIUS TO FAHRENHEIT  : ";
    cin >> x;
    switch (x)
    {
        case 'f' :
            cout <<"\nEnter Fahrenheit Temprature : ";
            cin >> a;
            cout <<f_to_c(a);
            break;
        case 'c' :
            cout <<"\nEnter Celsius Temprature : ";
            cin >> a;
            cout <<c_to_f(a);
            break;
        default :
            cout <<"\n\n WRONG ENTRY";

    }
    return 0;
}
float f_to_c(float y)
{
    y=(y-32)*(5/9);
    return y;
}
float c_to_f(float z)
{
    z=32+(z*(9/5));
    return z;
}

Functions Basic 2

#include <iostream.h>

double fact (int);
int main ()
{
    int a=0;
    cout <<"Enter Number : ";
    cin >> a;
    cout << fact (a);
    return 0;
}
double fact (int n)
{
    int counter=n;
    double factorial=1;
    while (counter>1)
    {
        factorial=factorial*counter;
        counter--;
    }
    return factorial;
}

Functions Basic

#include <iostream.h>

long int power (int,int);
int main ()
{
    int a;
    int b;
    cout <<"Enter Base Value : ";
    cin >> a;
    cout <<"\nEnter Exponent Value : ";
    cin >> b;
    cout << power (a,b);
    return 0;
}
long int power (int base,int exp)
{
    int counter=1;
    int product=base;
    while (counter<exp)
    {
        product=product*base;
        counter++;
    }
    return product;
}

Diagrams By loops(Final)

#include <iostream.h>

int main() {
    int i=0;
    int j=0;

    //--- for printing 11 asterisks ---
    for (i=0; i<11; i++)
        cout << "*";

    cout << endl;

    //--- for printing 11 asterisks ---
    for (i=0; i<3; i++){
        for (j=0; j<11; j++){
            if (j==0 || j==10)
                cout << "*";
            else
                cout << " ";
        }   // end for loop - with j variable
    cout << endl;
    } // end for

    //--- for printing 11 asterisks ---
    for (int k=0; k < 5; k++){
        if(!(k%2)){
            for (i=0; i<11; i++)
                cout << "*";
            cout << endl;
        }                                 // end if

        //--- for printing spaces between asterisks
        else {
            for (j=0; j<11; j++){
                    if (j==0 || j==10)
                        cout << "*";
                    else
                        cout << " ";
             }                    // end for loop - j variable
          cout << endl;
        } // end else
    } // end for loop - k variable


return 0;
} // end main

Diagrams By loops 4

#include <iostream.h>

int main() {
    int i=0;
    int j=0;

    for (i=9;i>0;i--){            // outer loop - for rows
        for (j=0;j<i;j++){      // inner for loop - for coloumns
            cout << "*";
        }  // end for loop - inner
        cout << " ";             // for space between them.

        for (j=9;j>=i;j--){     // inner for loop - for coloumns
            cout << "*";
        }  // end for loop - inner

        cout << endl;
    } // end for loop - outer

return 0;
} // end main

Diagrams By loops 3

#include <iostream.h>

int main() {
    int i=0;
    int j=0;
    int k=0;

    for (i=0;i<4;i++){         // outer loop - for rows
        for (j=3;j>i;j--){  // inner for loop - for blank spaces
            cout << " ";      // print blank spaces
        }                    // end for loop - inner

        for (k=0; k<=i+j; k++)      // k <=i+j: to put asterisk on left side as well
            cout << "*";
        cout << endl;
    }                     // end for loop - outer

return 0;
} // end main

Diagrams By loops 2

#include <iostream.h>

int main() {
    int i=0;
    int j=0;

    for (i=9;i>0;i--){        // outer loop - for rows
        for (j=0;j<i;j++){ // inner for loop - for coloumns
            cout << "*";
        }  // end for loop - inner
        cout << endl;
    } // end for loop - outer

return 0;
} // end main

Diagrams By loops

#include <iostream.h>

int main() {
    int i=0;
    int j=0;

    for (i;i<9;i++){        // outer loop - for rows
        for (j=0;j<=i;j++){ // inner for loop - for coloumns
            cout << "*";
        }  // end for loop - inner
        cout << endl;
    } // end for loop - outer

return 0;
}

Loops practise(ASCII CODE RETURN at EVERY INDEX)

#include<iostream>
using namespace std;
#include<conio.h>
void main()
{
char a[20];
int i=0;
char *p;
for(i=0;i<=20;i++)
{
a[i]==NULL;
}
cout<<"enter name ";
cin>>a;
for(i=0;i<20 ;i++)
{
p=&a[i];
if(*p!=0)
cout<<"ASCII code at array indexes "<<i<<" "<<int(*p)<<" \n";
else
break;
}

_getch();
}

Loops practise(ASCII CODE RETURN)

#include<iostream.h>
void main()
{
int x=65;
char a;
a=x;
while(x<=122)
{
a=x;
cout<<"ASCII code of "<<a<<" is "<<x<<endl;
x++;
}
}

Loops Practise(Table with DO- While LOOP)

Wednesday, May 18, 2011

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    long int a=0;
    long int b=0;
    cout <<"Enter The Number For Table : ";
    cin >> a;
    cout <<"Enter Table Length : ";
    cin >> b;
    long int x=1;
    do
    {
        cout << a <<"  x  " << x <<" = " << a*x;
        cout <<endl;
        x++;
    }
    while (x<=b);
    getch();
    return 0;

    }

Loops Practise(Table with While LOOP)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    long int a=0;
    long int b=0;
    cout <<"Enter The Number For Table : ";
    cin >> a;
    cout <<"Enter Table Length : ";
    cin >> b;
    long int x=1;
    while (x<=b)
    {
        cout << a <<"  x  " << x <<" = " << a*x;
        cout <<endl;
        x++;
    }
    getch();
    return 0;

    }

Loops Practise(Table with FOR LOOP)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    long int a=0;
    long int b=0;
    cout <<"Enter The Number For Table : ";
    cin >> a;
    cout <<"Enter Table Length : ";
    cin >> b;
    int x=1;
    for (int n=x;n<=b;n++)
    {
        cout << a <<"  x  " << n <<" = " << a*n;
        cout <<endl;
    }
    getch();
    return 0;

    }

Switch statement Practice(Square and Cube)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    int a=0;
    char x=' ';
    cout <<"\n Enter THe Number Whose Squre And Cube You Want To Calculate : ";
    cin >> a;
    cout <<"\n\n Now Press s To Start Calculations   =  ";
    cin  >>x;
    switch (x)
    {
        case 's' :
            a=a*a;
            cout <<"Squre Is : " << a <<endl;
        a=a*a;
        cout <<"Cube Of The Number Is = " << a;
    }
    getch();
    return 0;
}

Switch statement Practice(Unit Convertor)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    float cm=0;
    float inch;
    float feet=0;
    float x=0;
    char function=' ';
    cout <<"    Wellcome To The Unit Converter In C++ ";
    cout <<"\n Enter The character According To Menu \n";
    cout <<"\n 'a' For cm To Inches \n\n 'b' For Inches To cm \n\n 'c' For Feet TO cm \n\n 'd' For cm TO Feet \n\n 'e' For Feet To Inches \n\n 'f' For Inches To Feet \n\n ONLY SMALL LETTERS ARE ACCEPTED\n\n = "; // menu for user
    cin >>function;
    switch (function)
    {
        case 'a' :
            cout <<"\n Enter centimeters : ";
            cin >>cm;
            x=cm/2.5;
            cout <<"\n Inches = "<< x <<endl;
            break;
        case 'b' :
            cout <<"\n Enter Inches : ";
            cin >>inch;
            x=inch*2.5;
            cout <<"\n CM = "<< x <<endl;
            break;
        case 'c' :
            cout <<"\n Enter Feet : ";
            cin >>feet;
            x=feet*30;
            cout <<"\n CM = " << x <<endl;
            break;
        case 'd' :
            cout <<"\n Enter Centimeters : ";
            cin >>cm;
            x=cm/30;
            cout <<"\n FEET = " << x <<endl;
            break;
        case 'e' :
            cout <<"\n Enter FEET : ";
            cin >>feet;
            x=feet*12;
            cout <<"\n INCHES = " << x <<endl;
            break;
        case 'f' :
            cout <<"\n Enter INCHES : ";
            cin >>inch;
            x=inch/12;
            cout <<"\n FEET = " << x <<endl;
            break;
    }
    getch();
    return 0;
}

Switch statement Practice(Area of Circle,Square and Rectangle)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    float r=0;   //for radius
    float w=0;   //for width
    float l=0;   //for length
    float z=0;  //for storing areas
    char area=' ';  //for users choice
    cout <<"Enter The Letter According To Menu\n";
    cout <<" s To Calculate Area Of Squre \n r To Calculate Area Of Rectangle \n c  To Calculate Area Of Circle \n =  ";
    cin >>area;
    switch (area)
    {
        case 's' :
            cout <<"\n Enter Length Of The Side : ";
            cin >> l;
            z=l*l;
            cout <<"\n The Area Of The Squre Is : " << z;
            break;
        case 'r' :
            cout <<"\n Enter The  Length : ";
            cin >> l;
            cout <<"\n Enter The Width : ";
            cin >> w;
            z=l*w;
            cout <<"\n The Area Of The Rectangle Is : " << z;
            break;
        case 'c' :
            cout <<"\n Enter The Radius Of THe Circle : ";
            cin >> r;
            z=3.14*(r*r);
            cout <<"\n The Area Of Circle Is : " << z;
            break;
        }
    getch();
    return 0;
}

Switch statement Practice

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    int student;
    float a=0,b=0;
    float p=0;
    float f=0;
    int n=0;
    char g=' ';
    cout <<"Enter The Number Of Students : ";
    cin >> student;
    cout <<endl;
    while (n<student)
    {
    cout <<"Enter A Grade : ";
    cin >> g;
    switch (g)
    {
        case 'a':
        case 'b':
        case 'c':
        case 'd':    ++p;
        break;
        case 'f':    ++f;
    }
    n++;
    }
    a=((p/student)*100);
    b=((f/student)*100);
    cout << p <<" Students Are Passed  "<< a <<" % "<<endl;
    cout << f <<" Students Are Failed " << b <<" % "<<endl;
    getch();
    return 0;
}

Switch statement Practice

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    const int student=20;
    int p=0;
    int f=0;
    int n=0;
    char g=' ';
    while (n<student)
    {
        cout <<"Enter A Grade : ";
        cin >> g;
        switch (g)
        {
            case 'a':
            case 'b':
            case 'c':
            case 'd':    ++p;
            break;
            case 'f':    ++f;
        }
        n++;
    }
    cout << p <<" Students Are Passed"<<endl;
    cout << f <<" Students Are Failed";
    getch();
    return 0;
}

Loops Practise

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    long int start=0;
    long int last=0;
    long int a=0;
    cout <<"Enter The First Number: ";
    cin >> start;
    cout <<"\n Enter Second Number : ";
    cin >> last;
    for (int n=last;n>=start;n--)
    {
        if (a==0)
            a=last;
        last=last-1;
        a=a+last;
        n=last;
    }
    cout <<"\n Sum Of All Integers is = " <<a;
    getch();
    return 0;

Loops Practise

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    long int last=0;
    cout <<"Enter The Number Till That You Want Sum Of Odd Numbers: ";
    cin >> last;
    long int a=0;
    long int start=1;
    for (int n=start;n<last;n++)
    {
        if (start<=1)
            a++;
        start=start+2;
        a=a+start;
        n=start;
    }
    cout <<"\n Sum Of All Odd Numbers = "<<a;
    getch();
    return 0;
}

Loops Practise

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    const int start=20;
    int last=20;
    for (int number=0;number<start;number++)
    {
        cout << last <<", ";
        last-=1;
    }
    getch();
    return 0;
}

Loops Practise

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    const int finish=10;
    int last=0;
    for (int number=last;last<finish;last++)
    {
        number+=2;
        cout << number <<" , ";
    }
    getch();
    return 0;
}

Loops Practise(Find square)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    const int finish=10;
    int start;
    cout <<"Enter The Starting Number: ";
    cin >> start;
    for (int number=start;number<=finish;number++)
    {
        cout << number <<" squre is : "<< number*number <<endl;
    }
    getch();
    return 0;
}

Loops Practise(Multiple number Program)

#include <iostream.h>
int main ()
{
    int a;
    int b;
    do
    {
        cout <<"Enter First Number = ";
        cin >> a;
        cout <<"\nEnter Scond Number = ";
        cin >> b;
        cout <<endl;
    }
    while (a==0 || b==0);
    if (a%b==0)
    {
        cout << a <<" Is Multiple Of " << b <<endl;
    }
    else
        cout << a <<" Is Not Multiple Of " << b <<endl;

    return 0;

    }

Loops Practise(Power number Program)

#include <iostream.h>
int main()
{
    long int n=0;
    long int p;
    long int a=1;
    cout <<"Enter The Number Whose Power You Want To CAlculate = ";
    cin >> n;
    cout <<"\n Enter Power = ";
    cin >> p;
    cout <<endl;
    if (p==0)
    {    a=1;
    }
    while (p>0)
    {
        a=a*n;
        p--;
    }
    cout <<" Answer Is = " << a;
    return 0;
}

Loops Practise

#include <iostream.h>
int main()
{
    int a=1;
    int b;
    int box=50;
    int shed=75;
    while (a<box)
    {
        b=1;
        while (b<shed)
        {
            if (a==b)

                cout <<"\xDB\xB0\xB0\xB0\xB0\xDB";
            cout <<"\xB0";
                b++;

        }
        a++;
    }
    return 0;
}

Loops Practise

#include <iostream.h>
int main()
    {
    int a=0;
    while (a<4)
    {
    cout <<"\xDB \xDB \xDB \xDB "<<endl;
    cout <<" \xDB \xDB \xDB \xDB"<<endl;
    a=a++;
    }
    return 0;
}

Loops Practise

#include <iostream.h>
int main ()
{
    int a;
    for (a=1;a<=10;a++)
    {
        cout <<"10  x  " << a <<" = " << 10*a;
        cout <<endl;
    }
    return 0;

    }

Loops Practise(Average)

#include <iostream.h>
int main ()
{
    long int v=0;  //v for value
    float c=0;
    long int s=0;  //s for sum
    float avg;  //avg for average
    cout <<"Enter value \n";
    cin >> v;
    while (v!=0) //loop until "v"=o
    {
        c=c++;
        s=s+v;
        cout <<"\n\nEnter another value (0 to get average)) = ";
        cin >> v;
    }
    avg=s/c;
    cout <<"\naverage is = " << avg;
    return 0;

}

Loops Practise(Area of circle)

#include <iostream.h>
int main ()
{
    float radius;
    float area;
    cout <<"Enter Radius (0 To Quit) = ";
    cin >> radius;
    cout <<endl;
    while (radius>0)
    {
        area=3.14*(radius*radius);
        cout <<"The Area Is = " << area <<endl;
        cout <<"\nEnter Radius (0 To Quit) = ";
        cin >> radius;
    } // end while
    return 0;

    }

Loops Practise

#include <iostream.h>
int main ()
{
    int number;
    cout <<"ENTER NUMBER BETWEEN 1&10\n";
    cin >> number;
    while (number<0 || number>10)
    {
        cout <<"NUMBER MUST BETWEEN 1&10\n";
        cout <<"PLEASE RE-ENTER : ";
        cin >> number;
    } // end while
    return 0;

    }

Loops Practise(positive number)

#include <iostream.h>
int main ()
{
    int number;
    cout <<"ENTER POSITIVE NUMBER\n";
    cin >> number;
    while (number<0)
    {
        cout <<"NUMBER MUST BE POSITIVE\n";
        cout <<"PLEASE RE-ENTER : ";
        cin >> number;
    } // end while

    }

If Else Practice(Grade calculator)

#include <iostream.h>
int main()
{
    char  CF,BEE,ENG,PKSTD,PHY,CAL;
    float GPA=0;
    float sum=0;
        cout <<"\n Enter Your Grades For The Following Subjects: ";
        cout <<"\n\n ENGLISH : ";
        cin >> ENG;

        if (ENG=='a')
            sum=sum+12;
        else if (ENG=='b')
            sum=sum+10.5;
        else if (ENG=='c')
            sum=sum+9;
        else if (ENG=='d')
            sum=sum+7.5;
        else if (ENG=='f')
            sum=sum+0;

        cout <<"\n PHYSICS : ";
        cin >> PHY;

        if (PHY=='a')
            sum=sum+12;
        else if (PHY=='b')
            sum=sum+10.5;
        else if (PHY=='c')
            sum=sum+9;
        else if (PHY=='d')
            sum=sum+7.5;
        else if (PHY=='f')
            sum=sum+0;

        cout <<"\n COMPUTERS FUNDAMENTALS : ";
        cin >> CF;


        if (CF=='a')
            sum=sum+16;
        else if (CF=='b')
            sum=sum+14;
        else if (CF=='c')
            sum=sum+12;
        else if (CF=='d')
            sum=sum+10;
        else if (CF=='f')
            sum=sum+0;

        cout << "\n CALCULUS : ";
        cin >> CAL;

        if (CAL=='a')
            sum=sum+12;
        else if (CAL=='b')
            sum=sum+10.5;
        else if (CAL=='c')
            sum=sum+9;
        else if (CAL=='d')
            sum=sum+7.5;
        else if (CAL=='f')
            sum=sum+0;


        cout << "\n BASIC ELOCTRONIC ENGINEERING: ";
        cin >> BEE;


        if (BEE=='a')
            sum=sum+16;
        else if (BEE=='b')
            sum=sum+14;
        else if (BEE=='c')
            sum=sum+12;
        else if (BEE=='d')
            sum=sum+10;
        else if (BEE=='f')
            sum=sum+0;


        cout << "\n PAKISTAN STUDIES : ";
        cin >> PKSTD;

        if (PKSTD=='a')
            sum=sum+12;
        else if (PKSTD=='b')
            sum=sum+10.5;
        else if (PKSTD=='c')
            sum=sum+9;
        else if (PKSTD=='d')
            sum=sum+7.5;
        else if (PKSTD=='f')
            sum=sum+0;
        GPA = sum/20;
        cout << "\n GPA= " << GPA;
        cout << "\n\n Press 0 to exit";
    return 0;
}

If Else Practice(Calculator with if else Statement)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    float fn,sn;  //declaration for users input.
    float x;  //declaration for answer.
    float c,a=1,s=2,m=3,d=4;  //declaration "c" for user input and others are for +,-,*,/.
    cout <<"WELLCOM TO CALCULATOR IN C++" <<endl;
    cout <<"Enter First Number : ";
    cin >> fn;  //first number
    cout <<"\n Enter Opration Sign That You Want To Perform According To Menu" <<endl;
    cout <<"   MENU \n 1 Is For Addition \n 2 Is For Subtraction \n 3 Is For Multiplacation \n 4 Is For Division " <<endl;  //menu
    cout <<" OPRATION SIGN = ";
    cin >> c;
    cout <<"\nEnter Your Scond Number : ";
    cin >> sn;  //scond number
    cout <<endl;
    if (c==a)
    {
        x=fn+sn;
        cout <<" The Sum Is = "<< x <<endl;
    }
    else if (c==s)
    {
        x=fn-sn;
        cout <<"The Difference Is = "<< x <<endl;
    }
    else if (c==m)
    {
        x=fn*sn;
        cout <<" The Multiplacation Is = "<< x <<endl;
    }
    else if (c==d)
    {
        x=fn/sn;
        cout <<"The Division Is = "<< x <<endl;
    }
    getch();
    return 0;
}

If Else Practice(Max , Min OR Equal in three numbers)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    int a,b,c;
    cout <<"Enter Your First Number : ";
    cin >> a;
    cout <<"\nEnter Your Scond Number : ";
    cin >> b;
    cout <<"\nEnter Your Third Number : ";
    cin >> c;
    cout <<endl;
    if (a>b && b>c && a>c) //just like 5,4,3.
    {
        cout << a <<" Is Maximum And " << c <<" Is Minimum" <<endl;
    }
    else if (a<b && b>c && c<a) //just like 4,5,3.
    {
        cout << b <<" Is Maximum "<< c <<" Is Munimum" <<endl;
    }
    else if (a<b && b<c && a<c) //just like 3,4,5.
    {
        cout << c <<" Is Maximum "<< a <<" Is Minumum" <<endl;
    }
    else if (a>b && b<c && c>b)  //just like 4,3,5.
    {
        cout << c <<" Is Maximum "<< b <<" Is Minimum" <<endl;
    }
    else if (a>b && b<c && c<a) //just like 5,3,4.
    {
        cout << a <<" Is Maximum "<< b <<" Is Minimum" <<endl;
    }
    else if (a<b && b>c && c>a)  //just like 3,5,4.
    {
        cout << b <<" Is Maximum "<< a <<" Is Minimum" <<endl;
    }
    else if (a==b && b==c)  //if all are not true
    {
        cout << a <<" , "<< b <<" And "<< c <<" Are Equal" <<endl;   //just like 3,3,3.
    }
    else if (a==b && a>c)
    {
        cout << a <<" And " << b <<" Are equal and "<< c <<" is less then both "<<endl;
    }
    else if (a==b && a<c)
    {
        cout << a <<" , " <<b <<" Are equal and "<<c <<" is greater then both"<<endl;
    }
    else if (a==c && b>c)
    {
        cout << a <<" , " <<c <<" Are equal and " <<b <<" is greater then both "<<endl;
    }
    else if (a==c && a<b)
    {
        cout <<a <<" , " <<c <<" Are equal and " <<b <<" Is less then both "<<endl;
    }
    getch();
    return 0;
}

If Else Practice(Next Odd or Even number)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    int n;
    cout <<"Enter Your Number : ";
    cin >> n;
    cout <<endl;
    if (n%2==0)
    {
        n++;   //n=n+1
        cout << n <<" Is Next Odd Number " <<endl;
    }
    else
    {
        n++;   //n=n+1
        cout << n <<" Is Next Even Number " <<endl;
    }
    getch();
    return 0;
}

If Else Practice(Greator or lesser number Check)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr ();
    int a,b;
    cout << "Enter First Number : ";
    cin >> a;
    cout << "\nEnter Scond Number : ";
    cin >> b;
    cout <<endl;
    if (a>b)
    {
        cout << a << " Is Greater Then "<< b <<endl;
    }
    else if (a<b)
    {
        cout << a << " Is Less Then "<< b <<endl;
    }
    else
    {
        cout << a <<" Is Equal To "<< b <<endl;
    }
    if (a<0)
    {
        cout <<" First Number "<< a <<" Is Negative" <<endl;
    }
    else if (a>0)
    {
        cout <<" First Number "<< a <<" Is Positive " <<endl;
    }
    if (b<0)
    {
        cout <<" Scend Number "<< b <<" Is Negative" <<endl;
    }
    else if (b>0)
    {
        cout <<" Scond Number "<< b <<" Is Positive" <<endl;
    }
    getch();
    return 0;
}

If Else Practice (Positive negative)

#include <iostream.h>
#include <conio.h>
int main()
{
    clrscr();
    int n;
    cout <<"Enter Number: ";
    cin >> n;
    if (n>0)
    {
        cout << "\nThe Number Is Positive" <<endl;
    }
    else
    {
        cout << "The Number Is Negetive" <<endl;
    }
    getch();
    return 0;
}

If Else Practice (EVEN ODD)

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    int n;
    cout <<"Enter Number : ";
    cin >> n;
    if (n%2==0)  //if n is divisible by 2 and remainder is 0.
    {
        cout <<"The Number Is Even\n";
    }
    else
    {
        cout <<"The Number Is Odd";
    }

    getch();
    return 0;
}

If Else Practice

#include <iostream.h>
#include <conio.h>
int main()
{
    clrscr();
    double temp; //declaration
    cout <<"Enter today's temprature (celsius) : ";
    cin >> temp;
    if (temp<0)
    {
        cout <<"freezing " <<endl;
    }
    else if (temp<13)
    {
        cout <<"cool " <<endl;
    }
    else if (temp<41)
    {
        cout <<"hot " <<endl;
    }
    else
    {
        cout <<"very hot " <<endl;
    }
    getch();
    return 0;
}

If Else Practice

#include <iostream.h>
#include <conio.h>
int main ()
{
    clrscr();
    double temp;  //declaration
    cout <<"Enter today's temprature (celsius); ";
    cin >> temp;
    if (temp<5)
    {
        cout <<"Weare a coat today.\n";
        cout <<"It is cold outside| \n";
    }
    else
    {
        cout <<"Don't wear a coat today.\n";
        cout <<"You can manage without it.\n";
    }
    getch();
    return 0;
}

If Else Practice

#include <iostream.h>
#include <conio.h>
int main()
{
    clrscr();
    double temp; //declaration
    cout <<"Enter today's temprature (celsius) :"; //instruction for user
    cin>>temp;
    if (temp<5)
        cout <<"\nWear a coat today " <<endl;
    cout <<"have a great day|";
    getch();
    return 0;
}

If Else Practice (Random numbers)

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int main() {
    int number=0;
    int rand_number=0;
    clrscr();
    cout << "Enter Number " ;
    cin >> number;
    rand_number = rand();    // rand() is a C++ function that generates random numbers
       cout << "Random number = " << rand_number << endl;

       if (number == rand_number)
        cout << "Congrats! you won the game";
     else
        cout << "Oops! You lose the Game.";

    getch();
    return 0;
}

LAB ASSIGNMENT#4 Q2;

Tuesday, May 17, 2011

#include<iostream>
using namespace std;
#include<conio.h>
#include<string>
void main()
{
    string str;
    getline(cin,str);
    int size=str.length();
    for(int i=size-1;i>=0;i--)
    {
        cout<<str[i];
    }
    _getch();
   
   
}

Assignment 4 Question 1

#include<iostream>
using namespace std;
#include<string>
#include<conio.h>
void main()
{
    string str,str2;
    cout<<"Enter First String = ";
    cin>>str;
    cout<<"Enter 2nd String = ";
    cin>>str2;
    str.append(" ");
    str.append(str2);
    cout<<"After concatenation = "<<str;
    getch();
}

LAB ASSIGNMENT 4 Question 3

#include<iostream>
using namespace std;
#include<conio.h>
#include<string>
void main()
{
string str1("Fawad and Ali ");
string str2("Hallo ");
string str("were studying together ");
string str3;
cout<<"str1:"<<str1<<"\nstr2:"<<str2<<"\nstr:"<<str<<endl;
cout<<"Length of str: ";
cout<<str.length();
cout<<"\nSize of str1: ";
cout<<str1.length();
cout<<"\nis str empty ?: "<<str.empty();
str2.replace(1,1,"e");
cout<<"\nreplacing a with e in str2: str2= "<<str2;
str3=str;
cout<<"\nassigning value of str to str3: str3="<<str3;
str1.append(str3,0,22);
cout<<"\nappending str3 with str1: str1="<<str1;
str2.append(str1,10,4);
cout<<"\nappending some part of str1 with str2: str2="<<str2;
cout<<"\ncharacter in str1 at index 2 is: "<<str1.at(2);
cout<<"\ncomparing ali of str1 with str2: "<<str1.compare(11,3,str2,0,6)<<endl;;
string str5=str1.substr(0,5);
string str4=str1.substr(6,4);
str2=str2+str4+str5;
cout<<"after substrings added: str2="<<str2<<endl;
str1.swap(str2);
cout<<"after swapping str2 with str1:\n str2="<<str2<<"\n str1="<<str1<<endl;
cout<<"First occurence of character A in str2: "<<str2.find("A");
cout<<"\nFirst occurence of character a in str2: "<<str2.find("a");
cout<<"\nFirst occurence of character er in str2: "<<str2.find_first_of("er");
cout<<"\n\n\nlast occurence of character A in str2: "<<str2.find_last_of("A");
cout<<"\nlast occurence of character a in str2: "<<str2.find_last_of("a");
cout<<"\nlast occurence of character er in str2: "<<str2.rfind("er")<<endl;;
cout<<"\n\nindex of wltd: Forward search= "<<str2.find_first_of("wltd");
cout<<"\nindex of wltd: Backward search= "<<str2.find_last_of("wltd")<<endl<<endl;
for(int i=0;i<str2.length();i++)
{
if(str2[i]=='a')
{
cout<<"index of a: "<<i<<endl;
}
}
cout<<"after replacing ali to adil in str1:"<<endl<<str1.replace(6,3,"adil")<<endl;
string str6("auzia,ja");
str2.replace(1,1,str6);
cout<<"inserting auzia,ja in str2:"<<endl<<str2<<endl;
cout<<"after erassing:"<<endl<<str2.erase(6,6);

_getch();
}

Understanding of functions

Monday, May 16, 2011


#include<iostream>
using namespace std;
#include<conio.h>
void sum(void);
void dif(int, int);

void main()
{int c,d;
cout<<"enter 2 no of which u want a difrnc"<<endl;
cin>>c>>d;
dif(c,d);
cout<<"back in main\n";


_getch();

}
void sum()
{
int c,d;
static int counter=0;
int sum=0;
cout<<"enter 2 no"<<endl;
cin>>c>>d;
sum=c+d;
cout<<"sum is ="<<sum<<endl;
counter++;
if(counter<5)
dif(c,d);


}
void dif(int c, int d)
{
int difr=0;
difr=c-d;
cout<<"difrence is "<<difr<<endl;
sum();
cout<<"back in diff fun\n";
}

Assignment 3 Question 1&2


#include<iostream>
using namespace std;
#include<conio.h>
#include<string>
class person
{
public:
      virtual void getdata()=0;
      virtual void showdata()=0;
};
class student:public person
{
protected:
      string rol,gpa;
      string ag,nm;
     
public:
     
      void getdata()
      {
            cout<<"Enter name :";
            cin>>nm;
            cout<<"Enter age :";
            cin>>ag;
            cout<<"Enter enrollment number :";
            cin>>rol;
            cout<<"Enter GPA :";
            cin>>gpa;
      }
      void showdata()
      {
            cout<<"Name is : "<<nm<<endl;
            cout<<"Age is : "<<ag<<endl;
            cout<<"Enrollment number is :"<<rol<<endl;
            cout<<"GPA of student is :"<<gpa<<endl;
      }
      string chk()
      {
            return nm;
      }
};
class employee:public person
{
protected:
      string id,ag,nm;
public:
      void getdata()
      {
            cout<<"Enter name :";
            cin>>nm;
            cout<<"Enter age :";
            cin>>ag;
            cout<<"Enter id :";
            cin>>id;
      }
      void showdata()
      {
            cout<<"Name is : "<<nm<<endl;
            cout<<"Age is : "<<ag<<endl;
            cout<<"Id is : "<<id<<endl;
      }
     
};
class lecturer:public employee
{
protected:
      string pub,clpw;
public:
      void getdata()
      {
            employee::getdata();
            cout<<"Enter number of publications :";
            cin>>pub;
            cout<<"Enter classes per week :";
            cin>>clpw;
      }
      void showdata()
      {
            employee::showdata();
            cout<<"Number of publications is : "<<pub<<endl;
            cout<<"Classes per week : "<<clpw<<endl;
      }
      string chk()
      {
            return nm;
      }
};
class director:public employee
{
protected:
      string ms,ys;
public:
      void getdata()
      {
            employee::getdata();
            cout<<"Enter maximum qualification :";
            cin>>ms;
            cout<<"Enter years of service : ";
            cin>>ys;
      }
      void showdata()
      {
            employee::showdata();
            cout<<"Maximum qualification is : "<<ms<<endl;
            cout<<"Years of serice : "<<ys<<endl;
      }
      string chk()
      {
            return nm;
      }
};
class clerk:public employee
{
protected:
      string ovrtm,hrdt;
public:
      void getdata()
      {
            employee::getdata();
            cout<<"Enter Duty hours of clerk : ";
            cin>>hrdt;
            cout<<"enter Overtime of clerk : ";
            cin>>ovrtm;
      }
      void showdata()
      {
            employee::showdata();
            cout<<"Duty hours of clerk : "<<hrdt<<endl;
            cout<<"Overtime of clerk : "<<ovrtm<<endl;
      }
      string chk()
      {
            return nm;
      }
};
void main()
{
      int a;
      student obss;
      lecturer obll;
      clerk obcc;
      director obdd;
      cout<<"__________________Student Input________________"<<endl;
      cout<<"Enter number of students : ";
      cin>>a;
      student *obd=new student[a];
      for(int i=0;i<a;i++)
      {
            obd[i].getdata();
      }
      for(int d=0;d<a;d++)
      {
            for(int s=0;s<a;s++)
            {
                  if(obd[d].chk()<obd[s].chk())
                  {
                        obss=obd[d];
                        obd[d]=obd[s];
                        obd[s]=obss;
                  }
            }
      }

      cout<<"__________________Student Output________________"<<endl;
      for(int k=0;k<a;k++)
      {
            obd[k].showdata();
      }
      cout<<"__________________Lecturer Input________________"<<endl;
      cout<<"Enter number of lecturer`s ";
      cin>>a;
      lecturer *obl=new lecturer[a];
      for(int i=0;i<a;i++)
      {
            obl[i].getdata();
      }
      for(int d=0;d<a;d++)
      {
            for(int s=0;s<a;s++)
            {
                  if(obl[d].chk()<obl[s].chk())
                  {
                        obll=obl[d];
                        obl[d]=obl[s];
                        obl[s]=obll;
                  }
            }
      }
      cout<<"__________________Lecturer Output________________"<<endl;
      for(int k=0;k<a;k++)
      {
            obl[k].showdata();
      }
      cout<<"__________________Director Input________________"<<endl;
      cout<<"Enter number of director`s ";
      cin>>a;
      director *ob=new director[a];
      for(int i=0;i<a;i++)
      {
            ob[i].getdata();
      }
      for(int d=0;d<a;d++)
      {
            for(int s=0;s<a;s++)
            {
                  if(ob[d].chk()<ob[s].chk())
                  {
                        obdd=ob[d];
                        ob[d]=ob[s];
                        ob[s]=obdd;
                  }
            }
      }
      cout<<"__________________Director Output________________"<<endl;
      for(int k=0;k<a;k++)
      {
            ob[k].showdata();
      }
      cout<<"__________________Clerk Input________________"<<endl;
      cout<<"Enter number of clerk`s ";
      cin>>a;
      clerk *obc=new clerk[a];
      for(int i=0;i<a;i++)
      {
            obc[i].getdata();
      }
      for(int d=0;d<a;d++)
      {
            for(int s=0;s<a;s++)
            {
                  if(obc[d].chk()<obc[s].chk())
                  {
                        obcc=obc[d];
                        obc[d]=obc[s];
                        obc[s]=obcc;
                  }
            }
      }
      cout<<"__________________Clerk Output________________"<<endl;
      for(int k=0;k<a;k++)
      {
            obc[k].showdata();
      }

      cout<<"_________________________THANK YOU_________________________"<<endl;

getch();
}