Tuesday, 10 January 2012

Create Graphical Loading Bar in C



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

void main(void)
{
int driver=DETECT, mode;


        int i;

         initgraph(&driver,&mode,"c:\\tc\\bgi");   //initialize graphics mode
textcolor(BLACK);      //background color

        moveto(200,180);        //loading process
outtext("Loading, please wait...");
   for(i=0;i<600;i+=15)
   {
    setcolor(3);            //loading bar color
    line(20,380,i+39,380);
    delay(150);         //slow down loop speed
   }
setcolor(WHITE);
moveto(210,200);
outtext("Loading Completed!");   //loading process complete
delay(1000);
       closegraph();               //close graphics mode


}




If you have any problem with the above code, you can contact me...

Wednesday, 21 December 2011

What are Pointers?

A pointer provides a way of accessing a variable without referring to the variable directly.
The mechanism used for this is the address of variable. The address act as an intermediate between the variable  and the program accessing it.
Let's have a look at the following example:


#include<stdio.h>
#include<conio.h>
void main()
{
   int *p, q;   //pointer variable always initialize with an *
     q = 100;   //assigning value to q 
     p = &q;    //inserting the address of q into p
       printf("%d",*p);  //see the description below
   getch();     //exit on key press                                                         
}

In the above program, the value of q (i.e. 100) is shown through the pointer variable p (i.e. *p). You can easily see in the picture above. First, store 100 the q variable and then the address of q (i.e. 1312) is given to pointer variable p and at last the value of q is print through pointer variable (*p).

If you still have any problem or confusion, you can contact me... 

Saturday, 17 December 2011

How to display a colorful animated string/phrase


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>      // for delay() function


void main(void)
{
    int driver=DETECT, mode;
    int i;    //for loop
    initgraph(&driver, &mode, "c:\\TC\\bgi" );
    textcolor(BLACK);
    for(i=0;i<=400;i++)
    {
    clrscr();
    setcolor(i/15);     //set text/string color
    outtextxy(20+i,150,"**C Language Codes**");  //see the description below
    delay(30);       //slow down the loop
    }
    getch();
    closegraph();
}


What is outtextxy(); ?


It is a built-in function defined in the header file "graphics.h". This function used to display graphical string. The general syntax is as follows:
outtextxy(x-coordinate, y-coordinate, "String to display"); 


In the above program, outtextxy() function is put into for loop to make the text animated. As you can see that variable i is written with the x-coordinate whereas, the y-coordinate remains constant which means the text will move towards left to right on the screen.


If you still have any question, you can ask me freely... 



Draw a colorful Circle


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

void main(void)
{
    int driver=DETECT, mode;
    int xc1=320, yc1=240;


       initgraph(&driver, &mode, "c:\\TC\\bgi" );
         setcolor(7);                     //edge color
         setfillstyle(SOLID_FILL,GREEN);  // fill inside the circle
         circle(xc1,yc1,100);             // create a circle
         floodfill(xc1,yc1,7);            // flood color
     getch();
    closegraph();
}


Output:
















If you have any question, you can ask me freely...

Friday, 18 November 2011

How to Initialize Graphics mode in C language

/*Header Files*/
#include<stdio.h>                  //for input/output purpose
#include<conio.h>                 //for getch() function
#include<graphics.h>            //for graphics functions
void main()
{
         int driver=DETECT, mode;                               //auto detect the driver
         int x=320, y=240, radius=100;                            // initialization
           initgraph(&driver , &mode , "c:\\tc\\bgi");  // all necessary graphics files in BGI folder
                  circle(x , y, radius);                                            //create a circle
         getch();                                                          // waits for key press
         closgraph();                                                    //close graphics mode
}

The above program will give the following output:


Wednesday, 17 August 2011

Program to identify the prime number


Code:
#include<stdio.h>
#include<conio.h>
void main()
{
      int num,flag=0,a;
            printf("Enter an integer: ");
            scanf("%d",&num);
      //Logic for prime number
            if(num==2)
                  printf("%d is Prime Number.",num); 
            else
            {
                  for(a=2;a<num;a++)
                  {
                        if(num%a==0)
                              flag=1;
                  }
            }
            if(flag==1)
                  printf("%d is not a Prime Number.",num);
            else
                  printf("%d is Prime Number.",num);
      getch();

}

Result:

Enter an integer: 8
8 is not a Prime Number.

If you have any confusion about that program, contact me freely :)


Interchanging two variables without using the third one


Using less variables is a sign of good programming. Here's the example program to interchanging the values of two variables without using third variable.

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
//Initialization
  int var1,var2; 
    printf("Enter two integers\n");
//Taking input from user
      printf("a = ");
      scanf("%d",&var1);
      printf("b = ");
      scanf("%d",&var2);
   var1=var1+var2;
   var2=var1-var2;
   var1=var1-var2;
    printf("a = %d\n",var1);
    printf("b = %d\n",var2);
  getch();

}

Result:

Enter two integers
a = 5
b = 11
a = 11
b = 5