C Language Program for Game Application


MAIN.C
  ======


  Main function for maze solver
*/


#include <stdio.h>
#include <stdlib.h>

#include "maze.h"
#include "solve.h"


int main(int argc, char *argv[]) {
    struct maze maze;

    if ( argc < 2 ) {
        puts("You must specify the filename of your maze");
        return EXIT_FAILURE;
    }
    else if ( argc > 2 ) {
        puts("Too many command line arguments");
        return EXIT_FAILURE;
    }

    GetMazeFromFile(argv[1], &maze);

    if ( solve(&maze) == MAZE_FOUNDEXIT )
        puts("Found exit!");
    else
        puts("Can't reach exit!");

    PrintMaze(&maze);
    FreeMaze(&maze);

    return EXIT_SUCCESS;
}




--------------------------------------------------------------------------------

maze.h
/*
  MAZE.H
  ======
 
  Interface to maze functions
*/


#ifndef PG_MAZE_H
#define PG_MAZE_H


/*  Structure definitions  */

struct maze {
    char ** map;
    int startx, starty;
    int numrows;
    int initdir;
};

struct pos {
    int x, y, dir;
};


/*  Macros  */

#define BUFFERSIZE (1000)

#define MAZE_ENTRANCE 'I'
#define MAZE_EXIT     'O'
#define MAZE_WALL     'X'
#define MAZE_PATH     ' '
#define MAZE_TRAIL    '@'

#define MOVE_LEFT  (0)
#define MOVE_UP    (1)
#define MOVE_RIGHT (2)
#define MOVE_DOWN  (3)

#define MAZE_NOWAY      (0)
#define MAZE_FOUNDEXIT  (1)
#define MAZE_NOEXIT    (-1)


/*  Function declarations  */

void GetMazeFromFile(char * filename, struct maze * maze);
void FreeMaze(struct maze * maze);
void PrintMaze(struct maze * maze);


#endif  /*  PG_MAZE_H  */


--------------------------------------------------------------------------------

maze.c
/*
  MAZE.C
  ======
 

  Implementation of maze functions
*/


#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "maze.h"


/*  Creates a maze from a file  */

void GetMazeFromFile(char * filename, struct maze * maze) {
    char buffer[BUFFERSIZE];
    FILE * fp;
    char ** map;
    int n = 0, foundentrance = 0, foundexit = 0;


    /*  Open file  */

    if ( !(fp = fopen(filename, "r")) ) {
        sprintf(buffer, "Couldn't open file %s", filename);
        perror(buffer);
        exit(EXIT_FAILURE);
    }


    /*  Determine number of rows in maze  */

    while ( fgets(buffer, BUFFERSIZE, fp) )
        ++n;


    /*  Allocate correct number of rows  */

    if ( !(map = malloc(n * sizeof *map)) ) {
        fputs("Couldn't allocate memory for map\n", stderr);
        exit(EXIT_FAILURE);
    }


    /*  Reset file  */

    rewind(fp);
    n = 0;


    /*  Store each row  */

    while ( fgets(buffer, BUFFERSIZE, fp) ) {
        int i;

        if ( !(map[n] = malloc((strlen(buffer)+1) * sizeof map[n])) ) {
            fputs("Couldn't allocate memory for map\n", stderr);

            for ( i = 0; i < n; ++i )
                free(map[i]);

            free(map);

            exit(EXIT_FAILURE);
        }

        strcpy(map[n], buffer);
       

        /*  Remove trailing whitespace  */

        for ( i = strlen(map[n]) - 1; isspace(map[n][i]); --i )
            map[n][i] = 0;


        /*  Check for entrance and store position if found  */

        if ( !foundentrance ) {
            i = 0;
            while ( map[n][i] != 'I' && map[n][i++] );
            if ( map[n][i] == MAZE_ENTRANCE ) {
                maze->startx = i;
                maze->starty = n;
                foundentrance = 1;
            }
        }

        /*  Check for exit  */

        if ( !foundexit ) {
            if ( strchr(map[n], MAZE_EXIT) )
                foundexit = 1;
        }
        ++n;
    }


    /*  Fill in maze structure  */

    maze->map = map;
    maze->numrows = n;


    /*  Exit if there is no entrance or no exit */

    if ( !foundentrance ) {
        fputs("Maze has no entrance!\n", stderr);
        FreeMaze(maze);
        exit(EXIT_FAILURE);
    }

    if ( !foundexit ) {
        fputs("Maze has no exit!\n", stderr);
        FreeMaze(maze);
        exit(EXIT_FAILURE);
    }


    /*  Check for initial direction into the maze  */

    if ( maze->startx < strlen(maze->map[maze->starty]) - 1 &&
         maze->map[maze->starty][maze->startx+1] == MAZE_PATH )
        maze->initdir = MOVE_RIGHT;
    else if ( maze->startx > 0 &&
              maze->map[maze->starty][maze->startx-1] == MAZE_PATH )
        maze->initdir = MOVE_LEFT;
    else if ( maze->starty > 0 &&
         maze->map[maze->starty-1][maze->startx] == MAZE_PATH )
        maze->initdir = MOVE_UP;
    else if ( maze->starty < (maze->numrows-1) &&
              maze->map[maze->starty+1][maze->startx] == MAZE_PATH )
        maze->initdir = MOVE_DOWN;


    /*  Close file and return  */

    if ( fclose(fp) ) {
        perror("Couldn't close file");
        FreeMaze(maze);
        exit(EXIT_FAILURE);
    }
}


