New Android apps UniqueKey

Saturday, 21 March 2015

Inline Function

Inline function: whenever we use inline function in our program, and whenever compiler executes inline function, each time it create a copy of function code and execute the functions.
for use inline function you just have to write "inline" in front of function name.  

#include<iostream>
using namespace std;

inline float area(float radius)
{
  return 3.14*radius*radius;
}

inline float peri(float radius)
{
  return 2*3.14*radius;
}

inline float simple(float amount,float interest,int year)
{
  return amount*(interest/100)*year;
}

main()
{
  int year;
  float amount,radius,interest;
  cout << "Enter radius: ";
  cin >> radius;
  cout << "Area : " << area(radius)<<endl;
  cout << "Perimeter : " << peri(radius)<<endl;
  cout << "Simple Interest Calculator" << endl;
  cout << "Amount: "<<endl;
  cin >> amount;
  cout << "Interest (in %): ";
  cin >> interest;
  cout << "Year: ";
  cin >> year;
  cout << "Simple interest: "<< simple(amount,interest,year)<<endl;
}

Swapping using Function Overloading


#include<iostream>
#include<string.h>
using namespace std;
class swapp{
        public:
        int itemp;
        float ftemp;
        char ctemp[100];

        void swap_it(int num1,int num2)
        {
                cout<<"\nBEFORE SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2;
                itemp=num1;
                num1=num2;
                num2=itemp;
                cout<<"\nAFTER SWAPPING :"<<"\nNUM1: "<<num1<<"\nNUM2 :"<<num2;
        }
        void swap_it(float num1,float num2)
        {
                cout<<"\nBEFORE SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2;
                ftemp=num1;
                num1=num2;
                num2=ftemp;
                cout<<"\nAFTER SWAPPING :"<<"\n"<<"NUM1: "<<num1<<"\nNUM2 :"<<num2;
        }
        void swap_it(char c1[100],char c2[100])
        {
                cout<<"\nBEFORE SWAPPING :"<<"\n"<<"CHAR1 :"<<c1<<"\nCHAR2 :"<<c2;
                strcpy(ctemp,c1);
                strcpy(c1,c2);
                strcpy(c2,ctemp);
                cout<<"\nAFTER SWAPPING :"<<"\n"<<"CHAR1 :"<<c1<<"\nCHAR2 :"<<c2;
        }
};
int main()
{
        swapp swapp;
        int i1,i2;
        float f1,f2;
        char c1[100],c2[100];
        cout<<"\nENTER THE INTEGER VARIABLES";
        cin>>i1>>i2;
        swapp.swap_it(i1,i2);
         cout<<"\nENTER THE FLOAT VARIABLES";
        cin>>f1>>f2;
        swapp.swap_it(f1,f2);
         cout<<"\nENTER THE CHARACTER VARIABLES";
        cin>>c1>>c2;
        swapp.swap_it(c1,c2);
        return 0;
}

Stack operations


#include<iostream>
using namespace std;
int size;
class stack{
        public:
                int stck[100];
                int top;
                int i;
                int element;


                void push(void)
                {
                        cout<<"\nenter the element to push into th stack\n";
                        cin>>element;
                        stck[++top]=element;

                }
                void display(void)
                {
                        for(i=top;i>=0;i--)
                        {
                        if(i==top)
                        {
                        cout<<"TOP OF STACK-->"<<stck[i];
                        }
                        else
                        {
                        cout<<"\t\n"<<stck[i];
                        }
                        }
                }
                int pop(void)
                {
                        return(stck[top--]);
                }
int isfull()
                {
                        if(top==size-1)
                        {
                        return 1;
                        }
                        else
                        {
                        return 0;
                        }
                }
                int isempty()
                {
                        if(top==-1)
                        {
                                return 1;
                        }
                        else
                        {
                                return 0;
                        }
                }
                stack()
                {
                        stck[100]={0};
                        top=-1;
                }

};
int main()
{
        int choice;
        stack stack;
        cout<<"\nenter the size of stack\n";
        cin>>size;
        do
        {
        cout<<"\n..MENU..\n1.push\n2.pop\n3.display.\n4.EXIT";
        cin>>choice;
        switch(choice)
        {
        case 1:
                if(!(stack.isfull()))
                {
                        stack.push();
                }
                else
                {
                        cout<<"\nSTACK IS FULL\n";
                }
                break;
        case 2:
                if(!(stack.isempty()))
                {
                        cout<<"\nTHE POPPED OUT ELEMENT IS :"<<stack.pop();
                }
                else
                {
                        cout<<"\nSTACK IS EMPTY\n";
                }
                break;
        case 3:
                stack.display();
                break;
        case 4:
                cout<<"\nYOU HAVE EXITED\n";
                break;

        }
 }while(choice!=4);
        return 0;
}

Saturday, 7 March 2015

Armstrong number


/*check number is Armstrong number or not*/

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

main()
{
  int num,i,j,temp,sum=0,dig,n;

  printf("enter the number to check\n");
  scanf("%d",&num);

  for(temp=num;temp>0;)
  {
     dig=temp%10;
     sum=sum+(dig*dig*dig);
     temp=temp/10;
  }
  if(num==sum)
  {
   printf("number is a armstrong number\n");
  }
  else
   printf("number is not a armstrong number\n");

}

Print all the characters using ASCII value.


/*print alphabet*/

#include<stdio.h>

main()
{
  int n[122],i,j,num;
   
  for(i=0;i<=121;i++)
  {
    n[i]='0'+i;
    printf("%d  %c\t",n[i],n[i]);
    
  }
}