Friday 18 October 2013

stack program through array

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50
int top=-1;
int stack[MAX];
int push(x)
{    if(top==MAX)
{
     printf("stack is full\n");
     return 0;
     }
     stack[top+1]=x;
     top++;
     return 1;
}
int pop()
{   if(top==-1)
{
    printf("stack is empty\n");
    getch();  
    exit(0);
}
    int temp;
    temp=stack[top];
    top--;
    return temp;
}
int main()
{
    int x,y;
    while(x!=0)
    {
    printf("enter 1 to push 2 to pop 0 to exit");
    scanf("%d",&x);
    if(x==1)
    {
            printf("enter data\n");
            scanf("%d",&y);
            push(y);
}
if(x==2)
{
        y=pop();
        printf(" %d poped\n",y);
        }
        }
        getch();
        }

Friday 27 September 2013

C Program of Double Linked List

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

struct node
{
 struct node *prev;
 int info;
 struct node *next;
}*start;

main()
{
 int choice,n,m,po,i;
 start=NULL;
 while(1)
 {
  printf("1.Create List\n");
  printf("2.Add at begining\n");
  printf("3.Add after\n");
  printf("4.Delete\n");
  printf("5.Display\n");
  printf("6.Count\n");
  printf("7.Reverse\n");
  printf("8.exit\n");
  printf("Enter your choice : ");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:
   printf("How many nodes you want : ");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
    printf("Enter the element : ");
    scanf("%d",&m);
    create_list(m);
   }
   break;
   case 2:
   printf("Enter the element : ");
   scanf("%d",&m);
   addatbeg(m);
   break;
   case 3:
   printf("Enter the element : ");
   scanf("%d",&m);
   printf("Enter the position after which this element is inserted : ");
   scanf("%d",&po);
   addafter(m,po);
   break;
   case 4:
   printf("Enter the element for deletion : ");
   scanf("%d",&m);
   del(m);
   break;
   case 5:
   display();
   break;
   case 6:
   count();
   break;
   case 7:
   rev();
   break;
   case 8:
   exit();
   default:
   printf("Wrong choice\n");
 }/*End of switch*/
   }/*End of while*/
}/*End of main()*/

create_list(int num)
{
 struct node *q,*tmp;
 tmp= malloc(sizeof(struct node));
 tmp->info=num;
 tmp->next=NULL;
 if(start==NULL)
 {
  tmp->prev=NULL;
  start->prev=tmp;
  start=tmp;
 }
 else
 {
  q=start;
  while(q->next!=NULL)
   q=q->next;
  q->next=tmp;
  tmp->prev=q;
 }
}/*End of create_list()*/

addatbeg(int num)
{
 struct node *tmp;
 tmp=malloc(sizeof(struct node));
 tmp->prev=NULL;
 tmp->info=num;
 tmp->next=start;
 start->prev=tmp;
 start=tmp;
}/*End of addatbeg()*/

addafter(int num,int c)
{
 struct node *tmp,*q;
 int i;
 q=start;
 for(i=0;i<c-1;i++)
 {
  q=q->next;
  if(q==NULL)
  {
   printf("There are less than %d elements\n",c);
   return;
  }
 }
 tmp=malloc(sizeof(struct node) );
 tmp->info=num;
 q->next->prev=tmp;
 tmp->next=q->next;
 tmp->prev=q;
 q->next=tmp;
}/*End of addafter() */

del(int num)
{
 struct node *tmp,*q;
 if(start->info==num)
 {
  tmp=start;
  start=start->next;  /*first element deleted*/
  start->prev = NULL;
  free(tmp);
  return;
 }
 q=start;
 while(q->next->next!=NULL)
 {
  if(q->next->info==num)     /*Element deleted in between*/
  {
   tmp=q->next;
   q->next=tmp->next;
   tmp->next->prev=q;
   free(tmp);
   return;
  }
  q=q->next;
 }
 if(q->next->info==num)    /*last element deleted*/
 {  tmp=q->next;
  free(tmp);
  q->next=NULL;
  return;
 }
 printf("Element %d not found\n",num);
}/*End of del()*/

display()
{
 struct node *q;
 if(start==NULL)
 {
  printf("List is empty\n");
  return;
 }
 q=start;
 printf("List is :\n");
 while(q!=NULL)
 {
  printf("%d ", q->info);
  q=q->next;
 }
 printf("\n");
}/*End of display() */

count()
{  struct node *q=start;
 int cnt=0;
 while(q!=NULL)
 {
  q=q->next;
  cnt++;
 }
 printf("Number of elements are %d\n",cnt);
}/*End of count()*/

