Skip to main content

Posts

Showing posts with the label C Program

How to find size of array in C/C++ program

#include<iostream> using namespace std; int main() { int a[]={1,2,3,4,5,6,7}; int size; size=sizeof(a)/sizeof(a[0]); cout<<"sizeof a"<<size<<"\n"; system("PAUSE"); return 0; } Output: sizeof a  7 Note : sizeof(a) returns size in bytes and sizeof(a)/sizeof(a[0]) returns number of elements in an array.

Swap two numbers using Pointers in C programming

#include<stdio.h> #include<conio.h> void swap(int*,int*); void main() {   int a,b;   clrscr();   printf("enter first number  :");   scanf("%d",&a);   printf("enter second number :");   scanf("%d",&b);   clrscr();   printf("Before swapping\n\t\ta=%d\n\n\t\tb=%d",a,b);   swap(&a,&b);   printf("\n\nAfter swapping\n\t\ta=%d\n\n\t\tb=%d",a,b);   getch(); }     void swap(int *x,int *y)   {     int z;     z=*y;     *y=*x;     *x=z;    }

Program to print pattern using * (asterik)

Q1 Write a C program to print pattern as below :-     *    ***  ***** ******* #include<stdio.h> #include<conio.h> void main() { clrscr(); for(int i=0;i<4;i++) { for(int j=3;j>i;j--) { printf(" "); //print a space } for(int k=0;k<2*i+1;k++) { printf("*"); } printf("\n"); } getch(); } Q2 Write a C program to print pattern as below :-       *     * *   * * * * *  *  * #include<stdio.h> #include<conio.h> void main() { clrscr(); for(int i=0;i<4;i++) { for(int j=3;j>i;j--) { printf(" "); //print a space } for(int k=0;k<=i;k++) { printf("*"); } printf("\n"); } getch(); }

Selection Sort using C program

#include<stdio.h> #include<conio.h> void main() {int i,j,a[5],temp;  clrscr();  printf("enter elements :\n");  for(i=0;i<5;i++)  {  scanf("%d",&a[i]);  }  for(i=0;i<=4;i++)  {for(j=i+1;j<=4;j++)   {if(a[i]>a[j])    {temp=a[i];     a[i]=a[j];     a[j]=temp;    }   }  }  printf("\n\n After sorting \n");  for(i=0;i<5;i++)  {printf(" %d",a[i]);  }  getch();  }

Merge two sorted Array

C program to merge two sorted array #include<stdio.h> #include<conio.h>  void main()  { int a[10],b[10],c[20],i,j=0,a_size,b_size;    int *p1,*p2,*p3,*p4;    clrscr();    printf("\nenter the size of A: ");    scanf("%d",&a_size);    printf("\nenter the size of B: ");    scanf("%d",&b_size);    p1=&a[0];    p2=&a[a_size-1];    p3=&b[0];    p4=&b[b_size-1];    printf("\n\ninsert elements of A:(in ascending order)\n");    for(i=0;i<a_size;i++)    { scanf("%d",&a[i]);    }    printf("\n\ninsert elements of B:(in ascending order)\n");    for(i=0;i<b_size;i++)    { scanf("%d",&b[i]);    }    if(a_size<=b_size)    {     while(p1<=p2)     { if(*p1<*p3)       { c[j]=*p1; j++; p1++;   ...

Macro - Pragma in C

C program to show usage of pragma macro #include<stdio.h> #include<conio.h>  void earth();  void moon();  #pragma startup earth  #pragma exit moon void main() {printf("\n\n We are in the space"); }  void earth()  {clrscr();   printf("\n\n We are on the earth");  }  void moon()  {printf("\n\n We are on the moon");  getch();  }

C program to create student information system of 10 students in the file

#include<stdio.h> #include<conio.h>  struct student  { char name[20];    int rollno;  }st[10];  void main()  {int var,i;   char ch='y';   FILE *pk=fopen("student.txt","w");   clrscr();   if(pk==NULL)   { printf("\n FILE DOES NOT EXIST ");     getch();     exit(0);   }   while(ch=='y')   { printf("\n Enter records:\n");     for(i=0;i<10;i++)     {       printf("\n Enter name: ");       scanf("%s",st[i].name);       printf("\n Enter roll no.: ");       scanf("%d",&st[i].rollno);       fprintf(pk," %s %d\n",st[i].name,st[i].rollno);     }     printf("\n\n Do you want to continue(yes-Y): ");     fflush(stdin);     scanf("%c",&ch);   }   fclose(pk);   clrscr();   pk=fopen("student.txt","r")...

C program to read data of structure in existing file using fscanf()

#include<stdio.h> #include<conio.h>  struct emp  {char name[20];   int id;   }e;  void main()  { int vb;    FILE *pk=fopen("keviv.txt","a+");    clrscr();     if(pk==NULL)     { printf("\n\n FILE DOES NOT EXIST");       getch();       exit(0);     }     while(1)     { vb=fscanf(pk,"%s%d",e.name,&e.id);       if(vb==EOF)        break;       printf("\n\n Name: %s\n ID: %d",e.name,e.id);     }     fclose(pk);     getch();     }

C program to read strings from existing file using fgets()