/*  Frees memory used by a maze  */

void FreeMaze(struct maze * maze) {
    int n;

    for ( n = 0; n < maze->numrows; ++n )
        free(maze->map[n]);

    free(maze->map);
}


/*  Outputs a maze  */

void PrintMaze(struct maze * maze) {
    int n;

    for ( n = 0; n < maze->numrows; ++n )
        puts(maze->map[n]);
}


--------------------------------------------------------------------------------

solve.h
/*
  SOLVE.H
  =======
 Interface to maze solving operation
*/


#ifndef PG_SOLVE_H
#define PG_SOLVE_H

#include "maze.h"


/*  Function declarations  */

int solve(struct maze * maze);


#endif  /*  PG_SOLVE_H  */


--------------------------------------------------------------------------------

solve.c
/*
  SOLVE.C
  =======
 
  Implementation of maze solving operation
*/


#include "solve.h"


/*  Recursive function to find a path through a maze  */

int look(struct maze * maze, struct pos pos) {
    int i, n;


    /*  Set new position based on direction  */

    switch ( pos.dir ) {
    case MOVE_UP:
        pos.y -= 1;
        break;
       
    case MOVE_DOWN:
        pos.y += 1;
        break;

    case MOVE_LEFT:
        pos.x -= 1;
        break;

    case MOVE_RIGHT:
        pos.x += 1;
        break;

    default:
        break;
    }


    /*  Check new position  */

    if ( pos.y < 0 || pos.y >= maze->numrows )
        return MAZE_NOWAY;
    else if ( pos.x < 0 || pos.x >= strlen(maze->map[pos.y]) )
        return MAZE_NOWAY;
    else if ( maze->map[pos.y][pos.x] == MAZE_WALL )
        return MAZE_NOWAY;
    else if ( maze->map[pos.y][pos.x] == MAZE_EXIT )
        return MAZE_FOUNDEXIT;
    else if ( maze->map[pos.y][pos.x] == MAZE_ENTRANCE )
        return MAZE_NOEXIT;


    /*  Try all the three directions other than backwards  */

    pos.dir -= 1;
    if ( pos.dir < 0 )
        pos.dir += 4;

    for ( i = 0; i < 3; ++i ) {
        maze->map[pos.y][pos.x] = MAZE_TRAIL;          /*  Leave trail through maze  */

        n = look(maze, pos);
        if ( n ) {
            if ( n == MAZE_NOEXIT )
                maze->map[pos.y][pos.x] = MAZE_PATH;   /*  No exit, so clear trail   */
            return n;
        }

        pos.dir += 1;
        if ( pos.dir > 3 )
            pos.dir -= 4;
    }
   

    /*  Dead end, so clear trail and return  */

    maze->map[pos.y][pos.x] = MAZE_PATH;

    return 0;
}


