SWAP Two No Code

//Swap Two No.

#include<stdio.h>
main()
{
 int a,b,c;
 printf("enter the value of a,b,c");
 scanf("%d%d",&a,&b);
 c=a;
 a=b;
 b=c;
 printf("%d%d",a,b);
}

Add Two No Using Pointer Code

//Add Two No. using Pointer

#include<stdio.h>
int add(int *a,int *b);
int main()
{
 int a,b,x;
 printf("enter two no to add\n");scanf("%s%s",&a,&b);
 x=add(&a,&b);
 printf("Addition = %d ",x);
}
int add(int *p,int *q)
{
 int *p,*q,y;
 y=*p+*q;
 return(y);
}

Add Two Matrix code

//Matrix Addition.

#include<stdio.h>
main()
{
 int a[10][10],b[10][10],c[10][10];
 int m,n,i,j,sum=0;
 printf("enter the no of rows and col in matrix\n");
 scanf("%d%d",&m,&n);
 
 
 for (i=0;i<m;i++)
{
 for(j=0;j<n;j++)
 {
  printf("enter the data in array a[%d][%d] And b[%d][%d]\n",i,j,i,j);
  scanf("%d%d",&a[i][j],&b[i][j]);
 }
}


for (i=0;i<m;i++)
{
 for(j=0;j<n;j++)
 {
  c[i][j]=a[i][j] + b[i][j];
 }
}
printf("sum of matrix a And b :");
 for (i=0;i<m;i++)
{
 for(j=0;j<n;j++)
 {
  printf(" %d  ",c[i][j]);
 }
}
}

Basic "Structure" Code

//Structure input / Output

#include<stdio.h>
struct stu {
 int h,e,m;
 char na[10];
 int rno;
};typedef struct stu stu;
void main()
{
stu b;
printf("enter the name of student and roll no\n");
scanf("%s%d",b.na,&b.rno);
printf("enter the marks of 3 subjects hindi english and maths\n");
scanf("%d%d%d",&b.h,&b.m,&b.e);
printf("\nName : =%s\nRoll No : =%d\n",b.na,b.rno);
printf("\nMarks of three subjects :\n Hindi : = %d\nEnglish : = %d\nMaths : = %d\n",b.h,b.e,b.m);
}

Prime Number Or Not Code

//Prime no.
#include<stdio.h>
main()
{
 int n,i,k=0;
 printf("enter value");
 scanf("%d",&n);
 for(i=2;i<n;i++)
 {
  if(n%i==0)
  k=1;
 }
 { 
 if(k==0)
 printf("it is a prime no");
 else
 printf("it is not a prime no");
    }
}


Hcf and Lcm Of Two No.

//Hcf and Lcm.

#include<stdio.h>
main()
{
 int a,b,c,lcm,i;
 printf("enter two no\n");
 scanf("%d%d",&a,&b);
 if(a>b)
 c=b;
 else
 c=a;
 for(i=c;i>=1;i--)
 {
  if(a%i==0&&b%i==0)
 {
  printf("\nHCF of %d and %d is : %d",a,b,i);
  break;
 }
 }
 lcm=a*b/i;
 printf("\nLCM of %d and %d is : %d",a,b,lcm);
 
}

Factorial Using CALL BY Refrance

//Fact Using call by Refrance.

#include<stdio.h>
main()
{
 int n,f;
 int fact(int*);
 printf("enter value");
 scanf("%d%d",&n,&f);
 f=fact(&n);
 printf("factorial=%d\n");
}
int fact(int *x)
{
 int n,i,fact;
 for(i=1;i<=n;i++)
 fact=fact*i;
 return(fact);
}

Factorial

//Factorial.

#include<stdio.h>
main()
{
 int i,n,fact=1;
 printf("enter value");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
  fact=fact*i;
 }
 printf("factorial of value=%d\n",fact);
}

Fibonacci Series

//Fibonacii series.

#include<stdio.h>
main()
{
 int i,first=0,second=1,range,next;
 printf("Enter the range of fibonacii series\n");
 scanf("%d",&range);
    printf("FIBONACII SERIES : %d %d ",first,second);
 for(i=2;i<range;i++) 
 {
  next=first+second;
  first=second;
  second=next;
  printf("%d ",next);
 }
} 

Factorail Using RECURSSION.

//Factorial Using Recurssion.

