Tuesday, 27 November 2012

trapezoidal rule using c program


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
float arr[10],sum,integral,h;

printf("Enter the interval: ");
scanf("%f",&h);
printf("Enter Number of terms: ");
scanf("%d",&n);
printf("Enter the values of y:- \n");
for(i=0;i<n;i++)
{
scanf("%f",&arr[i]);
}
sum=(arr[0]+arr[n-1])/2;
for(j=1;j<=n-2;j++)
{
sum=sum+arr[j];
}
integral=h*sum;
printf("Output= %f",integral);
getch();
}


Enter the interval: 1
Enter Number of terms: 7
Enter the values of y:-
81
16
1
0
1
16
81
Output= 115.000000

operator overloading


#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class space
{
public:
int x;
int y;
int z;
space(int a, int b,int c)
{x=a;y=b;z=c;}
void operator --()
{
x=x-1;
y=y-1;
z=z-1;
}
void operator ++()
{
x=x+1;
y=y+1;
z=z+1;
}
void display()
{
cout<<"x: "<<x;
cout<<"\ny: "<<y;
cout<<"\nz: "<<z;
cout<<"\n\n";
}
};

class str
{
public:
char *p;
str()
{}
str(char *tem)
{
p = new char[strlen(tem)+1] ;
strcpy(p,tem);
}
str operator + (str &s)
{
char *temp;
temp= new char[strlen(p) + strlen(s.p)+1];
temp = strcat(p,s.p);
return str(temp);
}
};
int main()
{
space c(10,20,30);
cout<<"Before:\n";
c.display();
c--;
cout<<"After --:\n";
c.display();
space d(1,2,3);
cout<<"Before:\n";
d.display();
d++;
cout<<"After ++:\n";
d.display();

cout<<"\n\nFor binary operator-> ";
str obj1("Gautam");
cout<<"\n\n1st string:"<<obj1.p;
str obj2(" Naik");
cout<<"\n\n2nd string:"<<obj2.p;
str obj3;
obj3 = obj1+obj2;
cout<<"\n\nconcatenated string:"<<obj3.p;
getch();
return 0;
}

Output:
x: 10
y: 20
z: 30

After --:
x: 9
y: 19
z: 29

Before:
x: 1
y: 2
z: 3

After ++:
x: 2
y: 3
z: 4



For binary operator->

1st string:Gautam

2nd string: Naik

concatenated string:Gautam Naik

bank program using classes


#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
class bankaccount
{
char m_name[20];
int m_accno;
char m_type[10];
double m_balance;
char m_pswd[10];
public:
void initial_val()
{
cout<<"\n\nName of the depositer: ";
cin>>m_name;
cout<<"\nAccount No. : ";
cin>>m_accno;
cout<<"\nType of the account (saving/current): ";
cin>>m_type;
cout<<"\nBalance amount in the account : Rs ";
cin>>m_balance;
cout<<"\nPassword: ";
cin>>m_pswd;
}
void deposit()
{
double m;
cout<<"\nAmount to be deposited: Rs ";
cin>>m;
m_balance=m_balance+m;
cout<<"\nYour current balance is: Rs "<<m_balance;
cout<<"\n\n";
}
void withdraw()
{
double n;
char a[10];
cout<<"\nEnter password: ";
cin>>a;
if(strcmp(m_pswd,a)==0)
{
cout<<"\nAmount to be withdrawn: Rs ";
cin>>n;
if(m_balance<=n)
{
cout<<"Balance amount is less ==> amount cannot be withdrawn\n";
}
else
{
m_balance=m_balance-n;
cout<<"\nYour current balance is: Rs "<<m_balance;
cout<<"\n\n";
}
}
else
{
cout<<"\nINVALID PASSWORD !!!\n";
}
}
void display()
{
char b[10];
cout<<"\nEnter password: ";
cin>>b;
if(strcmp(m_pswd,b)==0)
{
cout<<"\n\n\nName of the depositer: "<<m_name;
cout<<"\n\nBalance amount in the account : "<<m_balance;
cout<<"\n\n";
}
else
{
cout<<"\nINVALID PASSWORD !!!\n";
}
}
};

void main()
{
bankaccount b;
int i,c;
cout<<"\n* This is your bank account *\n";
cout<<"\n Enter details";
b.initial_val();
while(1)
{
cout<<"\n1. Deposit\n"<<"2.Withdraw\n"<<"3.Display\n"<<"4. Exit\n";
cout<<"\nEnter your choice: ";
cin>>c;
switch(c)
{
case 1:b.deposit();
break;
case 2:b.withdraw();
break;
case 3:b.display();
break;
case 4:exit(0);
default:cout<<"\n**INVALID**";
break;
}
}
getch();
}

