Collection of C programs

sumitmehta

New Member
Messages
215
Reaction score
1
Points
0
Hye Guys

I am starting this thread to share some of the useful C Programs developed in C. I developed these programs during my exam practice. Most of the programs are runnable. Only a few programs are there which I might have got from somewhere else. These programs are quite important for people who are beginners and at intermediate level in C. I hope these programs are useful for you. Pls don't forget to rep me if you like my work. Of curse if someone wishes to give me credits, he is most welcome but that's not a condition or compulsion. It's on your wish.

Starting now with the first program. Infact they are two. Adding matrices using pointers as well as arrays.

First through arrays:
Code:
int matrix1[3][3];
int matrix2[3][3];
int matrix3[3][3];

int row_size=3,col_size=3;

void getElements(int m);
void addMatrices();
void display(int);

void getElements(int m) {

    int i,j;
    for(i=0;i<row_size;i++) {

        for(j=0;j<col_size;j++) {

            printf("\nEnter element for Row %d & Column %d: ",(i+1),(j+1));

            if(m==1)
                scanf("%d",&matrix1[i][j]);
            else
                scanf("%d",&matrix2[i][j]);
        }
    }
}
void addMatrices() {

    int i,j;

    for(i=0;i<row_size;i++) {

        for(j=0;j<col_size;j++) {

            matrix3[i][j]=matrix1[i][j] + matrix2[i][j];
        }
    }

}
void display(int m) {

    int i,j;
    printf("\n");

    for(i=0;i<row_size;i++) {

        for(j=0;j<col_size;j++) {

            if(m==1) {
                printf("%d\t",matrix1[i][j]);
            }
            else if(m==2)  {
                printf("%d\t",matrix2[i][j]);
            }
            else if(m==3)  {
                printf("%d\t",matrix3[i][j]);
            }
            if(j==(col_size -1))
                printf("\n");

        }
    }
}
void main() {

    clrscr();
    printf("Details of Matrix 1:\n");
    getElements(1);
    printf("Details of Matrix 2:\n");
    getElements(2);
    addMatrices();

    clrscr();
    printf("\nMatrix 1:");
    display(1);
    printf("\nMatrix 2:");
    display(2);
    printf("\nResultant Matrix:");
    display(3);

    getch();

}

Now with pointers:

#include<stdio.h>

struct node {

    int row;
    int col;
    int data;
    struct node *next;
};
typedef struct node* NODE;

void add(NODE*,int,int,int);
NODE getMatrix(int,int);
NODE addMatrices(NODE*,NODE*);
void display(NODE*,int);

void add(NODE *n,int r,int c,int v) {

    NODE temp=*n,t=*n;
    NODE newN=(struct node *)malloc(sizeof(struct node));
    newN->data=v;
    newN->row=r;
    newN->col=c;
    newN->next=NULL;

    if(temp==NULL)
        temp=newN;
    else {

        while(t->next!=NULL)
            t=t->next;
        t->next=newN;
    }
    *n=temp;
}
NODE getMatrix(int r,int c) {

    int i,j;
    int data;
    NODE m=NULL;

    for(i=0;i<r;i++) {

        for(j=0;j<c;j++) {

            printf("\nEnter element for row %d & column %d : ",i+1,j+1);
            scanf("%d",&data);
            add(&m,i+1,j+1,data);
        }
    }
    return m;
}
NODE addMatrices(NODE *m1,NODE *m2) {

    NODE temp1=*m1;
    NODE temp2=*m2;
    NODE r=NULL;
    int data;

    while(temp1!=NULL)  {

        data=temp1->data + temp2->data;
        add(&r,temp1->row,temp1->col,data);
        temp1=temp1->next;
        temp2=temp2->next;
    }
    return r;
}
void display(NODE *matrix,int c) {

    NODE m=*matrix;

    printf("\n");

    while(m!=NULL) {

        printf("%d\t",m->data);

        if(m->col==c)
            printf("\n");
        m=m->next;
    }
}
void main() {

    int r,c;
    NODE m1=NULL,m2=NULL,m3=NULL;

    clrscr();

    printf("Enter number of total rows for both matrices: ");
    scanf("%d",&r);

    printf("Enter number of total columns for noth matrices: ");
    scanf("%d",&c);

    printf("\nDetails for Matrix 1: ");
    m1=getMatrix(r,c);

    printf("\nDetails for Matrix 2: ");
    m2=getMatrix(r,c);

    m3=addMatrices(&m1,&m2);

    printf("Matrix 1: ");
    display(&m1,c);

    printf("Matrix 2: ");
    display(&m2,c);

    printf("Resultant Matrix: ");
    display(&m3,c);

    getch();
}
Edit:
No reply..............:dunno:.........I am surprised to see that....Should I post more.......
Edit:
So let me post some more. You might then reply.