#include<stdio.h>
long factorial(int);
int main()
{
 int n;long f; printf("enter no\n");scanf("%d",&n);
 if(n<0)
 printf("negative no is not allowed\n");
 else
 {
  f=factorial(n);
  printf("%d!=%ld",n,f);
 }
}
long factorial(int n)
{
 if(n==1)
 return 1;
 else{
  return(n*factorial(n-1));
 }
}

Amstrong No Code

//Amstrong No.

#include<stdio.h>
main()
{
 int n,sum=0,r,k;
 printf("enter value");
 scanf("%d",&n);
 k=n;
 while(n>0)
 {
 r=n%10;
 sum=sum+r*r*r;
 n=n/10; 
 }
     if(sum==k)
     {
     printf("it is an armstrong no");
     }
 else
      printf("it is not an armstrong no");
}

Maximum and Minimum in Array Code

// Max and Min in Array.

#include<stdio.h>
main()
{
 int a[100],size,max,min,i;
 printf("Enter size Of Array\n");
 scanf("%d",&size);
 
 printf("Enter  Values\n");
 for(i=0;i<size;i++)
  scanf("%d",&a[i]);
 //Now consider first value maximum and minimum//
 max=a[0];min=a[0];
 
 for(i=0;i<size;i++)
 {
  if(a[i]>max)
  max=a[i];
     if(min>a[i])
     min=a[i];
 }
 printf("Maximum=%d\nMinimum=%d",max,min);
}

Stack Using Link List Code

// Stack Using Link List

#include<stdio.h>
#include<process.h>
#include<malloc.h>
struct link{
 int data;
 struct link *next;
};
typedef struct link node;
node* top=NULL;
main()
{
 void push(int);int pop();void display();
 int data,ch,x;
 printf("Menu\n1.Push\n2.POP\n3.Display\n4.Exit\n");
 do{
  printf("\nEnter your Choice\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("Enter data\n");
    scanf("%d",&data);
    push(data);
    break;
    case 2:
     x=pop();
     printf("Deleted data is = %d\n",x);
     break;
     case 3:
      display();
      break;
      case 4:
       exit(1);
       default:
        printf("Wrong choice\n");
  }
  
 }while(ch!=4);
}
void push(int x)
{
 node* nnode;
 nnode=(node*)malloc(sizeof(node));
 nnode->data=x;
 nnode->next=top;
 top=nnode;
}
int pop()
{
 int k;
 node* t;//we took t node to make memory free of node t
 t=top;
 if(top==NULL)
 printf("\nThere is no data\n");
 else{
  k=top->data;
  top=top->next;
  return(k);
  free(t);
 }
}
void display()
{
 node* i;
 if(top==NULL)
 printf("Nothing to Display\n");
 else{
  i=top;
  for(i=top;i!=NULL;i=i->next)
  {
   printf("%d ",i->data);
  }
 }
}

Tower of Hanoi Code

//Tower Of Hanoi.

#include<stdio.h>
main()
{
 char A,B,C;
 int n;
 void TOH(char,char,char,int);
 printf("Enter No Of disc of TOH\n");
 scanf("%d",&n);
 TOH('A','B','C',n);//Alert when you pass character then it must be in
 //single qoute otherwise it will pass garbage value;
}
void TOH(char t1,char t2,char t3,int n)
{
 if(n<=0)
 printf("Negative Entry is Wrong\n");
 else if(n==1)
  printf("Move TOP disk from tower %c to tower %c\n\n",t1,t3);
 else
 {
  TOH(t1,t3,t2,n-1);
  TOH(t1,t2,t3,1);
  TOH(t2,t1,t3,n-1);
 } 
}

Link List Code

// Link List


