Right Place For Right PersonTM 
Sponsored Ads
Home Tutorials Articles Forums Source Code Books Certifications Interviews Questions

C Tutorial

2. Let's Compute
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:
1.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.


More>>>                                                                     Previous>>
Contact Us

Interview Questions And Answers
Struts interview questions and answers (Subjective)
500 Java Objective Questions and Answer
Core Java Objective Questions And Answers
Core Java Subjective Questions And Answers
Core Java Interview Questions And Answers
Core Java Interview Questions and Answers (Subjective)
Core Java Interview Questions and Answers( Objective)
50 Servlet interview questions
155 Java Interview Questions
EJB Interview Questions and Answers(Subjective)
R4R,JSP Interview Questions and Answer(Subjective)
R4R,Java Servlets Interview Questions and Answers(Subjective)
Core Java Subjective ,Objective and Interview Questions And Answers
275 Core java interview questions
Java Objective Questions and Answer
Java Architect Interview Questions
Applet Interview Questions and Answers
Core Java example
Servlet Objective Questions And Answers
Servlet Subjective Questions And Answers
Servlet Interview Questions And Answers
JSP Objective Questions And Answers
JSP Subjective Questions And Answers
JSP Interview Questions And Answers

Contact US:

Your Name:


Your Email:

Message:

Comments:

Give Your Comments:


Advertiser PRIVACY POLICY ||User PRIVACY POLICY || R4R Group Srvices