#include<stdio.h> #include<conio.h>  void main() { char s[50];   int len=0;   FILE * pk=fopen("keviv.txt","r");   clrscr();   if(pk==NULL)   { printf("\n FILE DOES NOT EXIST ");     getch();     exit(0);   }   while(1)   { len=fgets(s,49,pk);     if(len==NULL)       break;     puts(s);   }   getch();   fclose(pk);   }

C program to write strings in existing file using fputs()

#include<stdio.h> #include<string.h> #include<conio.h>  void main()  {    char s[50];    FILE *pk=fopen("keviv.txt","w++");    clrscr();    if(pk==NULL)    { printf("\n FILE DOES NOT EXIST ");      getch();      exit(0);    }    while(1)    { gets(s);      if(strlen(s)==NULL)        break;      fputs(s,pk);    }    fclose(pk);  }

C program to read & write structure data from existing file using fread() & fwrite() functions

/* program to read & write structure data from existing file    using fread() & fwrite() functions */ #include<stdio.h> #include<conio.h>  struct emp  {char name[20];   int id;   }e;  void main()  { char ch='y';    FILE *pk=fopen("keviv.txt","aw+");    clrscr();     if(pk==NULL)     { printf("\n\n FILE DOES NOT EXIST");       getch();       exit(0);     }    while(ch=='y')    { printf("\n\n Enter records of employee:\n");      printf("\n Enter name: ");      scanf("%s",e.name);      printf("\n Enter ID: ");      scanf("%d",&e.id);      fwrite(&e,sizeof(e),1,pk);      printf("\n\n Do u want to continue... ");      fflush(stdin);      scanf("%c",&ch);    }  ...

C program to write data of a structure in existing file using fprintf()

#include<stdio.h> #include<conio.h>  struct emp  {char name[20];   int id;   }e;  void main()  { char ch='y';    int i;    FILE *pk=fopen("keviv.txt","a+");    clrscr();     if(pk==NULL)     { printf("\n\n FILE DOES NOT EXIST");       getch();       exit(0);     }    while(ch=='y')    { printf("\n\n Enter records of employee:\n");      printf("\n Enter name: ");      scanf("%s",e.name);      printf("\n Enter ID: ");      scanf("%d",&e.id);      //i=ftell(pk);      //fseek(pk,i,0);      fprintf(pk," %s %d\n",e.name,e.id);      printf("\n\n Do u want to continue...(Y/N): ");      fflush(stdin);      scanf("%c",&ch);    }    fclose(pk);...

C program to write characters in existing file using fputc()

#include<stdio.h> #include<conio.h>  void main()  {char ch;   FILE *vk=fopen("vvv.txt","wb"); //to write"w" in the file   clrscr();   if(vk==NULL)            //if file doesnot exit(NULL), exit from program    {     printf("\n\n File does not exit \n");     exit(0);     }     printf("\n\n Enter 0 to exit ");    while(1)    {ch=getche();     if(ch=='0')      break;     fputc(ch,vk);     }    fclose(vk);     getch();     }

C program to read characters from existing file using fgetc() function

#include<stdio.h> #include<conio.h>  void main()  {char ch;   FILE *fp=fopen("c:\\tc\\data_file.txt","r");   clrscr();   if(fp==NULL)    {     printf("\n\n File does not exist \n");     getch();     exit(0);     }    while(1)    {ch=fgetc(fp);     if(ch==EOF)      break;     putc(ch,stdout);     }    fclose(fp);     getch();   }

Bubble Sorting using C program

#include<stdio.h> #include<conio.h> void main() {int i,j,a[5],temp;  clrscr();  printf("enter elements :\n");  for(i=0;i<5;i++)  {  scanf("%d",&a[i]);  }  for(i=0;i<=4;i++)  {for(j=0;j<=4-i-1;j++)   {if(a[j]>a[j+1])    {temp=a[j];     a[j]=a[j+1];     a[j+1]=temp;    }   }  }  printf("\n\n After sorting \n");  for(i=0;i<5;i++)  {printf(" %d",a[i]);  }  getch();  }

Dynamic Stack Implementation using Linked List

#include<stdio.h> #include<conio.h>  void push();  void pop();  void traverse();  struct node  { int num;    struct node *next;  }*top=NULL;  typedef struct node node;  typedef struct node* nptr;  void main()  { char choice;    while(1)    {    clrscr();    printf("-----DYNAMIC STACK-----\n\n");    printf(" 1.PUSH\n");    printf(" 2.POP\n");    printf(" 3.TRAVERSE\n");    printf(" 4.EXIT\n");    printf(" Enter your choice: ");    fflush(stdin);    scanf("%c",&choice);    switch(choice)    { case '1': push();       break;      case '2': pop();       break;      case '3': traverse();       break;      case '4': exit(0);      default: printf(...

UNIX Shell & History Feature

Operating System Project Implementation of  UNIX Shell & History Feature in C language Note: - Implement this code on Linux platform How to Compile?       cc -o exec_name source_name.c  Source Code: - /* press ctrl-c to show history & press ctrl-d to exit from command prompt */ #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <signal.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */ #define BUFFER_SIZE 50 #define buffer "\n\nCommand History:\n" char history[10][BUFFER_SIZE]; int count = 0; int caught = 0; void printHistory() { int i; int j = 0; int hcount = count; for (i = 0; i<10;i++) { printf("%d.  ", hcount); while (history[i][j] != '\n' && history[i][j] != '\0') { printf("%c", history[i][j]); j++; } printf("\n"); j =...