Sunday, May 26, 2013

Area of two triangles - CPP Program

#include<iostream.h>
#include<conio.h>
#include<math.h>
class right_triangle
{
double base;
double height;
   public:
void initialize(double,double);
double area();
double peri();
};
void right_triangle::initialize(double b, double h)
{
base=b;
height=h;
}
double right_triangle::area()
{
return (0.5*base*height);
}
double right_triangle::peri()
{
double hypt;
hypt = sqrt(base*base + height*height);
return base+height+hypt;
}
int main()
{
right_triangle r1,r2;
double bs,ht;
//Initializing triangles
cout<<"\nINPUT\n";
cout<<"\nEnter base of first triangle : ";
cin>>bs;
cout<<"Enter height of first triangle : ";
cin>>ht;
r1.initialize(bs,ht);
cout<<"\nEnter base of second triangle : ";
cin>>bs;
cout<<"Enter height of second triangle : ";
cin>>ht;
r2.initialize(bs,ht);
//Calculating area and perimeter
cout<<"\nArea of first triangle       : "<<r1.area();
cout<<"\nPerimeter of first triangle  : "<<r1.peri();
cout<<"\n\nArea of second triangle      : "<<r2.area();
cout<<"\nPerimeter of second triangle : "<<r2.peri();
return 0;
}


Output:
INPUT
Enter base of first triangle : 7
Enter height of first triangle : 4
Enter base of second triangle : 6
Enter height of second triangle : 2
Area of first triangle       : 14
Perimeter of first triangle  : 19.0623
Area of second triangle      : 6
Perimeter of second triangle : 14.3246
Process returned 0 (0x0)   execution time : 26.804 s
Press any key to continue.

Income Class in C++

#include<iostream.h>
#include<conio.h>
class income
{
double BS;
double DA;
   public:
void initialize(double,double);
double pay_sal();
double deduction();
double calc_tax();
void income_detail();
};
void income::initialize(double b,double d)
{
BS=b;
DA=d;
}
double income::pay_sal()
{
double HRA;
HRA = 0.15 * BS;
return (BS+DA+HRA);
}
double income::deduction()
{
double SC,PF;
SC = PF = 0.08 * BS;
return (SC+PF);
}
double income::calc_tax()
{
double sal,tax,sc;
sal = pay_sal();
sal*=12;
if(sal<100000)
{
tax = 0.2 * sal;
}
else
{
tax = 0.3 * sal;
sc  = 0.1 * tax;
tax+=sc;
}
return tax;
}
void income::income_detail()
{
cout<<"\nBasic Salary is         : "<<BS;
cout<<"\nDearness Allowance is   : "<<DA;
cout<<"\nMonthly Deduction is    : "<<deduction();
cout<<"\nTotal Monthly Salary is : "<<pay_sal();
cout<<"\nTotal Annual Salary is  : "<<12*pay_sal();
cout<<"\nAnnual Payable Tax is   : "<<calc_tax();
}
int main()
{


double bs,da;
income s;
//Initializing Income
cout<<"\nEnter Basic Pay : ";
cin>>bs;
cout<<"\nEnter Dearness Allowance : ";
cin>>da;
s.initialize(bs,da);
s.income_detail();
return 0;
}


Output:
Enter Basic Pay : 5000
Enter Dearness Allowance : 5
Basic Salary is  : 5000
Dearness Allowance is   : 5
Monthly Deduction is    : 800
Total Monthly Salary is : 5755
Total Annual Salary is  : 69060
Annual Payable Tax is   : 13812
Process returned 0 (0x0)   execution time : 36.459 s
Press any key to continue.

Example of Stack in C++