Output:
 Enter details
Name of the depositer: GAUTAM
Account No. : 4546
Type of the account (saving/current): saving
Balance amount in the account : Rs 60000
Password: xyz
1. Deposit
2.Withdraw
3.Display
4. Exit
Enter your choice: 1
Amount to be deposited: Rs 8000
Your current balance is: Rs 68000
1. Deposit
2.Withdraw
3.Display
4. Exit

Enter your choice: 2
Enter password: xyz
Amount to be withdrawn: Rs 56000
Your current balance is: Rs 12000
1. Deposit
2.Withdraw
3.Display
4. Exit
Enter your choice:

saving names of employee using structre


#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
struct emp
{
        char m_name[50];
        int m_id;
        float m_hr;
        float m_payrate;
};
void main()
{
        emp e[20];
        int n,i;
        float monthlysal[20],overtime[20],totalsal[20];
        cout<<"\nenter the number of employees: ";
        cin>>n;
        for(i=0;i<n;i++)
        {
                cout<<"\n\nenter the name of the employee: ";
        cin>>e[i].m_name;
        cout<<"\nenter ID: ";
        cin>>e[i].m_id;
        cout<<"\nenter workload in hours: ";
        cin>>e[i].m_hr;
        e[i].m_payrate=10;
                if(e[i].m_hr>150)
                {
                        monthlysal[i]=150*e[i].m_payrate;
                        overtime[i]=(e[i].m_hr-150)*2*e[i].m_payrate;
                }
                else
                {
                        overtime[i]=0;
                        monthlysal[i]=e[i].m_hr*e[i].m_payrate;
                }
                totalsal[i]=monthlysal[i]+overtime[i];
        }
        cout<<"\nEmployee_Name Workload PayRate   Salary   Overtime Total_Salary\n\n";
        for(i=0;i<n;i++)
        {
                cout<<"     "<<e[i].m_name<<"      "<<e[i].m_hr<<"      "<<e[i].m_payrate;
                cout<<"      Rs."<<monthlysal[i];
                cout<<"     Rs."<<overtime[i]<<"     Rs."<<totalsal[i]<<"\n";
        }
        getch();
}





















Output:

enter the number of employees: 3


enter the name of the employee: rita

enter ID: 11

enter workload in hours: 140


enter the name of the employee: nita

enter ID: 22

enter workload in hours: 150


enter the name of the employee: sita

enter ID: 33

enter workload in hours: 260

Employee_Name Workload PayRate   Salary   Overtime Total_Salary

     rita      140      10      Rs.1400     Rs.0     Rs.1400
     nita      150      10      Rs.1500     Rs.0     Rs.1500
     sita      260      10      Rs.1500     Rs.2200     Rs.3700

Thursday, 13 September 2012

bank program using c++


