C PROGRAMS-1

 1.   Program which separates odd and even integers in separate arrays

       #include<stdio.h>

void main()

 {

   int a[10],e[10],o[10];

   int i,even=0,odd=0;

   clrscr();

   printf("Initialize the array\n");

   for(i=0;i<10;i++)

    {

      printf("Enter an integer ");

      scanf("%d",&a[i]);

      if(a[i]%2==0)

       e[even++]=a[i];

      else

       o[odd++]=a[i];

    }

   printf("\nOdd integers in the array\n");

   for(i=0;i<odd;i++)

    printf("%d ",o[i]);

   printf("\nEven integers in the array\n");

   for(i=0;i<even;i++)

    printf("%d ",e[i]);

   getch();

 }


2. Program which sort elements of array in ascending order

      #include<stdio.h>

void main()

 {

   int i,j,t,arr[8];

   clrscr();

   printf("Sorting array elements in ascending order\n\n");

   printf("Initialize the array\n\n");

   for(i=0;i<8;i++)

    {

      printf("Enter an integer ");

      scanf("%d",&arr[i]);

    }

   printf("\nArray before Sorting\n\n");

   for(i=0;i<8;i++)

    printf("%d ",arr[i]);

   for(i=0;i<8-1;i++)

    for(j=0;j<8-1-i;j++)

     {

       if(arr[j]>arr[j+1])

{

t=arr[j];

    arr[j]=arr[j+1];

  arr[j+1]=t;

}

      }

   printf("\n\nArray after Sorting\n\n");

   for(i=0;i<8;i++)

    printf("%d ",arr[i]);

   getch();

 }


3. Program which multiplies two square matrices

     #include<stdio.h>

void main()

 {

   int i,j,k,a[2][2],b[2][2],c[2][2]={0};

   clrscr();

   printf("Matrix multiplication\n");

   printf("\nInitialize first matrix\n\n");

   for(i=0;i<2;i++)

    for(j=0;j<2;j++)

     {

       printf("Enter an integer ");

       scanf("%d",&a[i][j]);

     }

   printf("\nInitialize second matrix\n\n");

   for(i=0;i<2;i++)

    for(j=0;j<2;j++)

     {

       printf("Enter an integer ");

       scanf("%d",&b[i][j]);

     }

   for(i=0;i<2;i++)

    for(j=0;j<2;j++)

     for(k=0;k<2;k++)

      c[i][j]=c[i][j]+a[i][k]*b[k][j];

   printf("\nFirst matrix\n\n");

   for(i=0;i<2;i++)

    {

     for(j=0;j<2;j++)

      printf("%d ",a[i][j]);

     printf("\n\n");

    }

   printf("\nSecond matrix\n\n");

   for(i=0;i<2;i++)

    {

     for(j=0;j<2;j++)

      printf("%d ",b[i][j]);

     printf("\n\n");

    }

   printf("\nProduct matrix\n\n");

   for(i=0;i<2;i++)

    {

     for(j=0;j<2;j++)

      printf("%d ",c[i][j]);

     printf("\n\n");

    }

   getch();

 }


4.  Program which displays transpose of a matrix

     #include<stdio.h>

void main()

 {

   int a[3][2],t[2][3],i,j;

   clrscr();

   printf("Initialize the matrix\n");

   for(i=0;i<3;i++)

    for(j=0;j<2;j++)

     {

       printf("Enter an integer ");

       scanf("%d",&a[i][j]);

       t[j][i]=a[i][j];

     }

   printf("\nMatrix is\n\n");

   for(i=0;i<3;i++)

    {

      for(j=0;j<2;j++)

       printf("%d ",a[i][j]);

      printf("\n\n");

    }

   printf("\nTranspose Matrix is\n\n");

   for(i=0;i<2;i++)

    {

      for(j=0;j<3;j++)

       printf("%d ",t[i][j]);

      printf("\n\n");

    }

   getch();

 }


5.   Program which searchs an element in a row & column wise

       #include <stdio.h>

int search(int mat[4][4],int n,int x)

 {

   int smallest=mat[0][0],largest=mat[n - 1][n - 1];

   int i=0,j=n - 1;

   if(n==0)

     return -1;

   if(x<smallest || x>largest)

     return -1;

   while(i<n && j>=0)

    {

      if(x==mat[i][j])

       {

printf("\n Found at %d, %d", i+1, j+1);

return 1;

       }

      if(x<mat[i][j])

j--;

      else

i++;

    }

   printf("\n Element not found");

   return 0;

 }

void main()

 {

   int mat[4][4] = {

{ 10, 20, 30, 40 },

{ 15, 25, 35, 45 },

{ 27, 29, 37, 48 },

{ 32, 33, 39, 50 },

    };

   clrscr();

   search(mat, 4, 29);

   getch();

}


6. Program which prints individual characters of string in reverse order

#include<stdio.h>

void main()

 {

   char ch,str[25];

   int i=0;

   clrscr();

   printf("Enter a string ");

   while( (ch=getchar())!='\n' )

    {

      str[i]=ch;

      i++;

    }

   i--;

   printf("\nString in reverse order is\n\n");

   for(;i>=0;i--)

    putchar(str[i]);

   getch();

 }


