Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Sunday, December 13, 2015

Progress Bar with Execution time in C Program

Program #31

Description:
System will randomly generates unique (x,y) co-ordinates and in the front user can see the Progress Bar and Execution time.

/*
 Make 5 dots randomly in different (x,y) positions in the Screen
 Also Program will dynamically show the progress using Progress Bar Logic
 using C Program
*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
/* Defined constants */
#define MAXDOTS 5;
#define ASCII_POSITION  122;
#define PROGRESS_ROW     13
#define PROGRESS_COLUMN  28

/* !Important!
This Program defines the Progress Bar ASCII Positions as
79, 80, 81 and 122
79  - illusion Progress Bar
122 - Completion Progress Bar
*/

/* Global variable declaration */
int row=1, col=1, rowTrack[5], colTrack[5];

/* Global function declaration */
char getAscii(int);
int main(){

  /* Local variable declaration */
  int counter=0, dupCheck=0, maxDots;
  char asciiChar;
  clock_t startTime, stopTime;

  /* Function Prototype declaration */
  void getX();
  void getY();
  int duplicateCheck(int, int);
  void printCoOrdinates();
  void initialMessage();
  void drawProgressBar(int);
  void percentCompleted();
  void illusionProgressBar();
  void printExecutionTime(clock_t);

  /* Clear the Screen */
  clrscr();

  /* Start Execution Time */
  startTime = clock();

  //printf("Start Time %f",startTime);
  /* Print Initial Message to the User */
  initialMessage();

  /* Print illusion Progress Bar */
  illusionProgressBar();

  /* Indefinite loop */
  for (;!kbhit(); ){

    /* Randomize */
    randomize();

    /* get a next random (x,y) position */
    getX();
    getY();

    /* Initial value to Duplicate check */
    dupCheck = 0;

    /* Track Positions, avoid duplicate co-ordinates */
    if ( !duplicateCheck(row, col) ){

      /* no duplicate matched */
      dupCheck = 1;
    }
    if (dupCheck == 1){

      /* Sets current position to Positioning Array */
      rowTrack[counter] = row;
      colTrack[counter] = col;

      /* Increase DOT counter */
      counter++;
    }

    /* Draw Progress Bar about the Processing */
    drawProgressBar(counter);

    /* Print Percentage Completed */
    percentCompleted(counter);
    if (dupCheck == 1){

      /* Move Cursor to random position */
      gotoxy(col, row);
      printf("*");
    }
    maxDots = MAXDOTS;

    /* program ends, once counter reaches MAXDOTS */
    if (counter==maxDots){

      /* Print Dynamic Co-Ordinates */
      printCoOrdinates();
      break;
    }

     /* Display Execution time */
     printExecutionTime(startTime);
  }
  getch();
  return 1;
}

/* Print Execution Time */
void printExecutionTime(clock_t startTime){
   double execTim;
   int finalSec, color;

   clock_t stopTime = clock();

   /* Convert Milliseconds into Seconds, for user friendly */
   execTim =  ((double)(stopTime-startTime))/CLOCKS_PER_SEC;
   finalSec = (int)execTim;

   gotoxy(30, 10);

   /*  print execution message */
   printf("Execution Time: %d (secs)",finalSec);
}


/* Print illusion Progress Bar, for User Friendly ;) */
void illusionProgressBar(){
 int i, columnPos, rowPos, dynamicProgress;
 char asciiVal;


 /* get Default Row, Column Position */
  columnPos = PROGRESS_COLUMN;
  rowPos    = PROGRESS_ROW;


  /* Dynamic Progress Bar length using Completion % */
  dynamicProgress = 25;


  /* Get Ascii character */
  asciiVal = getAscii(79);


  /* default progress bar position */
  for(i=0; i<dynamicProgress; i++){
   gotoxy(columnPos++, rowPos);
   printf("%c",asciiVal);


   /* Add delay to draw illusion progress bar, smoothly */
   delay(10);
  }

  /* Delay after illusion progress Bar */
  delay(300);
}


