Showing posts with label Mouse. Show all posts
Showing posts with label Mouse. Show all posts

Saturday, April 6, 2013

cos in c - math.h

Cos function returns cosine of an angle(in radian).
1 radian = 57.2958(approximately).
Declaration: double cos(double);

C programming code

#include <stdio.h>
#include <math.h>
 
int main()
{
  double result, x = 1.0471;
 
  result = cos(x);
 
  printf("cos(%.4lf) = %.2lf\n", x, result);
 
  return 0;
}
Compiler used: 
GCC
Output of program: 
cos

log10

log10 function returns common logarithm (base is 10) of a number.
Declaration :- double log10(double number);

C programming code for log10

#include <stdio.h>
#include <math.h>
 
int main()
{
  double n, result;
 
  printf("Enter a number to calculate it's log(base is 10)\n");
  scanf("%lf", &n);
 
  result = log10(n);
 
  printf("Common log of %.2lf = %.2lf\n", n, result);
 
  return 0;
}
Compiler used: 
GCC
Output of program:

sin function in c

Sin function returns sine of an angle(in radian).
Declaration: double sin(double);

C programming code

#include <stdio.h>
#include <math.h>
 
int main()
{
  double result, x = M_PI/6;
 
  result = sin(x);
 
  printf("The sin(%lf) = %.2lf\n", x, result);
 
  return 0;
}
Compiler used: 
GCC
Output of program:

textbackground in c

textbackground function is used to change of current background color in text mode. See available colors.
Declaration : void textbackground(int color);

C programming code for textbackground

#include<stdio.h>
#include<conio.h>
 
main()
{
   textbackground(RED);
 
   cprintf("C program to change background color.");
 
   getch();
   return 0;
}
Output: 
background color

textcolor in c

textcolor function is used to change the color of drawing text in c programs.
Declaration :- void textcolor(int color);
where color is an integer variable. For example 0 means BLACK color, 1 means BLUE, 2 means GREEN and soon. You can also use write appropriate color instead of integer. For example you can write textcolor(YELLOW); to change text color to YELLOW. But use colors in capital letters only.

C programming code to change text color

#include<stdio.h>
#include<conio.h>
 
main()
{
   textcolor(RED);
   cprintf("C programming");
 
   getch();
   return 0;
}

C programming code for blinking text

#include<stdio.h>
#include<conio.h>
 
main()
{
   textcolor(MAGENTA+BLINK);
   cprintf("C programming");
 
   getch();
   return 0;
}
Note that we have used cprintf function instead of printf. This is because cprintf send formatted output to text window on screen and printf sends it to stdin.

wherex in c

wherex function return current horizontal cursor position.
Declaration :- int wherex();

C programming code for wherex

#include<stdio.h>
#include<conio.h>
 
main()
{
   int x;
 
   printf("Hello");
 
   x = wherex();
 
   printf("Horizontal cursor position from where this text appears = %d\n",x);
 
   getch();
   return 0;
}

kbhit in c

kbhit in c: kbhit function is used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file "conio.h". If a key has been pressed then it returns a non zero value otherwise returns zero.
Declaration : int kbhit();

C programming code for kbhit

#include <stdio.h>
#include <conio.h>
 
main()
{
   while (!kbhit())
      printf("You haven't pressed a key.\n");
 
   return 0;
}
As long as in the above program user doesn't presses a key kbhit() return zero and (!0) i.e. 1 the condition in while loop is true and "You haven't pressed a key." will be printed again and again. As a key is pressed from the keyboard the condition in while loop become false as now kbhit() will return a non-zero value and ( !(non-zero) = 0), so the control will come out of the while loop.

gotoxy in c

gotoxy in c: gotoxy function places cursor at a desired location on screen i.e. we can change cursor position using gotoxy function.
Declaration : void gotoxy( int x, int y);
where (x, y) is the position where we want to place the cursor.

C programming code for gotoxy

#include<stdio.h>
#include<conio.h>
 
main()
{
   int x, y;
 
   x = 10;
   y = 10;
 
   gotoxy(x, y);
 
   printf("C program to change cursor position.");
 
   getch();
   return 0;
}
Output:
gotoxy function code output
AttachmentSize
gotoxy.png4.17 KB

getche in c

getche function prompts the user to press a character and that character is printed on screen.

C code for getche

#include<stdio.h>
#include<conio.h>
 
main()
{
   printf("Waiting for a character to be pressed from the keyboard to exit.");
 
   getche();
   return 0;
}
Run this program and press a character. Then view the user screen (Alt+F5) if using turbo c. You will find the character printed on the screen if you pressed a printable character. Try pressing enter or tab key (non printable) characters also.

getch in c

getch in c language: getch function prompts the user to press a character and that character is not printed on screen, getch header file is conio.h.

C programming code for getch

/* getch in c example */
#include<stdio.h>
#include<conio.h>
 
main()
{
   printf("Waiting for a character to be pressed from the keyboard to exit.\n");
 
   getch();
   return 0;
}
When you will run this program, the program will exit only when you press a character, note that we are talking about a character so try pressing numlock, shift key etc (program will not exit if you press these keys) as these are not characters. Also try the above program by removing getch(), in this case program will exit without waiting for a character being pressed from keyboard.

How to use getch in c++

#include<iostream.h>
#include<conio.h>
 
main()
{
   cout << "Enter a character";
   getch();
}

getch in Dev C++ compiler

getch library function works in dev c++ compiler, also note that it doesn't support all functions of conio.h as turbo c does.

getchar in c

#include<stdio.h>
 