7. Program which compare two strings without using string library functions

#include<stdio.h>

void maissn()

 {

   char ch,str1[25],str2[25];

   int i=0,len1,len2;

   clrscr();

   printf("Enter first string ");

   gets(str1);

   for(i=0;str1[i]!='\0';i++);

   len1=i;

   printf("Enter second string ");

   gets(str2);

   for(i=0;str2[i]!='\0';i++);

   len2=i;

   if(len1==len2)

    {

      for(i=0;i<len1;i++)

       {

if(str1[i]!=str2[i])

  break;

       }

      if(i==len1)

       printf("Strings are equal");

      else

       printf("Strings are not equal");

    }

   else

    printf("Strings are not equal");

   getch();

 }


8. Program which copies one string to another string

     #include<stdio.h>

void main()

 {

   char str1[25],str2[25];

   int i;

   clrscr();

   printf("Enter a string ");

   gets(str1);

   for(i=0;str1[i]!='\0';i++)

    str2[i]=str1[i];

   str2[i]='\0';

   printf("\After string copying contents of str2 ");

   puts(str2);

   getch();

 }


9. Program to store info using structures with dynamically memory allocation

     #include<stdio.h>

#include<stdlib.h>

typedef struct

 {

   char id[11];

   char name[11];

   int maths,physics,chemistry;

 }STUDENT;

void main()

 {

   STUDENT* stud;

   int i,n;

   clrscr();

   printf("How many students info? ");

   scanf("%d",&n);

   stud=calloc(n,sizeof(STUDENT));

   for(i=0;i<n;i++)

    {

      fflush(stdin);

      printf("\nEnter student %d information\n",i+1);

      printf("\nStudent Id ");

      gets(stud[i].id);

      printf("\nStudent Name ");

      gets(stud[i].name);

      printf("\nMarks in Maths ");

      scanf("%d",&stud[i].maths);

      printf("\nMarks in Physics ");

      scanf("%d",&stud[i].physics);

      printf("\nMarks in Chemistry ");

      scanf("%d",&stud[i].chemistry);

    }

   printf("\n\nStudents Details\n\n");

   printf("\nId         Name\t\tMaths\tPhysics\tChemistry\n\n");

   for(i=0;i<n;i++,stud++)

    {

       printf("%s %-10s\t%d\t%d\t%d\n\n",stud->id,stud->name,stud->maths,stud->physics,stud->chemistry);

    }

   free(stud);

   getch();

 }


10. Program which demonstrates pointers handling

      #include<stdio.h>

void main()

 {

   int i,arr[5]={3,-4,5,7,-8};

   int* ptr;

   clrscr();

   ptr=arr;

   printf("Accessing elements using pointer name as array name\n\n");

   for(i=0;i<5;i++)

     printf("ptr[%d] = %d\n",i,ptr[i]);

   printf("\n\nAccessing elements using indirection operator with pointer\n\n");

   for(i=0;i<5;i++)

     printf("*(ptr+%d) = %d\n",i,*(ptr+i));

   getch();

 }

11. Program which demonstrate the use of & (address of)  and * (value at address) operators

#include<stdio.h>

void main()

 {

   int a=10;

   int* p;

   clrscr();

   p=&a;

   printf("\nAddress of a i.e: &a = %u\n",&a);

   printf("\nAddress of p i.e: &p = %u\n",&p);

   printf("\nAddress stored in p i.e: p = %u\n",p);

   printf("\nValue of a i.e: a = %d\n",a);

   printf("\nValue at address p i.e: *p = %d\n",*p);

   getch();

 }


12. Program which adds two numbers using pointers

#include<stdio.h>

void main()

 {

   int a,b,sum;

   int *pa,*pb;

   clrscr();

   printf("Enter two integers ");

   scanf("%d %d",&a,&b);

   pa=&a;

   pb=&b;

   sum=*pa+*pb;

   printf("Sum is %d",sum);

   getch();

 }


13. Program which adds numbers using call by reference

#include<stdio.h>

int add(int*,int*); //function declaration

void main()

 {

   int a,b,sum;

   clrscr();

   printf("Enter two integers ");

   scanf("%d %d",&a,&b);

   sum=add(&a,&b); //function call by reference

   printf("Sum is %d",sum);

   getch();

 }

int add(int* p,int* q)  //function definition

 {

   return(*p+*q);

 }


14. Program which finds largest element using Dynamic Memory Allocation

#include<stdio.h>

#include<stdlib.h>

void main()

 {

   int *arr;

   int n,i,large;

   clrscr();

   printf("Enter the size of the list ");

   scanf("%d",&n);

   arr=malloc(n*sizeof(int));

   printf("\nInitialize the list\n\n");

   for(i=0;i<n;i++)

    {

      printf("Enter an integer ");

      scanf("%d",&arr[i]);

    }

   large=arr[0];

   for(i=1;i<n;i++)

    if(arr[i]>large)

      large=arr[i];

   printf("\nLargest among the list is %d",large);

   free(arr);

   getch();

 }









     

No comments:

Post a Comment