 # C Programming Code Examples

## C > Mathematics Code Examples

### C Program to Add two Complex Numbers

/* C Program to Add two Complex Numbers This C Program adds two complex numbers. A complex number is a number that can be put in the form a + bi, where a and b are real numbers and i is called the imaginary unit, where i2 = -1. In this expression, a is called the real part and b the imaginary part of the complex number. */ #include <stdio.h> struct complex { int realpart, imaginary; }; main() { struct complex a, b, c; printf("Enter value of a and b complex number a + ib.\n"); printf("value of complex number a is = "); scanf("%d", &a.realpart); printf("value of complex number b is = "); scanf("%d", &a.imaginary); printf("Enter value of c and d complex number c + id.\n"); printf("value of complex number c is = "); scanf("%d", &b.realpart); printf("value of complex number d is = "); scanf("%d", &b.imaginary); c.realpart = a.realpart + b.realpart; c.imaginary = a.imaginary + b.imaginary; if (c.imaginary >= 0) printf("complex numbers sum is = %d + %di\n", c.realpart, c.imaginary); else printf("complex numbers sum = %d %di\n", c.realpart, c.imaginary); return 0; }

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,

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.

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.

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.

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

#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 programming code to read elements in an array and find frequency of each element in an array. C Program to count the occurrence of each element in an array. Logic to count...

Print help and exit with exval. Print version and exit with exval. Output some additional info. Very basic test of ip4 dotted quad address format. Convert address to base 10

C Language program to Amstrong Numbers. Armstrong Number is a number which equal to the sum of the cubes of its individual digits For example, 407 is an armstrong number as

C program to read elements in a matrix and find the sum of minor diagonal elements. C program to calculate sum of minor diagonal elements. Input elements in matrix from user

The annual examination is conducted for 50 students for three subjects. Write a program to read the data and determine the following: Total marks obtained by each student. The...

In C program user is asked to enter the binary number and the program then converts that binary number to the octal number by calling a user defined function. To understand this...