/* Print Percentage Completion */
void percentCompleted(int completion){
 int columnPos, rowPos, dynamicProgress;

 /* get Default Row, Column Position */
  columnPos = PROGRESS_COLUMN + 27;
  rowPos    = PROGRESS_ROW;

  /* Dynamic Progress Bar length using Completion % */
  dynamicProgress = completion * 20;

  /* Positioning % completion */
  gotoxy(columnPos, rowPos);
  printf("%d% Completed", dynamicProgress);
}

/* Draw Progress Bar to show Current Completion status to the User */
void drawProgressBar(int completion){
  int i, columnPos, rowPos, dynamicProgress;
  char asciiVal;


  /* get Default Row, Column Position */
  columnPos = PROGRESS_COLUMN;
  rowPos    = PROGRESS_ROW;


  /* Dynamic Progress Bar length using Completion % */
  dynamicProgress = completion * 5;


  /* Get Ascii character */
  asciiVal = getAscii(0);


  /* default progress bar position */
  gotoxy(columnPos, rowPos);

  for(i=0; i<dynamicProgress; i++){
   gotoxy(columnPos++, 13);
   printf("%c",asciiVal);


   /* Add delay to draw progress bar, smoothly */
   delay(100);
  }
}


/* Print Intial Message to the User */
void initialMessage(){
  char *plsMessage = "Please wait ... (Press ANY Key to Exit)";
  int i, columnPos=20;
  for(i=0; i<strlen(plsMessage); i++){
    gotoxy(columnPos++,12);
    printf("%c",plsMessage[i]);
    delay(50);
  }
}

/* Print dynamic co-ordinates */
void printCoOrdinates(){
  int i=0;
  /* Print the Result from 1st Row to 5th Row */
  for (; i<5; i++) {
    gotoxy(1,(i+1));
    printf("Dynamic Position #%d: (%d,%d)", (i+1), colTrack[i], rowTrack[i]);
  }
}

/*
  get Random x position
  'x' means column value
*/
void getX() {
  int x=1;
  x = random(80);

  /* randVal range from 0 to 99
   but we are expecting COLUMN value
   but it ranges from 1 to 80 only */
  if (x<1 || x>80){
    getX();
  }
  col = x;
}

/*
  get Random y position
  'y' means row value
*/
void getY() {
  int y=1;

  /* get random for Row */
  y = random(25);

  /* randVal range from 0 to 99
   but we are expecting ROW value
   but it ranges from 1 to 25 only */
  if (y<1 || y>25){
    getY();
  }
  row = y;
}

/* Duplicate Position Check */
int duplicateCheck(int r, int c) {
  int i;
  int rowMatch=0, colMatch=0;

  /* check Row Value match with Existing Value */
  for (i=0; i<5; i++) {
     if (rowTrack[i] == r){
       rowMatch = 1;
     }
  }

  /* check Column Value match with Existing Value */
  for (i=0; i<5; i++) {
     if (colTrack[i] == c){
       colMatch = 1;
     }
  }
  if (rowMatch == 0 && colMatch == 0) {
    return 0;
  }
  return 1;
}

/* Iterate the loop until expected ASCII character */
char getAscii(int overWritePos){
  int i, ascii_pos;

  /* Default position to the Ascii, !Important! */
  char cc='a';

  /* Assign default character position */
  ascii_pos = ASCII_POSITION;

  /* Overwrite Ascii Position, if function gets some position */
  if (overWritePos != 0){
    ascii_pos = overWritePos;
  }
  for (i=0; i<256; i++) {

    /*  Break the Loop, once we got the Character */
    if (i == ascii_pos)  break;
    cc = cc + 1;
  }
  return cc;
}




 

Draw Progress Bar using C Program

Program #29

Description:
Draw Progress Bar using C Program.  ASCII character plays to vital role to make it.
ASCII Positions of { 176, 177, 178 and 220} are good to make Progress Bar.

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

/* Define default postion to
Row, Column, Progress bar length and ASCII Position used to make progress Bar
*/
#define DEFAULT_COL 25;
#define DEFAULT_ROW 12;
#define BLOCKS_TO_PRINT 25;
#define ASCII_POSITION 220;

/* !Important:
  Character Position 176, 177, 178 and 220
  are good to make Progress Bar */