#include<iostream.h>
#include<conio.h>
const int size=10;
class stack
{
int arr[size];
int top;
   public:
void initialize();
void push(int);
int pop();
int stack_top();
void show();
};
void stack::initialize()
{
top=-1;
}
void stack::push(int n)
{
if(top!=size-1)
arr[++top]=n;
else
cout<<"\nOverflow!!!\n";
}
int stack::pop()
{
if(top!=-1)
return arr[top--];
else
{
cout<<"\nUnderflow!!!\n";
return NULL;
}
}
int stack::stack_top()
{
if(top==-1)
{
cout<<"\nStack is Empty!\n";
return NULL;
}
else
return arr[top];
}
void stack::show()
{
if(top==-1)
cout<<"\nEmpty Stack!!!\n";
else
{
for(int i=0;i<=top;i++)
cout<<arr[i]<<" ";
cout<<"<--TOP\n";
}
}
int main()
{
int p;
stack s1;
s1.initialize();
//Pushing Values
cout<<"\nPUSHING 3,5,7 onto stack\n";
s1.push(3);
s1.push(5);
s1.push(7);
cout<<"\nStack is : ";
s1.show();
//Show Top Value
cout<<"\nTop of Stack is :"<<s1.stack_top()<<endl;
//Popping Values
cout<<"\nPOPPING\n";
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
//Show Top of Stack
p=s1.stack_top();
if(p!=NULL)
cout<<"Top of Stack is : "<<p<<endl;
return 0;
}

Output:
PUSHING 3,5,7 onto stack
Stack is : 3 5 7 <--TOP
Top of Stack is :7
POPPING
Popped out value is : 7
Popped out value is : 5
Popped out value is : 3
Stack is Empty!


Complex number

#include<iostream.h>
#include<conio.h>
class complex
{
                float real;
                float imag;
   public:
                void getnum();
                void putnum();
                void sum(complex,complex);
                void dif(complex,complex);
};
void complex::getnum()
{
                cout<<"\nEnter the real part : ";
                cin>>real;
                cout<<"\nEnter the imaginary part : ";
                cin>>imag;
}
void complex::putnum()
{
                cout<<real;
                if(imag<0)
                                cout<<imag<<"i\n";
                else
                                cout<<"+"<<imag<<"i\n";
}
void complex::sum(complex a,complex b)
{
                real=a.real+b.real;
                imag=a.imag+b.imag;
}
void complex::dif(complex a,complex b)
{
                real=a.real-b.real;
                imag=a.imag-b.imag;
}
int main()
{
                complex c1,c2,c3,c4;
                cout<<"\nEnter first complex number\n";
                c1.getnum();
                cout<<"\nEnter second complex number\n";
                c2.getnum();
                //Sum of two inputted numbers
                c3.sum(c1,c2);
                cout<<"\nThe Sum is : ";
                c3.putnum();
                //Difference of two inputted numbers
                c4.dif(c1,c2);
                cout<<"\nThe Difference is : ";
                c4.putnum();


Output:
Enter first complex number
Enter the real part : 4
Enter the imaginary part : 5
Enter second complex number
Enter the real part : 3
Enter the imaginary part : 6
The Sum is : 7+11i
The Difference is : 1-1i

Friday, February 1, 2013

Find Prime Factor of number


/*Program to find out prime factors of a given number.*/

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

void main()
{
int number;
printf("Enter a number ");
scanf("%d",&number);
printf("\nThe prime factors of %d are:",number);
for(int num=2;num<32767;num++)
{
int i=2;
while(i<=num-1)
{
if(num%i==0)
{
break;
}
i++;
}
if(i==num)
{
if(number%num==0)
{
number=number/num;
printf("\t%d",num);
num=1;
}
else
{
continue;
}
}
}
getch();
clrscr();
}

Find Generic root of number


#include <stdio.h>
int main()
{        

int num,i;
printf("Enter any number: ");
scanf("%d",&num);
printf("Generic root: %d", (i = num % 9) ? i : 9);
return 0;
}

Add Two numbers without using operator


#include<stdio.h>

int main(){
  
    int a,b;
    int sum;

    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);

    //sum = a - (-b);
    sum = a - ~b -1;

    printf("Sum of two integers: %d",sum);

    return 0;
}