#include<stdio.h>
#include<stdlib.h>
#include<process.h>
struct link
{
int data;
struct link *next;
};
typedef struct link node;
node *head,*current,*nnode = NULL;
int main()
{
 
 int data,ch,x,p;
 node *k;
 void createHead(int);   void insertAtend(int);  void traverse();
 void insertAp(int,int); void insertBp(int,int);
 int deleteNode(int);        node* searchData(int);
 printf("\t\t\t\t\t***MENU***\n\n\t\t1.CreateHead\t\t2.Insert at End\t\t3.Traverse List\n\n\n"
 "\t\t4.Insert After Postion\t\t5.Insert Before Position\n\n\n\t\t6.Delete Node\t\t7.Search Data\n\n");
do
{
 printf("\nEnter Your choice\n\n");scanf("%d",&ch);
 switch(ch)
 {
  case 1:
   printf("\nEnter data to insert\n");scanf("%d",&data);
   createHead(data);
   break;
   case 2:
    printf("\nEnter Data to Insert at End\n\n");scanf("%d",&data);
    insertAtend(data);
    break;
    case 3:
     traverse();
     break;
     case 4:
      printf("\nEnter Position And Data to Insert\n\n");scanf("%d%d",&p,&data);
      insertAp(p,data);
      break;
      case 5:
       printf("\nEnter Position And Data to Insert\n\n");scanf("%d%d",&p,&data);
      insertBp(p,data);
      break;
      case 6:
       printf("\nEnter Position From Where to Delete\n\n");scanf("%d",&p);
       x=deleteNode(p);
       printf("\nDeleted Data is :-  %d",x);
       break;
      case 7:
       printf("\nEnter Data to search in Link List\n\n");
       scanf("%d",&data);
       k=searchData(data);
       if(k==NULL)
       {
        printf("\nData Not Found\n\n");
       }
       else
       {
        printf("\nData Found at Address =  %u",k);
        break;
       }
      case 8:
       exit(1);
       default:
        printf("\nWrong Choice Try Again\n\n");
 }
}while(ch!=8);
}
void createHead(int x)
{
 if(head!=NULL)
  printf("\n\t\t**Head already Created**\n\n");
 else
 {
 nnode=(node*)malloc(sizeof(node));
 nnode->data=x;nnode->next=NULL;
 head=nnode;
    }
    current=nnode;
}
void insertAtend(int x)
{
 if(head!=NULL)
 {
 nnode=(node*)malloc(sizeof(node));
 nnode->data=x;nnode->next=NULL;
 current->next=nnode;current=nnode;
    }
    else
    printf("\n\t\t**First Create Head**\n\n");
}
void traverse()
{
 node *i;
 if(head==NULL)
 printf("\n\t\t***There is No Data in Link List\n\n");
 else
 {
 printf("\nData in Link List is :- ");
 for(i=head;i!=NULL;i=i->next)
 {
  printf("%d\t",i->data);
 }
  }
}
void insertAp(int p,int x)
{
 node *temp;
 int i;
 nnode=(node*)malloc(sizeof(node));
 nnode->data=x;
 temp=head;
 for(i=0;i<p;i++)
 {
 temp=temp->next;
 }
  nnode->next=temp->next;
  temp->next=nnode;
}
void insertBp(int p,int x)
{
  node *temp;
 int i;
 nnode=(node*)malloc(sizeof(node));
 nnode->data=x;
 temp=head;
 for(i=0;i<p-1;i++)
 {
 temp=temp->next;
 }
  nnode->next=temp->next;
  temp->next=nnode;
}
int deleteNode(int p)
{
 node *t,*q;
 int i,k;
 t=head;
 for(i=1;i<p-1;i++)
 {
  t=t->next;
 }
 q=t->next;
 t->next=t->next->next;
 k=q->data;
 return(k);
}
node* searchData(int x)
{
 node *i;
 for(i=head;i!=NULL;i=i->next)
 {
  if(i->data==x)
  break;
 }
 return(i);
}

Binary Search In Array

//Binary Search.

#include<stdio.h>
main()
{
int a[5],i,high=4,low=0,mid,x,flag=1;
 printf("Enter array\n");
 for(i=0;i<5;i++)
 scanf("%d",&a[i]);
 printf("Enter element to search\n");
 scanf("%d",&x);
 while(low<=high&&flag==1)
 {
  mid=high+low/2;
  
  if(x<a[mid])
  high=mid-1;
  else if(x>a[mid])
  low=mid+1;
  else
  {
  flag=0;
  mid=mid+1;
  printf("Data found at = %d Positon",mid);
     }   
 }
 if(flag==1)
 {
  printf("Data Not found\n");
 }
}

Linear Search In array code

// Linear Search

#include<stdio.h>
main()
{
 int a[5],i,x,pos;
 printf("Enter array\n");
 for(i=0;i<5;i++)
 scanf("%d",&a[i]);
 printf("Enter element to search\n");
 scanf("%d",&x);
 for(i=0;i<5;i++)
 {
  if(x==a[i])
  {
   pos=i;
   break;
  }
 }
 if(i==5)
 printf("Element not Found\n");
 else
 {
  printf("Element found At postion = %d",pos);
 }
}

Circular Queue Code

//Circular Queue