void main() {
   int i, col, row, blocks;
   char myAscii;

   /* prototype declaration */
   char getAscii();

   /* Clear the Screen */
   clrscr();

   /* Set default position to print */
   col     = DEFAULT_COL;
   row     = DEFAULT_ROW;
   blocks  = BLOCKS_TO_PRINT;

   /* get expected ASCII */
   myAscii = getAscii();

   for(i=1;i<blocks;i++) {
      gotoxy(col++, row);
      printf("%c", myAscii);
      delay(50);
   }

   getch();
}

/* Iterate the loop until expected ASCII character */
char getAscii(){
  int i, ascii_pos;
  char cc;

  /* Assign default character position */
  ascii_pos = ASCII_POSITION;
  for (i=0; i<256; i++) {

    /*  Break the Loop, once we got the Character */
    if (i == ascii_pos)  break;
    cc = cc + 1;
  }
  return cc;
}



 

Slow Motion print using C Program

Program #28

Description:
Slow motion print using C Program.  Default (column,row) set as (35,12).

/*
 Please wait ... Message print
 using C Program
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main(){

  /* function prototype declaration */
  void initialMessage();

  /* Clear the Screen */
  clrscr();

  /* Print Initial Message to the User */
  initialMessage();

  getch();
}

/* Print Message */
void initialMessage(){

  char *plsMessage = "Please wait ...";

  int i, columnPos=35;
  for(i=0; i<strlen(plsMessage); i++){
    gotoxy(columnPos++,12);
    printf("%c",plsMessage[i]);
    delay(50);
  }
}





 

Saturday, December 12, 2015

Get Random Row y position using C Program

Program #27

Description:
Get a random Y (Row) position using C Program.

#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
void main(){

  /* Local variable declaration */
  int randVal;

  /* Clear the Screen */
  clrscr();

  for (;!kbhit();){

    /* get random for Column */
    randVal = ( rand() % 100 );

    /* randVal range from 0 to 99
    but we are expecting ROW value
    but it ranges from 1 to 25 only */

    if (randVal >= 1 && randVal <= 25){
      printf("%d\t", randVal%100);
      delay(25);
    }
  }
  getch();
}


 

Get Random Column x position using C Program

Program #26

Description:
Get a random X (Column) position using C Program.

#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
void main(){
  /* Local variable Declaration */
  int randVal;
 
  /* Clear the Screen */
  clrscr();

  for (;!kbhit();){

    /* get random for Column */
    randVal = ( rand() % 100 );
   
 /* randVal range from 0 to 99,
 but we need value range from 1 to 80
 because of COLUMN position range from 1 to 80
 */
    if (randVal >= 1 && randVal <= 80){
      printf("%d\t", randVal%100);
     
   /* Add some delay to see the Random number in the Output screen */
   delay(25);
    }
  }

  getch();
}


Simple Animation using Arrow Keys in C Program

Program #25

Description:
Write simple animation using Arrow key (Up, Down, Left and Right) only in C Program.


/* Arrow Programming in C Program
UP Arrow    - 72
DOWN Arrow  - 80
LEFT Arrow  - 75
RIGHT Arrow - 77
*/

#include<conio.h>

/* Global variable declaration, Row and Column */
int row=13, col=40;
void main(){
  /* Local variable declaration */
  char inputKey, arrowKey;

 /* Prototype declaration */
  void moveUp();
  void moveDown();
  void moveLeft();
  void moveRight();
 
/* Clear the Screen */
  clrscr();
 
/* Move Cursor to Center of the Screen by Default */
  gotoxy(col, row);

  /* Detect Arrow Keys in C Program
     Press ESCAPE - 27 to
     Get out from the Indefinite loop
  */

  while ( (inputKey = getch()) != 27){

    /* Find Arrow Key or not */
    if (inputKey == '\0'){

      /* Extract actual Arrow Key */
      arrowKey = getch();
      switch(arrowKey){
 case 72:
    //printf("UP");
    moveUp();
    break;

 case 80:
    //printf("DOWN");
    moveDown();
    break;

 case 75:
    //printf("LEFT");
    moveLeft();
    break;

 case 77:
    //printf("RIGHT");
    moveRight();
    break;
      }
    }
  }
}

