C Programming Language

c source code examples

1.check prime numbers source code

#include
#include
void main()
{
int l,i,j,f,n;
clrscr();
do
 {
 printf("\n[1] to check prime number\n");
 printf("[2] to generate prime numbers upto a given number\n");
 printf("[3] to exit\nEnter yor choice\n");
 scanf("%d",&l);
 switch(l)
  {
  case 1:
     {
     printf("enter a number\n");
     scanf("%d",&n);
     f=1;
     for(i=2;i

2.Matrix operation


#include
#include
void input(int z);
void multiply(void);
void add(void);
void display(void);
void transpose(void);
int r1,r2,c1,c2,c,m1[5][5],m2[5][5],m3[5][5],i,j,k,d=0;
void main()
{
clrscr();
do
 {
 printf("enter [1] to add two matrices\n");
 printf("enter [2] to perform matrix multiplication\n");
 printf("enter [3] to find traspose of a matrix\n");
 printf("enter [4]  exit\n");
 printf("enter your choice\n");
 scanf("%d",&c);
 switch(c)
   {
   case 1:
      input(c);
      if(d!=1)
       {
       add();
       display();
       }
   break;
   case 2:
      input(c);
      if(d!=1)
	{
	multiply();
	display();
	}
   break;
   case 3:
      input(c);
      if(d!=1)
	{
	transpose();
	display();
	}
   break;
   case 4:
   break;
   default:
      printf("invalid input\n");
   break;
   }
}
while(c!=4);
getch();
}

void input(int z)
   {
   d=0;
   printf("enter r1 and c1\n");
   scanf("%d%d",&r1,&c1);
   if(z!=3)
     {
      printf("enter r2 and c2\n");
      scanf("%d%d",&r2,&c2);
      }
   if((z==1)&&(r1==r2)&&(c1==c2)||(z==2)&&(c1==r2))
     {
     printf("enter elements of matrix 1\n");
     for(i=0;i


3.stack using linked list
#include
#include
#define null 0
struct linkedlist
   {
   int info;
   struct linkedlist *next;
   };
typedef struct linkedlist node;
void create(node *list);
node *deletee(node *list);
void display(node *list);
node *insert(node *list);
int i=1;

void main()
   {
   int x;
   node *head;
   clrscr();
   head=(node *)malloc(sizeof(node));
   create(head);
   display(head);
   printf("\nhow many numbers you want to push to stack\n");
   scanf("%d",&x);
   for(i=1;i<=x;i++)
      head=insert(head);
   printf("after insertion\n");
   display(head);
   printf("\nhow many numbers you want to pop from stack\n");
   scanf("%d",&x);
   for(i=1;i<=x;i++)
     head=deletee(head);
   printf("after deletion\n");
   display(head);
   getch();
   }


void create(node *list)
      {
      printf("enter %d-th number(0 to exit) \n",i);
      scanf("%d",&(list->info));
      i++;
      if(list->info==0)
	  list->next=null;
      else
	  {
	   list->next=(node *)malloc(sizeof(node));
	   create(list->next);
	  }
   }


 void display(node *list)
   {
   while(list->next!=null)
      {
      printf("%d->",list->info);
      list=list->next;
      }
   }

node *insert(node *list)
   {
   int x;
   node *new,*i;
   printf("value of new item\n");
   scanf("%d",&x);
   new=(node *)malloc(sizeof(node));
   new->info=x;
   for(i=list;i->next->next!=null;i=i->next)
     ;
   new->next=i->next;
   i->next=new;
   return(list);
   }

node *deletee(node *list)
   {
   int x;
   node *i;
   for(i=list;i->next->next!=null;i=i->next)
     ;
   i->next=null;
   return(list);
   }

4. Linked list

#include
#include
#define null 0
struct linkedlist
   {
   int info;
   struct linkedlist *next;
   };
typedef struct linkedlist node;
void create(node *list);
void display(node *list);
void sort(node *list);
/*node* find(node *list,int);*/
int i=1;

void main()
   {
   node *head1,*head2,*head3;
   clrscr();
   head1=(node *)malloc(sizeof(node));
   printf("Linked List A:\n");
   create(head1);
   printf("elements of linked list A is:\n");
   display(head1);
   head2=(node *)malloc(sizeof(node));
   printf("\n\nLinked List B:\n");
   create(head2);
   printf("elements of linked list B is:\n");
   sort(head1);
   sort(head2);
   display(head2);
   getch();
   }


void create(node *list)
      {
      printf("enter %d-th number(0 to exit) \n",i);
      scanf("%d",&(list->info));
      i++;
      if(list->info==0)
	  list->next=null;
      else
	  {
	   list->next=(node *)malloc(sizeof(node));
	   create(list->next);
	  }
      i=0;
   }

 void display(node *list)
   {
      if(list->next==null)
	 printf("list is empty\n");
      else
	 {
	 while(list->next!=null)
	     {
	     printf("%d-->",list->info);
	     list=list->next;
	     }
	}
    }


	    
	    
c coding

goto statement Home While loop

c program stats