/*  Function to find a path through a maze  */
   
int solve(struct maze * maze) {
    struct pos pos;

    pos.x = maze->startx;
    pos.y = maze->starty;
    pos.dir = maze->initdir;

    return look(maze, pos);
}   




Sample Output :
--------------------------------------------------------------------------------
maze1.maz
XXXXXXXXXXXXXXXIXXX
X                 X
X XXXXXXXXXXXXXXXXX
X             X   X
X XXX XXXXX X X X X
X   X X   X X X X X
X X X XXX X X X X X
X X X     X X   X X
XXXXXXXXXXXOXXXXXXX
--------------------------------------------------------------------------------
maze2.maz
XXXXXXXXXXXXXXXXXXXX
X X        X       X
X X XXXX XXX XXXXXXX
X X X              X
X X X XXXXX XXX X XX
X XXX X   X X X X XX
I     X   X X XXXXXX
X XXXXX X X        X
X X     X   X   XX O
XXXXXXXXXXXXXXXXXXXX
--------------------------------------------------------------------------------
nowayout.maz
XXXXXXXXXIXXXXXXXXX
X   X XX   XX X   X
X X   X     X   X X
X XXXXX X X XXXXX X
X        O        X
X XXXXX X X XXXXX X
X X   X     X   X X
X   X XX   XX X   X
XXXXXXXXXXXXXXXXXXX


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Program Create Table and Bar Chart Plot From Set Of Temperature Readings


#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>

/* Prototypes */

void  get_temps(void);
void  table_view(void);
void  min_max(int num_vals, int vals[], int *min_val, int *max_val);
float avg_temp(int num_vals, int vals[]);
void  graph_view(void);
void  save_temps(void);
void  read_temps(void);

/* Global defines */

#define TRUE      1
#define READINGS  8

/* Global data structures */

int  temps[READINGS];

int  main(void)
{
   clrscr();
   while (TRUE)
   {
      printf("\nTemperature Plotting Program Menu\n");
      printf("\tE - Enter temperatures for scratchpad\n");
      printf("\tS - Store scratchpad to disk\n");
      printf("\tR - Read disk file to scratchpad\n");
      printf("\tT - Table view of current data\n");
      printf("\tG - Graph view of current data\n");
      printf("\tX - Exit the program\n");
      printf("\nPress one of the above keys: ");

      switch (toupper(getche()))
      {
                 case 'E': get_temps();  break;
                 case 'S': save_temps(); break;
                 case 'R': read_temps(); break;
                 case 'T': table_view(); break;
                 case 'G': graph_view(); break;
                 case 'X': exit(0);
      }
   }
}

/* Function definitions */
void  get_temps(void)
{
   char inbuf[130];
   int  reading;

   printf("\nEnter temperatures, one at a time.\n");
   for (reading = 0; reading < READINGS; reading++)
   {
      printf("\nEnter reading # %d: ", reading + 1);
      gets(inbuf);
      sscanf(inbuf, "%d", &temps[reading]);
   }
}

void  table_view(void)
{
   int  reading, min, max;

   clrscr();                                /* clear the screen */
   printf("Reading\t\tTemperature(F)\n");

   for (reading = 0; reading < READINGS; reading++)
      printf("%d\t\t\t%d\n", reading + 1, temps[reading]);

   min_max(READINGS, temps, &min, &max);
   printf("Minimum temperature: %d\n", min);
   printf("Maximum temperature: %d\n", max);
   printf("Average temperature: %f\n", avg_temp(READINGS, temps));
}

void  min_max(int num_vals, int vals[], int *min_val, int *max_val)
{
   int  reading;

   *min_val = *max_val = vals[0];

   for (reading = 1; reading < num_vals; reading++)
   {
      if (vals[reading] < *min_val)
         *min_val = vals[reading];
      else if (vals[reading] > *max_val)
         *max_val = vals[reading];
   }
}