/* Move Cursor - Upward */
void moveUp(){
 if (row != 1){
   row--;
 } else if (row < 1){
   row = 1;
 }

/* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}

/* Move Cursor - Downward */
void moveDown(){
 if (row != 25){
   row++;
 } else if (row > 25){
   row = 25;
 }

 /* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}

/* Move Cursor - Left Side */
void moveLeft(){
 if (col != 1){
   col--;
 } else if (col < 1){
   col = 1;
 }

 /* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}


/* Move Cursor - Right Side */
void moveRight(){
 if (col != 80){
   col++;
 } else if (col > 80){
   col = 80;
 }
 
/* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}



 

Get Alphabet Input using C Program

Program #24

Description:
To detect only character input either lower/upper character and print.  Rest of the keypress are ignored.

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

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];

int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);
 clrscr();

 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }

 printf("\nPlease Enter Alphabets value [a-zA-Z]:\n");
 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }

    /* Accept only Lower Case charaters, else ignore */
    else if (c >= 97 && c <= 122){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }

    /* Accept only Upper Case charaters, else ignore */
    else if (c >= 65 && c <= 90){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
  printf("          ");
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}



 

To Detect or Recognize Arrow Keys in C Program


Program #23

Description:
To Detect/Recognize Arrow Keys in C Program


/* Program to Detect Arrow Keys in C Program
UP Arrow    - 72
DOWN Arrow  - 80
LEFT Arrow  - 75
RIGHT Arrow - 77
*/

void main(){
  char inputKey, arrowKey;

 /* Clear the Screen */
  clrscr();
 
/* Detect Arrow Keys in C Program
     Press ESCAPE - 27 to
     Get out from the Indefinite loop
  */
  while ( (inputKey = getch()) != 27){

  /* Find Arrow Key or not */
    if (inputKey == '\0'){
     
/* Extract actual Arrow Key */
      arrowKey = getch();
      switch(arrowKey){
 case 72: printf("UP"); break;
 case 80: printf("DOWN"); break;
 case 75: printf("LEFT"); break;
 case 77: printf("RIGHT"); break;
      }
    }
  }
}



 

Detect Function Keys in C Program

Program #22

Description:
To get the Function key values using getch()


void main(){
  char inputKey;
 
/* Clear the Screen */
  clrscr();

  /* Detect Function Keys in C Program */
  while ( (inputKey = getch()) != '\r'){
    printf("%c:%d\n", inputKey, inputKey);
  }
}

 

Simple Animation - Drawing Border using C Program


Program #21

Description:
To draw Border in the Screen using dots.  All dots are in multi-colors. Once the border drawing completed.  System will prints success message.


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

/* global prototype declaration */
void setTextColor();

int main(){
  int leftPos=1, rightPos=25;
  int botPos=1, topPos=80;

 /* prototype declaration */
  void drawLeftBorder(int);
  void drawRightBorder(int);
  void drawTopBorder(int);
  void drawBottomBorder(int);
  void initialMessage();
  void finalMessage();
 
/* Clear Screen */
  clrscr();

  /* Initial Message */
  initialMessage();

  for(;!kbhit();){

     /* take a random color */
     setTextColor();

     if (botPos == 1 && topPos == 80 && leftPos <= 25 && rightPos >= 1){
       drawLeftBorder(leftPos++);
       drawRightBorder(rightPos--);
     }
     if (leftPos == 25 && rightPos == 1 && topPos >= 1 && botPos <= 80){
       drawBottomBorder(botPos++);
       drawTopBorder(topPos--);
     }

     if(leftPos == 25 && rightPos == 1 && botPos == 80 && topPos == 1){
       finalMessage();
       getch();

       /* End the Program Execution */
       return 0;
     }
  }
  getch();
  return 0;
}

/*  Draw Left Border */
void drawLeftBorder(int leftPos){
 gotoxy(1,leftPos);
 cprintf("*",leftPos);
 delay(100);
}

/*  Draw Right Border */
void drawRightBorder(int rightPos){
 gotoxy(80,rightPos);
 cprintf("*",rightPos);
 delay(100);
}

/*  Draw Top Border */
void drawTopBorder(int topPos){
 gotoxy(topPos,1);
 cprintf("*",topPos);
 delay(100);
}

/*  Draw Bottom Border */
void drawBottomBorder(int bottomPos){
 gotoxy(bottomPos, 25);
 cprintf("*",bottomPos);
 delay(100);
}

void finalMessage(){
  int i;
  gotoxy(30,12);
  setTextColor();
  cprintf("Border Drawing Completed!");
  gotoxy(30,13);
  for(i=1;i<=25;i++){
    printf("_");
    delay(100);
  }
}

void initialMessage(){
  int i;
  gotoxy(30,13);
  setTextColor();
  cprintf("Press any Key to EXIT!");
  gotoxy(30,14);
  for(i=1;i<=22;i++){
    printf("-");
    delay(100);
  }
}

void setTextColor(){
  int a;
  a = random(15);
  if (a == 0) {
    /* Restrict BLACK color */
    setTextColor();
  } else {
    textcolor(a);
  }
}



 

Friday, December 11, 2015

Get number input using C Program


Program #20

Description:
Get number [0-9] only as input from the user.  Even if user press characters, system will ignore and wait for valid input.  Once input reaches MAXLENGTH (user defined length) program will stop the execution.


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

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];
int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);

 clrscr();
 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }
 printf("\nPlease Enter Integer value [0-9]:\n");

 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }
   
