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