 # C Programming Code Examples

## C > Mathematics Code Examples

### C Program to Calculate the Mean, Variance & Standard Deviation

/* C Program to Calculate the Mean, Variance & Standard Deviation This C Program calculates the mean, variance & standard deviation. The formula which is used in this program are mean = average of the numbers. variance = (summation( ( Xj - average of numbers) * ( Xj - average of numbers)) ) / Total no of elements. where j = 1 to N here N is the total no of elements. Standard deviation = Squareroot of the variance. */ #include <stdio.h> #include <math.h> #define maxsize 10 void main() { float x[maxsize]; int j, n; float average, variance, std_deviation, sum = 0, sum1 = 0; printf("Enter the value of N \n"); scanf("%d", &n); printf("Enter %d real numbers \n", n); for (j = 0; j < n; j++) { scanf("%f", &x[j]); } /* Compute the sum of all elements */ for (j = 0; j < n; j++) { sum = sum + x[j]; } average = sum / (float)n; /* Compute variance and standard deviation */ for (j = 0; j < n; j++) { sum1 = sum1 + pow((x[j] - average), 2); } variance = sum1 / (float)n; std_deviation = sqrt(variance); printf("Average of all elements = %.2f\n", average); printf("variance of all elements = %.2f\n", variance); printf("Standard deviation = %.2f\n", std_deviation); }

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

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.

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.

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:

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.

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.

Compute square root. Returns the square root of x. The sqrt() function takes a single argument (in double) and returns its square root (also in double). The sqrt() function is defined in math.h header file. To find the square root of int, float or long double data types, you can explicitly convert the type to double using cast operator. You can also use the sqrtf() function to work specifically with float and sqrtl() to work with long double type.

Raise to power. Returns base raised to the power exponent: baseexponent. The function pow() is used to calculate the power raised to the base value. It takes two arguments. It returns the power raised to the base value. It is declared in "math.h" header file.

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.

Convert decimail nos to roman equivalent upto 10,000. Function for generating roman equivalent of 1000's. Function for generating roman eq of 100's. Function for generating...

1. It is initializing two variables. Note: both are separated by comma (,). 2. It has two test conditions joined together using AND (&&) logical operator. You cannot use multiple test

C Program code input any number from user and find cube of the a number using function. Lets assign meaningful name to the function, say cube(). Function should accept a number

Code add a lnode at the beginning of the list. Reverse the whole list. Display the whole linked list. Insert some numbers. Add a lnode at the beginning of the list. Add new node.

Find Sum of Array elements Using Recursive function in C language. Function declaration to find sum of array. Input size and elements in array. Recursively find the sum of elements