Thursday, November 5, 2020

Fill Color or Fore color using C Program

 #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;

  int getColor();

  /* 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(50);

   /* 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;

}





Custom Clear Screen using C Program

 /*

  *****************************************************

    CUSTOM CLRSCR() function

    Clear the Screen using HORIZONTAL & VERTICAL wise

  *****************************************************

*/

#include <stdio.h>

#include <conio.h>

#include <dos.h>

void main(){

  int i,j;

  /* ------------------------------ */

  /* Message about Horizontal Clear */

  /* ------------------------------ */

     /* Fill the screen with @ symbol */

     for (i=1; i<=80;i++){

      for (j=1; j<=25; j++){

printf("@");

      }

    }


     gotoxy(20,13);

     textcolor(9);

     cprintf("Custom CLRSCR using HORIZONTAL !");

    /* Perform custom clrscr() by clearing in HORIZONTAL */

    for (j=1; j<=25; j++){

     for (i=1; i<=80;i++){

gotoxy(i,j); /* COLUMN, ROW */

printf(" ");

delay(2);

      }

    }

    /* ------------------------------ */

  /* Message about Vertical Clear */

  /* ------------------------------ */

     /* Fill the screen with @ symbol */

     for (i=1; i<=80;i++){

      for (j=1; j<=25; j++){

printf("@");

      }

    }

     gotoxy(20,13);

     textcolor(9);

     cprintf("Custom CLRSCR using Vertical !");

    /* Perform custom clrscr() by clearing in Vertical */

    for (i=1; i<=80;i++){

    for (j=1; j<=25; j++){

gotoxy(i,j); /* COLUMN, ROW */

printf(" ");

delay(2);

      }

    }

   getch();

}






Monday, July 1, 2019

Program to verify a given number is ODD or EVEN using C Program

Program to validate a given number.  In case the number is divisible by 2 without any remainders then the number is EVEN number.  Else the number is ODD number.

#include <stdio.h>
int main()
{
    int realnumber;
    printf("Enter a real numer: ");
    scanf("%d", &realnumber);
   
    // Below if condition return TRUE in case the number is perfectly divisible by 2
    // Else it return FALSE
    if(realnumber % 2 == 0)
        printf("%d is even.", realnumber);
    else
        printf("%d is odd.", realnumber);
    return 0;
}

Output:

Enter a real numer: 10                                                                                                       
10 is even. 

Enter a real numer: 5                                                                                                       
5 is odd. 





Factorial program using C Language

Factorial is a mathematical way of multiplying the numbers.

4! => 4 x 3 x 2 x 1 => 24
10 ! => 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 => 3628800


#include<stdio.h>
int main() 

int i,factorial=1,input; 
printf("Enter a number to Find Factorial?"); 
scanf("%d",&input); 
        for(i=1; i<=input; i++){ 
factorial=factorial*i; 


printf("Factorial of %d is: %d",input,factorial); 
return 0;
}

Output:

Enter a number to Find Factorial?10                                                                                         
Factorial of 10 is: 3628800

Enter a number to Find Factorial?5                                                                                         
Factorial of 5 is: 120

Program to swap two numbers without using third variable using C

Program to swap two numbers without using third variable:

#include <stdio.h>
int main()   
{   
    int number1=15, number2=25;     
    printf("Intially number1=%d number2=%d",number1,number2);     
    number1=number1+number2;//number1=40 (15+25)   
    number2=number1-number2;//number2=15 (40-25)   
    number1=number1-number2;//number1=25 (40-15)   
    printf("\nAfter swap number1=%d number2=%d",number1,number2);   
    return 0; 
}

Output:
Intially number1=15 number2=25                                                                                               
After swap number1=25 number2=15






Popular Posts