#include<stdio.h>
#include<process.h>
int front=0,rear=-1,q[5];
int main()
{
 void insertion(int);int deletion();void display();
 int x,data,ch;
 printf("\n\n***MENU For Circular Queue***\n\n1.Insertion\n\n2.Deletion\n\n3.Display\n\n4.Exit\n\n");
 do
 {
  printf("\n\nEnter your choice\n\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("\n\nEnter data to insert in Queue\n\n");
                scanf("%d",&data);
    insertion(data);
    break;
    case 2:
     x=deletion();
     printf("\n\nDeleted data is %d\n\n",x);
     break;
    /*here if you remove break; statememt from program then after deletion
    it will move to case 3 and call dispaly function*/
     case 3:
      display();
      break;
      case 4:
       exit(1);
       default:
        printf("\n\nWrong Choice Try again\n\n");
       
  }
 }while(ch!=4);
 
}
void insertion(int x)
{
 /* case 1- when rear is at end and front is at zero means 
 CQ is Full*/
 if(rear==4&&front==0)
 {
 printf("\nCircular Queue is Overflow\n\n");
    }
    /* Case 2- its intial condition when CQ is empty 
    this is intial insertioin*/
 else if(rear==-1&&front==0)
 {
  rear++;
  q[rear]=x;
 }
 /* Case 3- its very important condition normally in Q and DQ it is Condition 
 of Underflow But here its a condition for Overflow Because in CQ Fifo
 is followed and this condition is only true when CQ is full*/
 else if(rear==front-1)
 {
  printf("\nCircular Queue is Full\n\n");
 }
 /* Case 4- here rear is at end and front is greater than zero means 
 behind front there is place empty for insertion and we know insertion is from 
 rear so we have to make rear zero then insert*/
 else if(rear==4&&front>0)
 {
  rear=0;
  q[rear]=x;
 }
 /* Case 5- its a Normal condition Other than above 4 for inserion*/
 else
 {
  rear++;
  q[rear]=x;
 }
 
}

int deletion()
{
 int x;
 if(rear<0)
 printf("\nCircular Queue is Empty\n\n");
 else
 {
  /* if you are in else part then you atleast one element in the 
  queue and we have to return that value*/
  x=q[front];
  /* here if you are left with single element in the queue so 
  make front and zero at intial postion and return value*/
  if (front==rear)
  {
   front=0;rear=-1;
  }
  /* if front has reached at last deleting the element and rear is coming from the back
  entering rest of the element then we have make front zero*/
  else if(front==4)
  {
   front=0;
  }
  /* after returning value normally we have to move to next value */
  else
  {
   front++;
  }
  return(x);
 } 
}

void display()
{
 int i;
 if(rear==-1&&front==0)
 {
 printf("\nCircular Queue is empty\n\n");
    }
 /* lets rear is greater than and equal to front so there might be only one element inserted 
 in that case both rear and front will be at zero OR both rear and front be at last element of CQ*/
 else if(rear>=front)
 {
  for(i=front;i<=rear;i++)
  {
  printf("%d ",q[i]);
     }
 }
 else
 {
  /* if you are in else it means you more than one element and we will print the CQ from
  front to rear*/
  for(i=front;i<5;i++)
  {
  printf("%d ",q[i]);
     }
  for(i=0;i<=rear;i++)
  {
  printf("%d ",q[i]);
  }
 }
}

Stack(Data Structure) code

// Stack Code

#include<stdio.h>
#include<process.h>
int stack[5],top=-1;
main()
{
 int data,ch,i,x;
 printf("Menu\n1.Push\n2.POP\n3.Display\n4.Exit");
 do{
  printf("\nEnter choice no\n");
  scanf("%d",&ch);
 switch(ch)
 {
  case 1:
   printf("Enter Data\n");
   scanf("%d",&data);
   if(top==4)
   printf("Stack is Overflow");
   else
   {
    top++;
    stack[top]=data;
   }
   break;
   case 2:
    if(top==-1)
    printf("Stack is UnderFlow\n");
    else{
    x=stack[top];
    top--;
    printf("Deleted Data is = %d",x);}
    break;
    case 3:
     i=top;
     for(i=top;i>=0;i--)
     {
      printf("%d ",stack[i]);
     }
     break;
     case 4:
      exit(1);
      default:
       printf("Wrong choice try again\n");
 }
}while(ch!=4);
}

Doubly Ended Queue Code

//Doubly Ended Queue