A simple address book:

Code:
#include<stdio.h>

struct addressBook {
    char name[15];
    int age;
    char email[25];
    char tel[15];
};
void collectInfo(struct addressBook *ad);
void addRecord();
void readRecord();
void displayInfo(struct addressBook);
long filesize(FILE *stream);

FILE *ptr;

void collectInfo(struct addressBook *ad) {
    printf("\nEnter Name: ");
    scanf("%s",ad->name);
    printf("\nEnter Age: ");
    scanf("%d",ad->age);
    printf("\nEnter E-Mail Address: ");
    scanf("%s",ad->email);
    printf("\nEnter Telephone: ");
    scanf("%s",ad->tel);
}
void addRecord() {
    struct addressBook adB[1];

    ptr=fopen("D:/add.txt","a+");
    if(ptr==NULL)  {
        printf("\nUnable to open file for writing.");
        getch();
        exit(0);
    }
    else {
         collectInfo(adB);
         fwrite(adB,sizeof(struct addressBook),1,ptr);
         fclose(ptr);
         printf("\nEntery added to address Book.");
    }

}
void readRecord() {
    struct addressBook adB[1];
    long fsize;int entries=0,e,pos;

    ptr=fopen("D:/add.txt","r");

    if(ptr==NULL)  {
        printf("Unable to open file for writing.");
        getch();
        exit(0);
    }
    else {

        fsize=filesize(ptr);
        entries=fsize/sizeof(struct addressBook);
        printf("\nNumber of Entries in addressBook : %d",entries);
        printf("\nEnter Record Number to display : ");
        scanf("%d",&e);
        pos=(e-1) * sizeof(struct addressBook);
        fseek(ptr,pos,SEEK_SET);
        fread(adB,sizeof(struct addressBook),1,ptr);
        fclose(ptr);
        displayInfo(adB[0]);
    }

}
void displayInfo(struct addressBook ad) {

    printf("Record Details : \n================");
    printf("\nName : %s",ad.name);
    printf("\nAge : %d",ad.age);
    printf("\nE-Mail : %s",ad.email);
    printf("\nTelephone : %s",ad.tel);
}
long filesize(FILE *stream)
{
   long curpos, length;

   curpos = ftell(stream);
   fseek(stream, 0L, SEEK_END);
   length = ftell(stream);
   fseek(stream, curpos, SEEK_SET);
   return length;
}
void main() {
   char s;
   clrscr();
   printf("What do you want to do?");
   printf("\n\t(Press 'a' for adding record and 'r' for reading record.): ");
   scanf("%c",&s);
   if(s=='a')
    addRecord();
   else
    readRecord();
   getch();
}

A very simple program: Calculating factorial of a number recursively.

Code:
#include<stdio.h>

int factorial(int);
void main() {
    int n;
    clrscr();
    printf("Hanji Kiska factorial calculate karun : ");
    scanf("%d",&n);
    printf("Factroial hai : %d",factorial(n));
    printf("\nChalo Jee ab mujh computer par taras karo...Main factorial calculate karke thak gaya...Ab koi aur interesting kaam karva lo par factorial nahin.......:)");
    getch();
}

int factorial(int n) {
    int number=n;
    if(number!=1) {
        number*=factorial(number - 1);
    }
    return number;
}
 
Last edited:
Top