float avg_temp(int num_vals, int vals[])
{
   int  reading, total = 0;

   for (reading = 0; reading < num_vals; reading++)
      total += vals[reading];

   return   (float) total/reading;  /* reading equals total vals */
}

void  graph_view(void)
{
   int  graphdriver = DETECT, graphmode;
   int  reading, value;
   int  maxx, maxy, left, top, right, bottom, width;
   int  base;                          /* zero x-axis for graph */
   int  vscale = 1.5;       /* value to scale vertical bar size */
   int  space = 10;                     /* spacing between bars */

   char fprint[20];               /* formatted text for sprintf */

   initgraph(&graphdriver, &graphmode, "..\\bgi");
   if (graphresult() < 0)           /* make sure initialized OK */
      return;

   maxx  = getmaxx();              /* farthest right you can go */
   width = maxx /(READINGS + 1); /* scale and allow for spacing */
   maxy  = getmaxy() - 100;              /* leave room for text */
   left  = 25;
   right = width;
   base  = maxy / 2;              /* allow for neg values below */

   for (reading = 0; reading <  READINGS; reading++)
   {
      value = temps[reading] * vscale;
      if (value > 0)
      {
         top = base - value;            /* toward top of screen */
         bottom = base;
         setfillstyle(HATCH_FILL, 1);
      }
      else
      {
         top = base;
         bottom = base - value;      /* toward bottom of screen */
         setfillstyle(WIDE_DOT_FILL, 2);
      }
      bar(left, top, right, bottom);
      left  +=(width + space);       /* space over for next bar */
      right +=(width + space);        /* right edge of next bar */
   }

   outtextxy(0, base, "0 -");
   outtextxy(10, maxy + 20, "Plot of Temperature Readings");
   for (reading = 0; reading < READINGS; reading++)
   {
      sprintf(fprint, "%d", temps[reading]);
      outtextxy((reading *(width + space)) + 25, maxy + 40, fprint);
   }

   outtextxy(50, maxy+80, "Press any key to continue");

   getch();                               /* Wait for a key press */

   closegraph();
}

void  save_temps(void)
{
   FILE * outfile;
   char file_name[40];

   printf("\nSave to what filename? ");
   while (kbhit());  /* "eat" any char already in keyboard buffer */
   gets(file_name);

   if ((outfile = fopen(file_name,"wb")) == NULL)
   {
      perror("\nOpen failed! ");
      return;
   }
   fwrite(temps, sizeof(int), READINGS, outfile);
   fclose(outfile);
}

void  read_temps(void)
{
   FILE * infile;
   char file_name[40] = "test";

   printf("\nRead from which file? ");
   while (kbhit());  /* "eat" any char already in keyboard buffer */
   gets(file_name);

   if ((infile = fopen(file_name,"rb")) == NULL)
   {
      perror("\nOpen failed! ");
      return;
   }
   fread(temps, sizeof(int), READINGS, infile);
   fclose(infile);
}



Sample Input :

Temperature Plotting Program Menu
        E - Enter temperatures for scratchpad
        S - Store scratchpad to disk
        R - Read disk file to scratchpad
        T - Table view of current data
        G - Graph view of current data
        X - Exit the program

Press one of the above keys: E

Enter reading # 2: 34
Enter reading # 3: 42
Enter reading # 4: 38
Enter reading # 5: 42
Enter reading # 6: 40
Enter reading # 7: 39
Enter reading # 8: 28
Temperature Plotting Program Menu
        E - Enter temperatures for scratchpad
        S - Store scratchpad to disk
        R - Read disk file to scratchpad
        T - Table view of current data
        G - Graph view of current data
        X - Exit the program