#include<stdio.h>
#include<process.h>
int front=0,rear=-1,q[5];
int main()
{
 void insert(int);int deletion();void display();
 int x,data,ch;
 printf("\n\n***MENU***\n\n1.Insertion\n\n2.Deletion\n\n3.Display\n\n4.Exit\n\n");
 do
 {
  printf("\n\nEnter your choice\n\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("\n\nEnter data to insert in Queue\n\n");
                scanf("%d",&data);
    insert(data);
    break;
    case 2:
     x=deletion();
     printf("\n\nDeleted data is %d\n\n",x);
    /*here if you remove break; statememt from program then after deletion
    it will move to case 3 and call dispaly function*/
     case 3:
      display();
      break;
      case 4:
       exit(1);
       default:
        printf("\n\nWrong Choice Try again\n\n");
       
  }
 }while(ch!=4);
 
}
void insert(int x)
{
 int p;
 if(front==0&&rear==4)
 {
 printf("\n\nDQueue is Overflow\n");
 }
 else
 {
  printf("\nPress 1/2 to insert at Rear_end/Front_end\n\n ");
  scanf("%d",&p);
  if(p==1)
  {
   if(rear<4)
   {
     rear++;
                q[rear]=x;
   }
   else
   printf("\n\nCan not insert at Rear_end\n\n");
     }
       else if(p==2)
      {
         if(front>0)
       {
     front--;
     q[front]=x;
      }
       else
       printf("\n\nCan not insert at Front_end\n\n");
      }
    }
}

int deletion()
{
 int x,p;
 if(rear==front-1)
 {
  printf("\n\nDQueue is Underflow\n\n");
 }
 else
 {
  printf("Press 1/2 to Delete at Rear_end/Front_end\n\n");
  scanf("%d",&p);
  if(p==1)
  {
  x=q[rear];
  rear--;
  return(x); 
  }
  /* here no need to write else part because if rear==front-1 is false then it means there is atlest one element 
  in the queue to delete*/
  else if(p==2)
  {
   x=q[front];
   front++;
   return(x);
  }
 }
}


void display()
{
 
 int i;
 
  if(rear==front-1)
  printf("\n\nDQueue is Underflow\n\n");
       printf("DQueue is  ");
 for(i=front;i<=rear;i++)
 {
  printf("%d  ",q[i]);
 }
}

Quick Sort Code

//Quick Sort

#include<stdio.h>
void quick_sort(int*,int,int);
int a[5],i;
main()
{
 
 printf("Enter Five values\n\n");
 for(i=0;i<5;i++)
 {
  scanf("%d",&a[i]);
 }
 quick_sort(a,0,4);
 printf("\nFinal Sorted values\n\n");
 for(i=0;i<5;i++)
 {
  printf("%d",a[i]);
 }
 return 0;
}
void quick_sort(int*p,int first,int last)
{
 int low,high,temp,pivot;
 if(first<last){
 low=first;high=last;pivot=first;
 while(low<high)
 {
      while(p[low]<=p[pivot])
        low++;
      while(p[high]>p[pivot])
         high--;
         if(low<high)
        {
      
        temp=p[low];
        p[low]=p[high];
        p[high]=temp;
    
        }
        
    }
         temp=p[pivot];
         p[pivot]=p[high];
         p[high]=temp;
        quick_sort(p,first,high-1);
        quick_sort(p,high+1,last);
         
    }
}

Queue(Data structure) code


//QUEUE CODE

#include<stdio.h>
#include<process.h>
int front=0,rear=-1;
int x,data,ch,q[5];
int main()
{
 void insert(int);int deletion();void display();
 do
 {
  printf("\n\n***MENU***\n\n1.Insertion\n\n2.Deletion\n\n3.Display\n\n4.Exit\n\nEnter your choice\n\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("\n\nEnter data to insert in Queue\n\n");
                scanf("%d",&data);
    insert(data);
    break;
    case 2:
     x=deletion();
     printf("\n\nDeleted data is %d\n\n",x);
    /*here if you remove break; statememt from program then after deletion
    it will move to case 3 and call dispaly function*/
     case 3:
      display();
      break;
      case 4:
       exit(1);
       default:
        printf("\n\nWrong Choice Try again\n\n");
       
  }
 }while(ch!=4);
 
}
void insert(int x)
{
 
 if(rear==4)
 {
 printf("\n\nQueue is Overflow\n");
 }
 else
 {
  rear++;
 q[rear]=x;

 }
}
int deletion()
{
 int x;
 if(rear==front-1)
 {
  printf("\n\nQueue is Underflow\n\n");
 }
 else
 {
  x=q[front];
  front++;
  return(x); 
 }
}
void display()
{
 
 int i;
 
  if(rear==front-1)
  printf("\n\nQueue is Underflow\n\n");
       printf("Queue is  ");
 for(i=front;i<=rear;i++)
 {
  printf("%d  ",q[i]);
 }
}