#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
struct bankaccount
{
       char m_name[20];
       int m_accno;
       char m_type[10];
       double m_balance;
       char m_pswd[10];
};
void initial_val(bankaccount *p);
void deposit(bankaccount *q);
void withdraw(bankaccount *r);
void display(bankaccount *s);
void initial_val(bankaccount *p)
{
       cout<<"\n\nName of the depositer: ";
       cout<<"\n************************************\n";
       cin>>p->m_name;
       cout<<"\nAccount No. : ";
       cout<<"\n************************************\n";
       cin>>p->m_accno;
       cout<<"\nType of the account (saving/current): ";
       cout<<"\n************************************\n";
       cin>>p->m_type;
       cout<<"\nBalance amount in the account : Rs ";
       cout<<"\n************************************\n";
       cin>>p->m_balance;
       cout<<"\nPassword: ";
       cout<<"\n************************************\n";
       cin>>p->m_pswd;
}
void deposit(bankaccount *q)
{
       double m;
       cout<<"\nAmount to be deposited: Rs ";
       cout<<"\n************************************\n";
       cin>>m;
       q->m_balance=q->m_balance+m;
       cout<<"\nYour current balance is: Rs "<<q->m_balance;
       cout<<"\n\n";
}
void withdraw(bankaccount *r)
{
       double n;
       char a[10];
       cout<<"\nEnter password: ";
       cout<<"\n************************************\n";
       cin>>a;
       if(strcmp(r->m_pswd,a)==0)
       {
              cout<<"\nAmount to be withdrawn: Rs ";
              cout<<"\n************************************\n";


              cin>>n;
              if(r->m_balance<=n)
              {
                     cout<<"Balance amount is less ==> amount cannot be withdrawn\n";
                     cout<<"\n************************************\n";
              }
              else
              {
                     r->m_balance=r->m_balance-n;
                     cout<<"\nYour current balance is: Rs "<<r->m_balance;
                     cout<<"\n\n";
              }
       }
       else
       {
              cout<<"\nINVALID PASSWORD !!!\n";
              cout<<"\n************************************\n";
       }
}
void display(bankaccount *s)
{
       char b[10];
       cout<<"\nEnter password: ";
       cout<<"\n************************************\n";
       cin>>b;
       if(strcmp(s->m_pswd,b)==0)
       {
              cout<<"\n\n\nName of the depositer: "<<s->m_name;
              cout<<"\n\nBalance amount in the account : "<<s->m_balance;
              cout<<"\n\n";
       }
       else
       {
              cout<<"\nINVALID PASSWORD !!!\n";
              cout<<"\n************************************\n";
       }
}
void main()
{
       bankaccount b;
       int i,c;
       cout<<"\n* This is your bank account *\n";
       cout<<"\n************************************\n";
       cout<<"\n Enter details";
       cout<<"\n************************************\n";
       initial_val(&b);
       while(1)
       {
              cout<<"\n1. Deposit\n"<<"2.Withdraw\n"<<"3.Display\n"<<"4. Exit\n";
              cout<<"\n************************************\n";
              cout<<"\nEnter your choice: ";
              cin>>c;
              switch(c)
              {
              case 1:deposit(&b);
                     break;
              case 2:withdraw(&b);
                     break;
              case 3:display(&b);
                     break;
              case 4:exit(0);
              default:cout<<"\n**INVALID**";
              break;
       }
       }
       getch();
}

Wednesday, 12 September 2012

finding volume using function overloading


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

void vol(int);
void vol(float, float);
void vol(float);
void main()
{
int ch,s;
float r,h,rad;
do
{
cout<<"----MENU----\n";
cout<<"1. Cube\n";
cout<<"2. Sphere\n";
cout<<"3. Cylinder \n";
cout<<"4. Exit \n";
cout<<"------------\n";
cout<<"Make a choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter side of cube: ";
cin>>s;
vol(s);
break;
case 2:
cout<<"Enter radius of sphere: ";
cin>>r;
vol(r);
break;
case 3:
cout<<"Enter radius of cylinder: ";
cin>>rad;
cout<<"Enter height of cylinder: ";
cin>>h;
vol(rad,h);
break;
case 4:
exit(0);
default:
cout<<"Invalid Choice \n";
break;
}
}
while(ch!=4);
getch();
}
void vol(int s)
{
int v1;
v1=s*s*s;
cout<<"The volume of cube= "<<v1<<"\n\n";
}
void vol(float r)
{
float v2;
v2=(4.0/3.0)*pi*r*r*r;
cout<<"The volume of sphere= "<<v2<<"\n\n";
}
void vol(float rad,float h)
{
float v3;
v3=pi*rad*rad*h;
cout<<"The volume of cylinder= "<<v3<<"\n\n";
}

finding area using function overloading


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

void area(int);
void area(float);
void area(float, float);

void main()
{
int ch,s;
float l,b,r;
do
{
cout<<"-----MENU----- \n";
cout<<"1. Square \n";
cout<<"2. Circle \n";
cout<<"3. Rectangle \n";
cout<<"4. Exit \n\n";

cout<<"Make a choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter side of Square: ";
cin>>s;
area(s);
break;
case 2:
cout<<"Enter radius of Circle: ";
cin>>r;
area(r);
break;
case 3:
cout<<"Enter length of Rectangle: ";
cin>>l;
cout<<"Enter breadth of Rectangle: ";
cin>>b;
area(l,b);
break;
case 4:
exit(0);
default:
cout<<"Invalid Choice \n";
break;
}
}
while(ch!=4);
getch();
}
void area(int s)
{
int area;
area=s*s;
cout<<"The Area of Square= "<<area<<"\n";
}
void area(float r)
{
float area;
area=pi*r*r;
cout<<"The Area of Circle= "<<area<<"\n";
}
void area(float l, float b)
{
float area;
area=l*b;
cout<<"The Area of Rectangle= "<<area<<"\n";
}

