Sunday, September 12, 2010

C Program to Check whether a number is an Armstrong Number or not

#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
void main() 

int n,sum=0,rem=0,cube=0,n1;
clrscr();
printf("\n\tEnter a number :" );
scanf("%d",&n);
n1=n;
while(n!=0)
{
rem=n%10;
cube=pow(rem,3);
sum=sum+cube;
n=n/10;
}
if(sum==n1)
printf("\n\tIt is an Armstrong's Number ");
else
printf("\n\tIt is not an Armstrong's Number ");
getch(); 

C Program to check whether a number is a Perfect Number or not

#include<stdio.h>
int main(){
  int n,i=1,sum=0;
  printf("\nEnter a number:-");
  scanf("%d",&n);
  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
printf("\nThe nnumber %d is a perfect number",i);
  else
      printf("\nThe number  %d is not a perfect number",i);
  return 0;
}

C Program to print Krishnamurthy Number

#include<stdio.h>
#include<conio.h>
void main()
{
int temp,n,fact,num,sum=0,i;
clrscr();
printf("Enter the Number: ");
scanf("%d",&num);
temp=num;
sum=0;
while(temp>0)
{
n=temp%10;
fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
sum=sum+fact;
temp=temp/10;
}
if(sum==num)
printf("\nKrishnamurthy's Number :");
else
      printf("\n Not a Krishnanurthy's Number ");
getch();
}















C Program to print Towers Of Hanoi

#include<stdio.h>
#include<conio.h>
long step;
void Hanoi(int n,char from,char to,char spare)
{
   if(n>1)
      Hanoi(n-1,from,spare,to);
      printf(" Step %ld : move #%d %c-->%c\n",++step,n,from,to);

   if(n>1)
      Hanoi(n-1,spare,to,from);
}
void main()
{
   int n;
   printf(" Enter the number of blocks in the tower ");
   scanf("%d",&n);
   step=0;
   Hanoi(n,'A','B','C');
   getch();
}

Saturday, September 4, 2010

C Program to count the number of nodes in the singly linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
      int data;
      struct node *link;
};
void append(struct node**,int);
void display(struct node*);
int count(struct node *);
void main()
{
     struct node *head;


     clrscr();
     head=NULL;
     append(&head,5);
     append(&head,6);
     append(&head,7);
     append(&head,8);
     display(head);

     printf("\n No of nodes in the list is %d",count(head));

     getch();
}
void append(struct node **q,int num)
{
     struct node *temp,*r;
     temp=*q;

     if(temp==NULL)  /* If list is empty , create first node */
     {
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            temp->link=NULL;
            *q=temp;
     }
     else
     {
          temp=*q;
          while(temp->link !=NULL)
          temp=temp->link;

          r=(struct node*)malloc(sizeof(struct node));
          r->data=num;
          r->link=NULL;
          temp->link=r;
     }
}
void display(struct node *start)
{
     printf("\n");
     printf(" The Linked List is :\n");
     /* Traverse the entire linked list */
     while(start != NULL)
     {
          printf("   %d",start->data);
          start=start->link;
     }

}

int count(struct node *q)
{
     int c=0;
     /* traverse the entire linked list */
     while(q!=NULL)
     {
        q=q->link;
        c++;
     }
     return c;
}

C Program to delete an element from a singly linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
      int data;
      struct node *link;
};
void append(struct node**,int);
void display(struct node*);
void del(struct node **,int);
void main()
{
     struct node *head;

     int n;
     clrscr();
     head=NULL;
     append(&head,5);
     append(&head,6);
     append(&head,7);
     append(&head,8);
     display(head);

     printf(" \n Enter the number you want to delete ");

     scanf("%d",&n);

     del(&head,n);
     display(head);
     getch();
}
void append(struct node **q,int num)
{
     struct node *temp,*r;
     temp=*q;

     if(temp==NULL)  /* If list is empty , create first node */
     {
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            temp->link=NULL;
            *q=temp;
     }
     else
     {
          temp=*q;
          while(temp->link !=NULL)
          temp=temp->link;

          r=(struct node*)malloc(sizeof(struct node));
          r->data=num;
          r->link=NULL;
          temp->link=r;
     }
}
void display(struct node *start)
{
     printf("\n");
     printf(" The Linked List is :\n");
     /* Traverse the entire linked list */
     while(start != NULL)
     {
          printf("   %d",start->data);
          start=start->link;
     }

}

void del(struct node **q,int no)
{
     struct node *old,*temp;
     temp=*q;

     while(temp != NULL)
     {
         if(temp->data==no)
        { /* If number to be selected is the first node in the list */
         if(temp==*q)
            *q=temp->link;
         /* Deletes the intermediate nodes from the linked list */
         old->link=temp->link;
         /* Free the memory occupied by the node */
         free(temp);
         return;
      }
        /* Traverse the linked list till the last node is reached */
        else
        {
          old=temp;       /* old points to previous node */

          temp=temp->link;  /* go to the next node */

        }
        }
      printf(" \n Element %d not found ",no);
}

C Program to insert a node at the beginning of a singly linked list

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
      int data;
      struct node *link;
};
void append(struct node**,int);
void display(struct node*);
void add_at_beg(struct node**,int );
void main()
{
     struct node *head;

     int n;
     clrscr();
     head=NULL;
     append(&head,5);
     append(&head,6);
     append(&head,7);
     append(&head,8);
     display(head);

     printf(" \n Enter the number you want to insert ");

     scanf("%d",&n);

     add_at_beg(&head,n);
     display(head);
     getch();
}
void append(struct node **q,int num)
{
     struct node *temp,*r;
     temp=*q;

     if(temp==NULL)  /* If list is empty , create first node */
     {
            temp=(struct node*)malloc(sizeof(struct node));
            temp->data=num;
            temp->link=NULL;
            *q=temp;
     }
     else
     {
          temp=*q;
          while(temp->link !=NULL)
          temp=temp->link;

          r=(struct node*)malloc(sizeof(struct node));
          r->data=num;
          r->link=NULL;
          temp->link=r;
     }
}
void display(struct node *start)
{
     printf("\n");
     printf(" The Linked List is :\n");
     /* Traverse the entire linked list */
     while(start != NULL)
     {
          printf("   %d",start->data);
          start=start->link;
     }

}

void add_at_beg(struct node**q,int no)
{
  struct node *temp;
  temp=(struct node*)malloc(sizeof(struct node));
  temp->data=no;
  temp->link=*q;
  *q=temp;
}