Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Saturday, 14 July 2012

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;
 }

Calculate HCF in C


#include<stdio.h>
#include<conio.h>
int hcf(int n1 ,int n2);
void main()
{
int n1,n2;
clrscr();
printf("enter the two number =");
scanf("\n%d\n%d",&n1 ,&n2);


printf("\n HCF of two numbers is =%d",hcf(n1,n2));

getch();
}


int hcf(int n1 ,int n2)
{
int i,minimum,u;
if (n1>n2)
{
minimum=n2;
}
else
{
minimum=n1;
}

for(i=minimum;i>=1;i--)
{
if(n1%i==0 && n2%i==0)
{
  u=i;
return u;
  }

}
  }

Tuesday, 10 July 2012

Stack using Arrays in C

#include<stdio.h>


void push(int);
int pop(void);
void display(void);


#define maxsize 100


int stack[maxsize];
int stacktop=0;


int main()
{
    int choice=0,val=0;

    do{
        printf("\n\t1.Push 2.Pop 3.Display 4.Exit\n\tSelect Your Choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                printf("\tElement to be Pushed : ");
                scanf("%d",&val);
                push(val);
                break;
            case 2:
                val=pop();
                if(val!=-1)
                    printf("\tPopped Element : %d\n",val);
                break;
            case 3:
                display();
                break;
            case 4:
                break;
            default:
                printf("\tWrong Choice");
                break;
         }
    }while(choice!=4);
return 0;
}



void push(int val)
{
    if(stacktop<maxsize)
        stack[stacktop++]=val;
    else
        printf("Stack Overflow");
}





int pop()
{
    int a;
    if(stacktop>0)
        {
        stacktop--;
        a=stack[stacktop];
        return a;
    }
    else
    {
        printf("Stack is Empty");
        return -1;
    }
}





void display()
{
    int i=0;
    if(stacktop>0)
    {
        printf("\tElements are:");
        while(i<stacktop){
            printf("\t%d",stack[i++]);
    }
        printf("\n");
    }
    else
        printf("\tStack is Empty\n");
}

Linked list using Queue in C

#include<stdio.h>
#include<malloc.h>

#define MAXSIZE 10


void insertion();
void deletion();
void display();


struct node
{
    int info;
    struct node *link;
}

*n,*temp,*p,*front=NULL,*rear=NULL;

typedef struct node N;


void main()
{
    int ch;
    do
    {
    printf("\n\t\t\tLinked queue");
    printf("\n 1.Insertion");
    printf("\n 2.Deletion");
    printf("\n 3.Display");
    printf("\n 4.Exit");
    printf("\n Enter your choice : ");
    scanf("%d",&ch);
        switch(ch){
        case 1:
            insertion();
            break;
        case 2:
            deletion();
            break;
        case 3:
            display();
            break;
        default:
            break;
    }
}

while(ch<=3);
}




void insertion()
{
    int item;
    n=(N*)malloc(sizeof(N));
    printf("\nEnter the item : ");
    scanf("%d",&item);
    n->info=item;
    n->link=NULL;


    if(front==NULL)
        front=n;
    else
        rear->link=n;
        rear=n;
}





void deletion()
{
    if(front==NULL)
        printf("\nQueue is empty");
    else
    {
        p=front;
        printf("\nDeleted element is : %d",p->info);
        front=front->link;
        free(p);
    }
}





void display()
{
    if(front==NULL)
        printf("\nQueue is empty");
    else{
        printf("\nThe elements are : ");
        temp=front;
        while(temp!=NULL){
            printf("%d",temp->info);
            temp=temp->link;
        }
    }
}   

Linked List Using Stack in C

#include<stdio.h>
#include<malloc.h>
 
void push();
void pop();
void display();

struct node
{
    int info;
    struct node *link;
}


*start=NULL, *n, *temp, *p;

typedef struct node N;

int main()
{
    int ch, a;

    do{
        printf("\t\t\tLinked stack");
        printf("\n 1.Push");
        printf("\n 2.Pop");
        printf("\n 3.Display");
        printf("\n 4.Exit");
        printf("\n Enter your choice : ");
        scanf("%d",&ch);
        switch(ch){
                case 1:
                        push();
                        break;
                case 2:
                        pop();
                        break;
                case 3:
                        display();
                        break;
                case 4:
                        return 0;
                default:
                        printf("\nInvalid choice");
                        break;
        }
    }


while(ch >= 1 && ch <= 3);
}

void push()
{
    n=(N*)malloc(sizeof(N));
    printf("\nEnter the item : ");
    scanf("%d",&n->info);
    n->link=NULL;

    // If stack is empty
    if(start==NULL)
        start=n;

    // Otherwise move to end(top) of the stack.
    else{
        p=start;
        while(p->link!=NULL)
            p=p->link;
        p->link=n;
    }
}

void pop()
{
    // If stack is empty
    if(start==NULL)
        printf("\nStack is empty");                     
    // If there is only one item.
    else if(start->link==NULL){
        printf("\nThe deleted element is : %d",start->info);
        free(start);
        start=NULL;
    }
    // Else, move to last element.
    // 'p' holds last(top) element and 'temp' holds second last element.
    else{
        p=start;
        while(p->link!=NULL){
            temp=p;
            p=p->link;
        }
        printf("\nDeleted element is : %d\n", p->info);
        temp->link=NULL;
        free(p);
    }
}

void display()
{
    if(start==NULL)
        printf("\nStack is empty");
    else{
        printf("\nThe elements are : ");
        p=start;
        while(p!=NULL){
            printf("%d",p->info);
            p=p->link;
        }
        printf("\n");
    }
}

Use of Getter-Setter Function in C++

#include<iostream>
using namespace std;
using std::cout;
using std::cin;

#include<string>
using std::string;
using std::getline;



class book
{
private: string Book_Title;
private: int Book_ISBN;
private: string Author_Name;

  //defualt constructor
  public: book()
  {
  }
 
  //Setter Functions
  public: void setbook(string tit, string A_name, int isbn)
  {
      Book_Title=tit;
      Author_Name=A_name;         
      Book_ISBN=isbn;     
  }

  // getter Functions
  public: string getbook_Title()
  {
      return Book_Title;
       
  }

  public: string getbook_Author_Name()
  {
      return Author_Name;
       
  }

  public: int getbook_ISBN()
  {
      return Book_ISBN;
       
  }

};


int main()
{
  book mybook= book();
  string Author_Name;
  string Book_Title;
  int isbn;
  cout<<"Enter the Author Name == ";
  getline(cin,Author_Name);
  cout<<"\n";
  cout<<"Enter the Book Title == ";
  getline(cin,Book_Title);
  cout<<"\n";
  cout<<"Enter the ISBN NO == ";
  cin>>isbn;
  cout<<"\n";



  mybook.setbook(Book_Title,Author_Name,isbn);

  cout<<"Book TITLE == "<<mybook.getbook_Title()<<endl;
  cout<<"Author Name == "<<mybook.getbook_Author_Name()<<endl;
  cout<<"ISBN NO == "<<mybook.getbook_ISBN()<<endl;
    
getchar();

return 0;
}