/* Accept only Numbers, else ignore */
    else if (c >= 48 && c <= 57){
      userInput[charCounter++] = c;
     
/* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
 
  /* Clear the Output area */
  printf("          ");
 
  /* Start to print on perfect position */
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}


 

 

Identify Key Press using C program


Program #19


Description:
Identify Key Press using C program

int main(){
 char c=' ';
 clrscr();
 printf("\nPlease press a SPACE Bar to EXIT:\n");
 for(;!kbhit();){
    c = getch();
    if (c == ' '){
      return 0;
    } else {
      printf("%c:%d\t",c,c);
    }
 }
 getch();
 return 0;
}


 

Simple Animation to Fill Box color using C Program



Program #18

Description:
Dynamically plotting dots on a specific area in the screen using multi-colors.  Once all positions plotting is done.  Program will come to an end.

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

/* Global Array Declaration */
int Box[50][50];

int main(){
  int counter=0, rowValue=1, colValue=1;
  int boxStatus, colorValue;

  /* function prototype declaration */
  int getRow();
  int getColumn();
  int checkBoxCompletion();
 
/* Clear the Screen */
  clrscr();

 /* Iterate Indefinite loop */
  for(;!kbhit();){
    rowValue = getRow();
    colValue = getColumn();
    /* set Box position using Currrent Row,Column */
    if (Box[rowValue][colValue] != 1){
      Box[rowValue][colValue] = 1;
    } else {

 /* avoid overwritting plotted color
      get a new blank position.
      If you wish, just comment below "continue"
      and see the difference
      */
      continue;
    }

  /* Re-initialize counter as 0 once it reaches INT max value */
    if (counter == 32766) counter = 0;

  /* Increase the Counter */
    counter++;

 /* get random color */
    colorValue = getColor();

   /* set color using randow value */
    textcolor(colorValue);
    /* Move the cursor position */
    gotoxy(colValue, rowValue);

   /* do color print */
    cprintf("*");
   
/* set some delay in Milliseconds
    as much you decrease, that much fast system will
    fill the color + add mild-sound
    */
    sound(200*rowValue);
    delay(10);
   
/* after delay, mute the sound */
    nosound();
   
/* check box completion station after every dot plot */
    boxStatus = checkBoxCompletion();
    if (boxStatus == 1) {
      gotoxy(18, 18);

 /* print the success message using last dot color, Interesting!! */
      cprintf("Box Color Fill: Completed Successfully !!");
      getch();

  /* stop the execution */
      return 1;
    }
   }
   return 0;
}

int getRow(){
  int rowNum;

  /* getting a randow row */
   rowNum = random(25);
  if (rowNum<8 || rowNum>15){

 /* get next random */
    getRow();
  } else {
    return rowNum;
  }

  /* worst case, default return value */
  return 8;
}

int getColumn(){
  int colNum;

 /* getting a randow column */
   colNum = random(80);
   if (colNum<25 || colNum>50){

  /* get Next Column */
      getColumn();
   } else {
      return colNum;
   }
   /* worst case, default return value */
  return 25;
}