rev()
{
 struct node *p1,*p2;
 p1=start;
 p2=p1->next;
 p1->next=NULL;
 p1->prev=p2;
 while(p2!=NULL)
 {
  p2->prev=p2->next;
  p2->next=p1;
  p1=p2;
  p2=p2->prev; /*next of p2 changed to prev */
 }
 start=p1;
}/*End of rev()*/

Thursday 4 April 2013

Factorial of a number using resursion



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

int fact(int);
main()
{int y,n;
printf("Enter a number to find its factorial\n");
scanf("%d",&n);

y=fact(n);
printf("factorial = %d",y);
getch();
 }
 int fact(int x)
 {
  if(x>1)
  return x*fact(x-1);
  else
  return 1;

}

Program to print pyramid

program to print following pattern


#include<stdio.h>
#include<conio.h>
int abs(int);
main()
{
      int n,x=0,i,j,k=0,t=0;
      printf("Enter number of lines");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {k=(-1)*t;
                      for(j=n-i;j>0;j--)
                      {
                                         printf(" ");
                                         }
                      while(k<=t)
                      {
                                 printf("%d",abs(k));
                                 k++;
                                 }
                                 t++;
                                 printf("\n");
                               
                                 }
                      getch();
                      }
int abs(int x)
{
    if(x<0)
    return (-1)*x;
    else
    return x;
}


program to print following pattern

#include<stdio.h>
#include<conio.h>
main()
{
      int i,j,k,l,n,counter;
      printf("enter number of rows");
      scanf("%d",&n);
      l=n;
      for(j=1;j<=n;j++){
      for(i=1;i<l;i++)
      printf(" ");
      for(counter=0;counter<2*j;counter++)
      {
      if(counter%2==0)
      printf("*");
      else
      printf(" ");
      }
      l--;
      printf("\n");
      }
      getch();
      }
         

Thursday 28 March 2013

Palindrome

Palindrome number is a number that reads same from backward and forward. For example 101 is palindrome because when we reverse its digit we get 101. And 123 is not a palindrome because when we reverse its digits we get 321 which is not equal to 123.

To write a program to check whether it is palindrome,we have to reverse its digits and check whether the number obtained is equal to original number.

We can reverse the digits of a number by using modulus operator(%). It gives remainder of two numbers.
3%2 will give 1, 7%5 will give etc..

so we can reverse a number by following method(lets assume the number is 375

if n is the number n%10 will give its last digit
375%10=5
dividing the number by 10 will remove its last digit(if we declared the variable as integer type)
Therefore when we divide the number by 10 we are left with 37.Again using modulus operator and dividing we get 7 and number left is 3.This step is repeated again and we get all the digits. we can store these digits in an array.Note that we have got the digits in reverse order that is last digit first

Now that all digits are stored in array , the reverse number can generated by multiply the last item in array by 1, second last item by 10,third last by 100 and so on.This step can easily be done by using for loop

Below is a program to check whether a number(between 10000 to 999999) is palindrome.



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

int palindrome(int n)
{int i=0,z,sum=0,tota=10;
    int ar[6]={30,30,30,30,30,30};
    if(n>=100000)
    {z=n;
   
    while(n>0)
    {
     ar[i]=n%10;
     n=n/10;
     i++;
     }
     sum=ar[5]+10*ar[4]+100*ar[3]+1000*ar[2]+10000*ar[1]+100000*ar[0];
     if(sum==z)
     return 1;
     else return 0;
     }
     else
     {z=n;
   
    while(n>0)
    {
     ar[i]=n%10;
     n=n/10;
     i++;
     }
     sum=ar[4]+10*ar[3]+100*ar[2]+1000*ar[1]+10000*ar[0];
     if(sum==z)
     return 1;
     else return 0;
     }
     }
     main()
     {int n;
     printf("enter a number between 10000 to 999999 to check whether it is palindrome\n");
     scanf("%d",&n);
     if(palindrome(n)==1)
     printf("it is palindrome");
     else
     printf("it is not a palindrome");
         
           getch();
           }

Sunday 17 March 2013

sum of two numbers

#include<stdio.h>
void main()
{
int num1,num2,sum;
printf("Enter two numbers to calculate their sum");
scanf("%d%d",&num1,num2);
sum=num1+num2;
printf("Sum of  %d and %d = %d",num1,num2,sum);
}

hello world

hello world program

#include <stdio.h>
void main()
{
      printf("hello world");
}