function overloading comparing 2 intergers


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

void max(int,int);
void max(float,float);
void main()
{
int a,b;
float c,d;
cout<<"Enter Integer1: ";
cin>>a;
cout<<"Enter Integer2: ";
cin>>b;
cout<<"Enter Float1 :";
cin>>c;
cout<<"Enter Float2: :";
cin>>d;
max(a,b);
max(c,d);
getch();
}
void max(int a,int b)
{
if(a>b)
{
cout<<"The Greater Integer= "<<a<<"\n";
}
else
{
cout<<"The Greater Integer= "<<b<<"\n";
}
}
void max(float c,float d)
{
if(c>d)
{
cout<<"The Greater Float= "<<c<<"\n";
}
else
{
cout<<"The Greater Float= "<<d<<"\n";
}
}

Sunday, 9 September 2012

lagrange's interpolation


#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,i,j,n;
float ans,s,x[10],y[10],prodfunc,sum;

printf("How many terms of x and y Entered: ");
scanf("%d",&n);

printf("Enter values of x:- \n");
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
}
printf("Enter values of y:- \n");
for(j=0;j<n;j++)
{
scanf("%f",&y[j]);
}
sum=0;
printf("Enter the value of x which is to be found: ");
scanf("%f",&ans);

for(i=0;i<n;i++)
{
prodfunc=1;
for(j=0;j<n;j++)
{
if(i!=j)
{
prodfunc=(prodfunc*(ans-x[j]))/(x[i]-x[j]);
}
}
sum=sum+(y[i]*prodfunc);
}
printf("For y(%f)= %f",ans,sum);
getch();
}

Wednesday, 5 September 2012

bank program


#include<iostream>
using namespace std;
#include<conio.h>   
class vector
{
       char name[10],type[15];
       int ac_no;
       float amt;
public:
       void ival();
       void dep();
       void withd();
       void display();
};
void vector::ival()
{
       cout<<"enter name of the account holder :";
       cin>>name;
       cout<<"\nenter account number:";
       cin>>ac_no;
       cout<<"\nenter account type :";
       cin>>type;
       cout<<"\nenter balance :";
       cin>>amt;
      
}
void vector::dep()
{
       int j;
       cout<<"\nenter the amt to be deposited :";
       cin>>j;
       amt=amt+j;
}
void vector::withd()
{
       int s;
       cout<<"\nenter the amt to b withdrawn :";
       cin>>s;
       if(s>amt)
       {
              cout<<"\nmoney cant be withdrawn";
       }
       else
       {
              cout<<"\nmoney can be withdrawn";
              amt=amt-s;
              cout<<"\nbalance amt is :"<<amt;
             
       }
}
void vector::display()
{
       cout<<"name of the account holder :";
       cout<<name;
       cout<<"\n account number:";
       cout<<ac_no;
       cout<<"\n account type :";
       cout<<type;
       cout<<"\n balance :";
       cout<<amt;
}
void main()
{
       vector v;
       int k,m;
       do
       {
       cout<<"1. enter\n"<<"2. deposit\n"<<"3. withdrawal\n"<<"4. display\n";
       cin>>k;
       switch(k)
       {
       case 1:v.ival();
              break;
       case 2:v.dep();
              break;
       case 3:v.withd();
              break;
       case 4:v.display();
              break;
       default:cout<<"function does not exist";
              break;
       }
       cout<<"\npress 1 to continue";
       cin>>m;
       }
       while(m==1);
       getch();
}

sorting array using pointer


#include<iostream>
using std::cin;
using std::cout;
#include<conio.h>
void main()
{
            int *p,n,a[100],i,*j,t;
            cout<<"enter the no of elements: ";
            cin>>n;
            cout<<"\nenter the elements-\n";
            for(i=0;i<n;i++)
            {
                        cin>>a[i];
            }
            for(p=&a[0];p<=&a[n-1];p++)
            {
                        for(j=&a[0];j<=&a[n-1];j++)
                        {
                                    if(*p>*j)
                                    {
                                                t=*p;
                                                *p=*j;
                                                *j=t;
                                    }
                        }
            }
            cout<<"\nthe sorted elements are: ";
            for(p=&a[0];p<=&a[n-1];p++)
            {
                        cout<<*p<<" ";
            }
            getch();
}
           
Output:
enter the no of elements: 8

enter the elements-
-1 0 9 99 -45 5 6 3

the sorted elements are: 99 9 6 5 3 0 -1 -45