main()
{
   int c;
 
   c = getchar();
 
   putchar(c);
 
   return 0;
}
Common use of getch is that you can view the output (if any) of your program without having to open the output window if you are using turbo c compiler or if you are not running your program from command prompt.

delline

delline function deletes the line containing the cursor and move all lines below it one line up.

C programming code for delline

#include<stdio.h>
#include<conio.h>
 
main()
{
   printf("This line will be deleted when you press a key.");
 
   getch();
   delline();
 
   printf("Line deleted successfully.");
 
   getch();
   return 0;
}

clrscr in c

clrscr function clears the screen amd move the cursor to upper left hand corner of screen.

C programming code for clrscr

#include<stdio.h>
#include<conio.h>
 
main()
{
   printf("Press any key to clear the screen.\n");
 
   getch();
 
   clrscr();
 
   printf("This appears after clearing the screen.\n");
   printf("Press any key to exit...\n");
 
   getch();
   return 0;
}
In the above program first we display the message "Press any key to clear the screen." using printf and then ask the user to press a key. When user will press a key screen will be cleared and another message will be printed. clrscr function does not work in Dev C++ compiler. Also do not use clrscr in graphics mode instead use cleardevice.

sound c

Sound function produces the sound of a specified frequency. Used for adding music to c program, try to use some random values in loop, vary delay and enjoy.
Declaration:- void sound(unsigned frequency);

C programming code for sound

#include<dos.h>
 
main()
{
   int a;
 
   for ( a = 200 ; a <= 1000 ; a = a + 20 )
   {
      sound(a);
      delay(25);
   }
   nosound();
 
   return 0;
}

sleep c

Sleep function delays program execution for a given number of seconds.
Declaration: void sleep(unsigned seconds);

C programming code for sleep

#include<stdio.h>
#include<dos.h>
 
main()
{
   printf("Wait for 5 seconds to exit.\n");
   sleep(5);
   return 0;
}

nosound c

nosound function turn off the PC speaker.
Declaration : void nosound();

C programming code for nosound

#include<dos.h>
 
main()
{
   sound(400);
   delay(1000);
   nosound();
 
   return 0;
}

gettime c

gettime in c: gettime function is used to find current system time. We pass address of a structure varibale of type ( struct time ).

C programming code for gettime

#include<stdio.h>
#include<dos.h>
 
main()
{
   struct time t;
 
   gettime(&t);
 
   printf("Current system time is %d : %d : %d\n",t.ti_hour,t.ti_min,t.ti_sec);
 
   return 0;
}
Output: 
system time

getdate c

Program to print the current system date, getdate c code below explain how to use this function to print computer date.

Getdate example

C programming code to print date
#include<stdio.h>
#include<dos.h>
 
main()
{
   struct date d;
 
   getdate(&d);
 
   printf("Current system date is %d/%d/%d\n",d.da_day,d.da_mon,d.da_year);
 
   return 0;
}

c program countdown

This c graphics program performs countdown for 30 seconds.

C program countdown code

#include<graphics.h>
#include<dos.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm, i;
   char a[5];
 
   initgraph( &gd, &gm, "C:\\TC\\BGI");
 
   settextjustify( CENTER_TEXT, CENTER_TEXT );
   settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
   setcolor(RED);
 
   for(i = 30 ; i >=0 ; i--)
   {
      sprintf(a,"%d",i);
      outtextxy(getmaxx()/2, getmaxy()/2, a);
      delay(1000);
 
      if ( i == 0 )
         break;
      cleardevice();
   }
 
   getch();
   closegraph();
   return 0;
}

c program to draw circles in circles

This program draws circles in circles in two different colors.

C programming code

#include<graphics.h>
#include<conio.h>
#include<dos.h>
 
main()
{
   int gd = DETECT, gm, x, y, color, angle = 0;
   struct arccoordstype a, b;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
   delay(2000);
 
   while(angle<=360)
   {
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
      setcolor(RED);
      getarccoords(&a);
      circle(a.xstart,a.ystart,25);
      setcolor(BLACK);
      arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
      getarccoords(&a);
      setcolor(GREEN);
      circle(a.xstart,a.ystart,25);
      angle = angle+5;
      delay(50);
   }
 
   getch();
   closegraph();
   return 0;
}
Output of program:
circles in circles
AttachmentSize
circles in circles.png18.11 KB

c smiling face animation

This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily.

C programming code

#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
 
main()
{
   int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
   void *p;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   setcolor(YELLOW);
   circle(50,100,25);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(50,100,YELLOW);
 
   setcolor(BLACK);
   setfillstyle(SOLID_FILL,BLACK);
   fillellipse(44,85,2,6);
   fillellipse(56,85,2,6);
 
   ellipse(50,100,205,335,20,9);
   ellipse(50,100,205,335,20,10);
   ellipse(50,100,205,335,20,11);
 
   area = imagesize(left, top, left + 50, top + 50);
   p = malloc(area);
 
   setcolor(WHITE);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   outtextxy(155,451,"Smiling Face Animation");
 
   setcolor(BLUE);
   rectangle(0,0,639,449);
 
   while(!kbhit())
   {
      temp1 = 1 + random ( 588 );
      temp2 = 1 + random ( 380 );
 
      getimage(left, top, left + 50, top + 50, p);
      putimage(left, top, p, XOR_PUT);
      putimage(temp1 , temp2, p, XOR_PUT);
      delay(100);
      left = temp1;
      top = temp2;
   }
 
   getch();
   closegraph();
   return 0;
}
Output of program:
smiling face
AttachmentSize
smiling face.png2.79 KB