Press one of the above keys: s
Save to what filename?




Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Generate Random Symbols


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rnd(void);
void seedrnd();
void ramdom();
int main()
{
  int coun;
  FILE *myfile;
  ramdom();
  myfile = fopen("g:/ds/test/symbols.txt","w");
                if(!myfile)
                {
                                puts("Some kind of file error!");
                                exit(0);
                }
                else
                {
                                   for(coun=0;coun<10000;coun++)
                                  {


                                                fprintf(myfile,"%c            ",rnd());
                                  }
                fclose(myfile);
   }
  //getchar(); /*pause*/
  system("PAUSE");
  return 0;
}
void ramdom(void)
{
    int counter;
    seedrnd();
     for(counter=0;counter<10000;counter++)
     {
                printf("%c           ",rnd());
     }
}
int rnd(void)
{
    int randon;
    randon=rand();
    return(randon);
}
void seedrnd(void)
{
    srand((unsigned)time(NULL));
}

Sample Output:
 create symbols.text
            ô        ß        õ        d        ®              ‑        Π        O       a        “        ú        ø        &       Y                 Ü       Ä       Á       è        É        m       ›        +        ÷        h        ™      ®              Œ                
          ‚        ÿ        —                    0        Ê       
          _               ‑        Ë               œ       Ù       ˆ        ù               ñ        ¨         ù        &       6          >        º        ò        ¼       h        9        ¶        ‹        ì         ”        q        Š               ”        L          ©       Á       {        Ô       N       Ý              y               à        æ       G       ]         ·         ô          ã        /         ß        T        É        ð        À       S        Ä       j         I         à      ®       ¥        Π         ~        i                ‑        8        ‑        ý        è        Š        µ        -        P        =        •        Ý          Í         Ó              à        F        \         c               m       w       ¬        ö        [         ¦         +          À       m       Ø       ~        `        ^        1        g               ¥               G       ±               “          á        I         £        ®       L                  ¬        j         9        ð        '         ¼       g        ˆ        Y          Ö       c        ³         §        ¹         …      é        ·         ü        (        4        Ë        Û       )        æ          Z        ƒ        ¢        y        ð        œ       è        Æ       H                     k        §        ;         ì          v        {        ë        Í         V       5        :         t                c        1        Ø       m              ß          Q       #        c        £        Q       E        l         ¶        h        ¬        R       ü        x        X       ¤         
          5               ú        |         à               Π        r        D      
            š          i           ñ          „           3          B         ¸                    e          ‹           g          …        Р                 Æ            ©                  ®         Ñ         Ž          Ö         E          8          Ê         
            "                   
            *          z          í           T          “           <          é          Q         å                   U                  —        Ô         Z                     Ó         Ì           ø                   ‘           Ç         G         è          ö                   î           ß          ì           t            ä          Р                 j           f           8          †         



Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Generate Prime Numbers


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int prime(int number, int * primes);

int main(void) {
    int primes[998] = { 3, 0 };
    int n = 5, i;
    int count = 0, found;
    clrscr();
    printf("%8d%8d%8d", 1, 2, 3);               /*  Print first 3 primes     */


    /*  Find the next 997  */

    while ( count < 997 ) {
        i = 0;
        found = 1;


        /*  Test if number divides by any of the primes already found  */

        while ( primes[i] ) {
            if ( (n % primes[i++]) == 0 ) {   /*  If it does...              */
                found = 0;                    /*  ...then it isn't prime...  */
                break;                        /*  ...so stop looking         */
            }
        }

        if ( found ) {
            printf("%8d", n);                /*  If it's prime, print it...  */
            primes[i] =  n;                  /*  ...and add it to the list   */
            ++count;
       
       
            /*  Start a new line every 8 primes found  */
       
            if ( ((count + 3) % 8) == 0 )
                putchar('\n');
        }

                n += 2;
                //getch();    /*  There's no point testing even numbers, so skip them  */
    }

    putchar('\n');
    getch();

    return EXIT_SUCCESS;
}

Sample Output :
      1        2          3        5         7         11       13       17
      19      23       29       31      37         41       43       47
      53      59        61      67      71        73       79       83
      89      97       101     103    107     109     113      127
     131     137     139     149     151     157     63       167
     173     179     181     191    193     197     199      211
     223     227     229     233     239     241     251     257
     263     269     271     277     281     283     293     307
     311     313     317     331     337     347     349     353
     359     367     373     379     383     389     397     401
     409     419     421     431     433     439     443      449
     457     461     463     467     479     487     491     499
     503     509     521     523     541     547     557     563
     569     571     577     587     593     599     601     607
     613     617     619     631     641     643     647     653
     659     661     673     677     683     691     701     709
     719     727     733     739     743     751     757     761
     769     773     787     797     809     811     821     823
     827     829     839     853     857     859     863     877
     881     883     887     907     911     919     929     937
     941     947     953     967     971     977     983     991
     997    1009    1013   1019   1021   1031   1033   1039
    1049   1051   1061    1063   1069   1087   1091   1093
    1097   1103   1109    1117   1123   1129   1151   1153
     .                  .           .           .           .           .           .          
     .                  .           .           .           .           .        7907



Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Find Sum Of Series


#include <stdio.h>
#include <conio.h>
long int factorial(int n);
void main()
{
 int n,i;
 float s,r;
 char c;
 clrscr();
 repeat : printf("\nYou have this series:- 1/1! + 2/2! + 3/3! + 4/4! ...");
 printf("\nTo which term you want its sum?  ");
 scanf("%d",&n);
 s=0;
 for (i=1;i<=n;i++)
  {   s=s+((float)i/(float)factorial(i)); }
 printf("\nThe sum of %d terms is %f",n,s);
 fflush(stdin);
 printf ("\nDo you want to continue?(y/n):-  ");
 scanf("%c",&c);
 if (c=='y')
  goto repeat;
 getch();
}

long int factorial(int n)
 {
  if (n<=1)
                return(1);
  else
                n=n*factorial(n-1);
                return(n);
 }

Sample Output :
     You have this series:- 1/1! + 2/2! + 3/3! + 4/4! ...
To which term you want its sum?  15
The sum of 15 terms is 2.709237
Do you want to continue?(y/n):-  n


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Find Number Of Days Between Two Dates


#include<stdio.h>
#include<math.h>
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("\nEnter first day, month, year");
scanf("%d%d%d",&day1,&mon1,&year1);
printf("\nEnter second day,month,year");
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2<year1)
ref = year2;
dd1=0;
dd1=func1(mon1);
for(i=ref;i<year1;i++)
{
                if(i%4==0)
                dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
printf("\nNo. of days of first date fronm the Jan 1 %d= %d",year1,dd1);
/* Count for additional days due to leap years*/
dd2=0;
for(i=ref;i<year2;i++)
{
                if(i%4==0)
                dd2+=1;
}
dd2=func1(mon2)+dd2+day2+((year2-ref)*365);
printf("\nNo. of days from the reference year's first Jan = %d",dd2);
printf("\nTherefore, diff between the two dates is %d",abs(dd2-dd1));

getch();
}




int func1(x)       //x for month y for dd
{  int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120;break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: printf("Error encountered"); exit(1);
}
return(y);
}


Sample Output :
Enter first day, month, year29 7 2009
Enter second day,month,year12 12 2009
No. of days of first date fronm the Jan 1 2009= 210
No. of days from the reference year's first Jan = 346
Therefore, diff between the two dates is 136


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Find Digits, Characters , Spaces , Words , Lines and Specialcharacters


#include<stdio.h>
main()
{
FILE *fp;
char c;
int i,s=0,l=0,sp=0,ch=0,d=0;
clrscr();
fp=fopen("file1","w");
while((c=getchar())!=EOF)
putc(c,fp);
fclose(fp);
fp=fopen("file1","r");
while((c=getc(fp))!=EOF)
{
i=c;
if(c>='0'&&c<='9') d++;
else if(i>=65&&i<=90)ch++;
else if(i>=97&&i<=122)ch++;
else if(c=='\n')l++;
else if(c==' ')s++;
else sp++;
}
fclose(fp);
printf("\nDIGITS:%d",d);
printf("\nCHARACTERS:%d",ch);
printf("\nSPACES:%d",s);
printf("\nWORDS:%d",++s);
printf("\nLINES:%d",++l);
printf("\nSPECIAL CHARACTERS:%d",sp);
getch();
}


Sample Output Screen:
Hai welcome to file.
^Z
DIGITS:0
CHARACTERS:16
SPACES:3
WORDS:4
LINES:2
SPECIAL CHARACTERS:1


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Draw Ellipse in Various Shapes


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int xasp, yasp, midx, midy;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "..//bgi");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }

   midx = getmaxx() / 2;
   midy = getmaxy() / 2;
   setcolor(getmaxcolor());

   /* get current aspect ratio settings */
   getaspectratio(&xasp, &yasp);

   /* draw normal circle */
   circle(midx, midy, 200);
   getch();

   /* clear the screen */
   cleardevice();

   /* adjust the aspect for a wide circle */
   setaspectratio(xasp/2, yasp);
   circle(midx, midy, 200);
   getch();

   /* adjust the aspect for a narrow circle */
   cleardevice();
   setaspectratio(xasp, yasp/2);
   circle(midx, midy, 200);

   /* clean up */
   getch();
   closegraph();
   return 0;
}


Contact:

Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Display Permutation Of Given Numbers


#include<stdio.h>
#include<stdlib.h>
int lev=-1,n,val[50],a[50];
void main()
{
 int i,j;
 clrscr();
 printf("Enter Numbers to make Permutaion");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  val[i]=0;
  j=i+1;
  scanf("%d",&a[j]);
 }
 visit(0);
 getch();
}
visit(int k)
{
 int i;
 val[k]=++lev;
 if(lev==n)
 {
  for(i=0;i<n;i++)
  printf("%2d",a[val[i]]);
  printf("\t");
 }
 for(i=0;i<n;i++)
 if(val[i]==0)
 visit(i);
 lev--;
 val[k]=0;
}


Output:
Enter Numbers to make Permutaion4
1
2
3
4
 1 2 3 4         1 2 4 3         1 3 2 4         1 4 2 3         1 3 4 2
 1 4 3 2         2 1 3 4         2 1 4 3         3 1 2 4         4 1 2 3
 3 1 4 2         4 1 3 2         2 3 1 4         2 4 1 3         3 2 1 4
 4 2 1 3         3 4 1 2         4 3 1 2         2 3 4 1         2 4 3 1
 3 2 4 1         4 2 3 1         3 4 2 1         4 3 2 1


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Display Given Year Calender


#include<stdio.h>
main()
{
int i,j,n,y,d[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char m[12][5]={"JAN","FEB","MAR","APL","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
long nod;
clrscr();
printf("ENTER THE YEAR TO PRINT:");
scanf("%d",&y);
nod=(365L*(y-1)+(y/4));
if(y%4==0) d[1]=29;
n=nod%7+1;
if(y%4==0&&y!=2000) n--;
for(i=0;i<12;i++)
{ clrscr();
printf("\n\t\t\t%s\t%d",m[i],y);
printf("\n\n\tSUN\tMON\tTUE\tWED\tTHR\tFRI\tSAT\n\t");
for(j=1;j<n;j++) printf("\t");
for(j=1;j<=d[i];j++)
{
printf("%d\t",j); n++; if(n>7){ printf("\n\t"); n=1; }
}
getch();
}
}



Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

C Language Program To Display Disk Space in Dos System


#include <stdio.h>
#include <dos.h>
#include <conio.h>
void main(void)
{
struct dfree diskinfo;
long disk_space;
clrscr();
getdfree(3, &diskinfo);
disk_space = (long) diskinfo.df_avail * (long) diskinfo.df_bsec * (long) diskinfo.df_sclus;
printf("Available disk space %ld\n", disk_space);
clrscr();
}



Sample Output :
Available disk space 215359488


Contact:
Mr. Roshan P. Helonde
Mobile: +91-7276355704
WhatsApp: +91-7276355704
Email: roshanphelonde@rediffmail.com
Share:

Total Pageviews

CONTACT US

Prof. Roshan P. Helonde
Mobile: +917276355704
WhatsApp: +917276355704
Email: roshanphelonde@rediffmail.com

Enter Project Title

Popular Projects

All Archive

Contact Form

Name

Email *

Message *