# C Programming Code Examples

## C > Mathematics Code Examples

### Program to Calculate the Value of sin(x)

/* Program to Calculate the Value of sin(x) This C Program Calculates the Value of sin(x). It's a non-differentiable function. Start at zero, then goes up to 1, then back down to 0. But then, instead of going negative, it will just "reflect" about the x-axis. The derivative is 1 and then -1 for every x such that sin(x) = 0 (i.e. 0, 180, 360, 540, 720 ...). - C program to find the value of sin(x) using the series - up to the given accuracy (without using user defined function) - also print sin(x) using library function. */ #include <stdio.h> #include <math.h> #include <stdlib.h> void main() { int j, x1; float accuracy, term, denominator, x, sinx, sinval; printf("Enter the value of x (in degrees) \n"); scanf("%f", &x); x1 = x; /* Converting degrees to radians */ x = x * (3.142 / 180.0); sinval = sin(x); printf("Enter the accuracy for the result \n"); scanf("%f", &accuracy); term = x; sinx = term; j = 1; do { denominator = 2 * j * (2 * j + 1); term = -term * x * x / denominator; sinx = sinx + term; j = j + 1; } while (accuracy <= fabs(sinval - sinx)); printf("Sum of the sine series = %f \n", sinx); printf("Using Library function sin(%d) = %f\n", x1, sin(x)); }

Absolute value. The abs () function is a predefined function in the stdlib.h header file to return the absolute value of the given integers. So, if we want to return the absolute value of a given number, we need to implement the stdlib.h header file in the C program. The abs() function only returns the positive numbers.

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming. Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand. The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.

Compute absolute value. Returns the absolute value of x: |x|. The fabs() function returns the absolute value of its floating-point argument x; if x is greater than or equal to 0, the return value is equal to x. If x is less than 0, the function returns -x. x - the value to convert to an absolute value.

Compute sine. Returns the sine of an angle of x radians. The C sin Function is a C Math Library Function, used to calculate the Trigonometry Sine value for the specified expression.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

C program to enter elements in two matrices and check whether both matrices are equal or not. C code to check whether elements of two matrices are equal or not. Input elements

C Program to Input month number and Print Total Number of Days in Month using switch case. Input month number from user. Store it in some variable say month. Switch the value

C program code ask the user for operation like insert, delete, display and exit. According to the option entered, access its respective function using switch statement and use the

C program input number and check number is Palindrome or not using Loop. Palindrome number is such number which when reversed is equal to the original number. 121, 12321,...