Monday, 23 July 2012

String Catenation Without Using Strcat Function in C

#include<conio.h>
#include<stdio.h>

void stringconcatenation(char[],char[]);

void main()
{

    char str1[100],str2[100];
    int compare;

    printf("Enter the first string: ");
   gets(str1);

    printf("Enter the second string: ");
    gets(str2);

    stringconcatenation(str1,str2);

    printf(" The String after concatenation: %s",str1);

    getch();

}

void stringconcatenation(char str1[],char str2[])
{
    int i=0,j=0;
  
  
    while(str1[i]!='\0')
    {
         i++;
    }

    while(str2[j]!='\0')
    {
         str1[i] = str2[j];  
         i++;
         j++;
    }

    str1[i] ='\0';
}

Thursday, 19 July 2012

Fibonaaci Series in C


#include<stdio.h>
#include<conio.h>


int main(void)
{
    int n,a=0,b=1,c=0,i=1;
    printf("How Mnay First Fibonacci Numbers you Want To Generate?\t\t");
    scanf("%d",&n);
    printf("%d %d",a,b);
    while(i<=n-2)
    {
        c=a+b;

        printf("%d",c);

        a=b;

        b=c;
       
         i++;
    }


getch();
return 0;
}

Arrays of Objects in C++

#include<iostream>
#include<string>

using namespace std;

class Employee
{
private:
    char name[30];
    int age;
public:
    void setdata();
    void getdata();
};

void Employee::setdata()
{
    cout<<"\nEnter the name == ";
    cin>>name;

    cout<<"\nEnter the age == ";
    cin>>age;
}

void Employee::getdata()
{
    cout<<"\n\nThe name of employee is == "<<name;
    cout<<"\n\nThe age of employee is == "<<age;
}

const int size = 4;

int main()
{
    Employee supervisor[size];

    for(int i=1;i<=size;i++)
    {
        cout<<"\nDeatail of manager "<<i;
        supervisor[i].setdata();
    }
    cout<<"\n";
    for(int j=1;j<=size;j++)
    {
        cout<<"\n\nManager"<<j;
        supervisor[j].getdata();
    }
    getchar();
    return 0;
}

Arrays within a Class in C++

#include<iostream>
#include<string>


/*Let us consider a shopping list of different items for which we place an order with a dealer.This list includes
details such as the code and the price of each item. We would like to perform operations such as adding an item to the list, deleting an item from a list and printing the total value of the order.*/


using namespace std;

const int  val=50;

class ITEM
{
private:
    int item_code[val];
    int item_price[val];
    int count;
public:
    void initiliaze();
    void get_item();
    void display_item();
    void display_sum();
    void remove();
};
void ITEM::initiliaze()
{
    count=0;
}

void ITEM::get_item()
{
    cout<<"Enter the Item code == "<<endl;
    cin>>item_code[count];

    cout<<"Enter the Item cost == "<<endl;
    cin>>item_price[count];
    count++;
}

void ITEM::display_sum()
{
    int sum=0;
    for(int i=0; i<count;i++)
    {
        sum=sum + item_price[i];
    }
    cout<<"The Total Value Of The Cost Is == "<<sum<<endl;
}

void ITEM::display_item()
{
    cout<<"\nCode  Price\n";
    for(int k=0;k<count;k++)
    {
        cout<<"\n"<<item_code[k];
        cout<<"   "<<item_price[k];
    }
}
void ITEM::remove()
{
    int del;
    cout<<"Enter the code you want to remove == ";
    cin>>del;
    for(int search=0; search<count; search++)
    {
        if(del == item_code[search])
        {
            item_price[search]=0;
            item_code[search]=0;
        }
    }
}


int main()
{
    ITEM order;

    order.initiliaze();
    int x;
    do
    {
        cout<<"\n\nYou have the following opton";
        cout<<"\nEnter the Appropriate number";

        cout<<"\n\nPress 1 for ADD AN ITEMS";
        cout<<"\n\nPress 2 for DISPLAY TOTAL VALUE";
        cout<<"\n\nPress 3 for DELETE AN ITEM";
        cout<<"\n\nPress 4 for DISPLAY ALL ITEMS";
        cout<<"\n\nPress 5 for QUIT";

        cout<<"\nEnter The Desired Number == \n";
        cin>>x;
        switch(x)
        {
        case 1:
            {
                order.get_item();
                break;
            }
        case 2:
            {
                order.display_sum();
                break;
            }
        case 3:
            {
                order.remove();
                break;
            }
        case 4:
            {
                order.display_item();
                break;
            }
        case 5:
            break;
        default: cout<<"Incorrect option Please Press the right number == ";
        }

    }while(x!=5);

    getchar();
    return 0;
}

Wednesday, 18 July 2012

OOP: Composition in C++

#include<iostream>
#include<string>

using namespace std;


class employee
{
private: string employee_name;
public: employee(string name)
        {
            employee_name=name;
        }
public: string get_employee_name()
        {
            return employee_name;
        }
public: void show_employee_name()
        {
            cout<<"The name of the employee of this organization is == "<<employee_name<<endl;
        }

};
class organization
{
private: string organization_name;
         employee *myemployee;
public: organization(string name_of_employee, string name_of_organization)
        {
            myemployee=new employee(name_of_employee);
            organization_name=name_of_organization;
        }
public: void show()
        {
            cout<<"The organization name is == "<<organization_name<<endl;
            myemployee->show_employee_name();
        }
};
int main()
{
    string org_name,employ_name;
    cout<<"This is the organization structure"<<endl;
    cout<<"Enter the organization name == ";
    getline(cin,org_name);
    cout<<"Enter the name of employee == "<<endl;
    getline(cin,employ_name);
    organization myorganization(employ_name,org_name);
    myorganization.show();



    getchar();
    getchar();
    return 0;
}

Saturday, 14 July 2012

Recursion: Program to Convert Decimal To Binary


# include <stdio.h>
#include <conio.h>

void Dec_To_Bin (int num);

void main ()
{
int num;
int base;
clrscr();

printf ("Enter the decimal number to convert it binary.\n");
scanf ("%d", &num);


printf ("The %d in binary is : ", num);
Dec_To_Bin (num);

   getch();
}

void Dec_To_Bin (int num)
{

if (num > 1)
{
Dec_To_Bin ((num / 2));
}

printf ("%d", (num % 2));

}

Program To Convert Octal To Binary


#include<stdio.h>
#include<conio.h>
#include<math.h>


float octal_conversion(int octal);

void main()
{
int octal,answer;

clrscr();

printf("program which convert octal to decimal\n\n");

printf("Enter a number in octal form= ");
scanf("%d",&octal );


answer = octal_conversion(octal);
if(answer == 8)
{
printf("wrong");
}
else
{
printf("The result is %d", answer);
}

getch();

}







float octal_conversion(int octal)
{
int square;
int remainder;
int i = 0;
int result=0 ;

while(octal > 0)
{

remainder = octal % 10;
square=pow(8,i);
if(remainder<=7)
{
result = result + square * remainder;

}
else
{

return 8;
}
octal = octal / 10;
i++;

}

return result;
 }