In this section we are going to provide The following program computes a table of the sine function for angles between 0 and 360 degrees Example.
Previous | Home | Next |
Let's Compute the Program
The following program computes a table of the sine function for angles between 0 and 360 degrees.
#include < stdio.h> #include < math.h> void main() { int angle_degree; double angle_radian, pi, value; // Print a message printf ("\nCompute a table of the sine function\n\n"); // obtain pi pi = 4.0*atan(1.0); printf ( " Value of PI = %f \n\n", pi ); printf ( " angle Sine \n" ); angle_degree=0; // initial angle value // scan over angle while ( angle_degree <= 360 ) // loop until angle_degree > 360 { angle_radian = pi * angle_degree/180.0; value = sin(angle_radian); printf ( " %3d %f \n ", angle_degree, value ); angle_degree = angle_degree + 10; // increment the loop index} }
We will learn basic calculations and we learn how we can use deferent methods of deferent library header files in this example .Basically we are calculating the sine values from 0 to 360.Before exampling this example first you must know types of different variable.
C uses the following standard variable types:
int -> integer variable short -> short integer long -> long integer float -> single precision real (floating point) variable double -> double precision real (floating point) variable char -> character variable (single byte)
The compilers checks for consistency in the types of all variables used in any code. This feature is intended to prevent mistakes, in particular in mistyping variable names. Calculations done in the math library routines are usually done in double precision arithmetic (64 bits on most workstations). The actual number of bytes used in the internal storage of these data types depends on the machine being used.
The printf function can be instructed to print integers, floats and strings properly. The general syntax is:
printf( "format", variables );
where "format" specifies the conversation specification and variables is a list of quantities to print. Some useful formats are:
%.nd integer (optional n = number of columns; if 0, pad with zeroes) %m.nf float or double (optional m = number of columns,n = number of decimal places) %ns string (optional n = number of columns) %c character \n \t to introduce new line or tab \g ring the bell (``beep'' ) on the terminal.
Example descriptions
Include header files:
#include < stdio.h> #include < math.h>
These two header files must be included first.
first is included for printf()and main() methods as these methods are defined in this header file.
Second <math.h> header file is included for sin function. In <math.h> header file all mathematical methods eg sin(),cose(),sqrt() etc. are define to use them we need to include this header file at top most in our progarm.
We are using while loop for printing and calculating value of sin from 0 to 360.
Previous | Home | Next |