Showing posts with label DOS. Show all posts
Showing posts with label DOS. 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

sqrt function

sqrt function returns square root of a number.
Declaration :- double sqrt(double);

C programming code for sqrt

#include <stdio.h>
#include <math.h>
 
int main()
{
 
  double n, result;
 
  printf("Enter a number to calculate it's square root\n");
  scanf("%lf", &n);
 
  result = sqrt(n);
 
  printf("Square root of %.2lf = %.2lf\n", n, result);
 
  return 0;
}
Compiler used: 
GCC
AttachmentSize
sqrt.png4.53 KB

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:

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

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

setdate c

setdate function is used to change system date.

C programming code for setdate

#include<stdio.h>
#include<conio.h>
#include<dos.h>
 
main()
{
   struct date d;
 
   printf("Enter the new date ( day, month and year ) as integers ");
   scanf("%d%d%d",&d.da_day,&d.da_mon,&d.da_year);
 
   setdate(&d);
 
   printf("Current system date is %d/%d/%d\n",d.da_day,d.da_mon,d.da_year);
 
   getch();
   return 0;
}
Output: 
change system date

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