int getColor(){
   int color;

 /* getting a randow color */
   color = random(15);
   if (color == 0){

   /* 0 means BLACK color, it will spoil the Spot */
     getColor();
   } else {
     return color;
   }

  /* worst case, default return value */
   return 1;
}

int checkBoxCompletion(){
  int i,j;
  for (i=8;i<=15;i++){
    for (j=25; j<=50; j++){
      if(Box[i][j] != 1){

/* Color fill not yet completed */
 return 0;
      }
    }
  }
 
/* Color fill Finished */
  return 1;
}

 

Simple Animation using C Program


Program #17

Description:
Randomly move the position and rotate {/ \ | } symbols.


#include<conio.h>
#include<stdlib.h>
#include<dos.h>
void main(){
  int counter=0, rowValue=1, colValue=1, colorValue;

 /* function prototype declaration */
  int getRow();
  int getColumn();

 /* Clear the Screen */
  clrscr();

 /* Iterate Indefinite loop */
  for(;!kbhit();){
   
/* Move the Rotation every 32766 counter */
    if (counter%32766 == 0){
   /* Get New Row and Column */
      rowValue = getRow();
      colValue = getColumn();
     
/* Re-initialize counter as 0 once it reaches INT max value */
      if (counter == 32766){
 counter = 0;

/* Delay for a while in Milliseconds */
 delay(100);
      }
  
 /* Clear the Screen, and show a fresh as Rotation */
      //clrscr();
    }

   /* Increase the Counter */
    counter++;

    colorValue = getColor();
    textcolor(colorValue);
    gotoxy(colValue, rowValue);
    cprintf("|");
    gotoxy(colValue, rowValue);
    cprintf("/");
    gotoxy(colValue, rowValue);
    cprintf("\\");
   }
}

int getRow(){
  int rowNum=1, i=0;
  for(;i<25;i++){
   /* getting a randow row, if system gives 0, trying next number */
   rowNum = random(25);
    if (rowNum!=0){
      return rowNum;
    }
  }
  /* worst case, hope it won't return */
  return rowNum;
}

int getColumn(){
  int colNum=1, i=0;
  for(;i<80;i++){
   /* getting a randow column, if system gives 0, trying next number */
   colNum = random(80);
    if (colNum!=0){
      return colNum;
    }
  }
  /* worst case, hope it won't return */
  return colNum;
}

int getColor(){
   int color;
   /* getting a randow color */
   color = random(15);
   return color;
}


 

Indefinite loop in C Program


Program #16

Description:
To iterate indefinite loop until user press any keyboard characters.

void main(){
clrscr();
 /*
 Indefinite loop using kbhit() function
 kb  - KeyBoard
 hit - Type/Enter
 */
 for(;!kbhit();){
    printf(" * \t @ \t & \t %");
 }
 getch();
}

Note:
kbhit() functionality will not obey for few keyboard characters.. I am expecting the answer from blog readers.  Once you know the key.. Reply it.!!

Wednesday, December 9, 2015

Find a String is Palindrome or not in C Program

Program #15

Description:
To find a given string is PALINDROME or not

What is palindrome?

DUMMY != YMMUD
PALINDROME != EMORDNILAP

LIRIL   = LIRIL
AMMA = AMMA

If a string and its reversed string is same then that string is a "Palindrome".

#include<stdio.h>
void main(){
 char palind[50], compar[50];
 int i,j=0, IsPalind;

 clrscr();
 printf("Please Enter a String to find Palidrome or not?\n");
 scanf("%s", &palind);

 for(i=strlen(palind)-1; i>=0; i--){
   /* Assign last char of Input string into first position of Compare array */
   compar[j]=palind[i];
   j++;
 }

 /* Compare both array to find Palindrome or not */
 IsPalind = 1;
 for(i=0; i<strlen(palind); i++){
   if (palind[i] != compar[i] ){
      IsPalind = 0;
      /* If single character not match, go out of the Loop*/
      break;
   }
 }

 printf("\n\nResult:\n");
 printf("-------\n");

 if (IsPalind){
   printf("Input String is a Palindrome !!");
 } else {
   printf("Invalid Input, Better luck next time !!");
 }

 getch();
}

Sunday, December 6, 2015

Text and Background color print in C Program

Program #14

Description:
Set text color as well background color in C Program.

#include<stdio.h>
#include<conio.h>
int main(void)
{
   int i;

   clrscr();
   for (i=0; i<15; i++)
   {

       /* Just positioning to center */
       gotoxy(30,i+1);

       cprintf("Let Us C");
       cprintf("\r\n");
   
    /* set text color */
       textcolor(i+1);
   
    /* set background color */
       textbackground(i);
   }

   getch();
   return 0;
}



Draw a box in C Program

Program #13

Description:
Draw a box dynamically in the screen based on co-ordinates and box size.

#include <stdio.h>
#include <conio.h>
void main(){
 int x,y,boxSize,i,getY,getX;
 clrscr();

 printf(" Enter the co-ordinate to Draw Box (x,y) ;\n");
 printf(" x (column):");
 scanf("%d", &x);
 printf(" y (row):");
 scanf("%d", &y);


/* validate co-ordinates */
 if (x != y) {
   printf("Invalid Co-ordinates!!");
   getch();
   /* Stop the Execution */
   abort();
 }
 printf(" Enter size of the Box:\n");
 scanf("%d",&boxSize);

/* Validate possibility of Drawing Box */
 if ((x+boxSize*2) > 80 ){
   printf("Invalid X Co-ordinate!!");
   getch();
   /* Stop the Execution */
   abort();
 } else if ((y+boxSize) > 25){
   printf("Invalid Y Co-ordinate!!");
   getch();
   /* Stop the Execution */
   abort();
 }


/* Move the cursor (x,y) and Draw top line */
 gotoxy(x, y);
 for(i=1; i<=boxSize; i++){
    printf(" *");
 }


 /* Move the cursor (x,y) and Draw left line */
 getY = y+1;
 gotoxy(x,y);
 for(i=1; i<boxSize-1; i++){
    gotoxy(y, getY);
    printf("*");

    /* To move cursor to next row */
    getY++;
 }


 /* Move the cursor (x,y) and Draw bottom line */
 gotoxy(x, y+boxSize-1);
 for(i=1; i<=boxSize; i++){
    printf(" *");
 }


/* Move the cursor (x,y) and Draw right line */
 getY = y+1;
 getX = x+boxSize*2;
 for(i=1; i<boxSize-1; i++){
    gotoxy(getX, getY);
    printf("*");
    /* To move cursor to next row */
    getY++;
 }
 getch();
}


 

Saturday, December 5, 2015

main() function and return datatype in c

Description:
Why we need to keep void always to main().
Shall we try assign different type of datatypes?

Program #8
/* Usual way of void as return to main() */
# include "stdio.h"
void main(){
  clrscr();
  printf("void main() function in c (no more return) !!");
  getch();
 
  //no return
}

Output:
void main() function in c (no more return) !!

Program #9
/* int as return datatype to main() */
# include "stdio.h"
int main(){
  clrscr();
  printf("main() function in c (return integer) !!");
  getch();
 
  //return integer value, any as you wish
  return 1306;
}

Output:
main() function in c (return integer) !!

Program #10
/* float as return datatype to main() */
# include "stdio.h"
float main(){
  clrscr();
  printf("main() function in c (return float) !!");
  getch();
 
  //return float value, any as you wish
  return 13.06;
}

Output:
main() function in c (return float) !!

Program #11
/* char as return datatype to main() */
# include "stdio.h"
char main(){
  clrscr();
  printf("main() function in c (return char) !!");
  getch();
 
  //return char value, any as you wish
  return 'm';
}

Output:
main() function in c (return char) !!

Program #12
/* double as return datatype to main() */
# include "stdio.h"
double main(){
  clrscr();
  printf("main() function in c (return double) !!");
  getch();
 
  //return double value, any as you wish
  return 130600;
}







Output:
main() function in c (return double) !!

Different method of include header files in C

Program #7

Description:
In this program, {stdio.h} included 3 times and in 3 different formats.
But still it produced 1 output (without any warning / error).

Reason:
C compiler just keeping the included files and match with the used functions.
In case header files and functions used are match then it doesn't care about
how many times we included the same header file.

#include<stdio.h>
# include <stdio.h>
# include "stdio.h"

int main(){
  clrscr();
  printf("Thanks for viewing Different format of Include Header files!!");

  getch();
  return 0;
}

Popular Posts