# C Programming Code Examples

## C > For Loops and While Loops Code Examples

### C program to convert Hexadecimal to Decimal number system

/* C program to convert Hexadecimal to Decimal number system Write a C program to input hexadecimal number from user and convert it to Decimal number system. How to convert from Hexadecimal number system to Decimal number system in C programming. Hexadecimal number system Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all number i.e. 0123456789ABCDEF Decimal number system Decimal number system is a base 10 number system. It uses 10 symbols to represent all numbers i.e. 0123456789 Input a hexadecimal number from user. Store it in some variable hex. Initialize decimal = 0, digit = length_of_hexadecimal_digit - 1 and j = 0. Run a loop for each hex digit. Which is the loop structure should look like for(j=0; hex[j]!='\0'; j++). Inside the loop find the integer value of hex[j]. Store it in some variable say val. Convert the hex to decimal using decimal = decimal + (val * 16 ^ digit). Where val = hex[j]. */ #include <stdio.h> #include <math.h> #include <string.h> int main() { char hex[17]; long long decimal, place; int j = 0, val, len; decimal = 0; place = 1; /* Input hexadecimal number from user */ printf("Enter any hexadecimal number: "); gets(hex); /* Find the length of total number of hex digit */ len = strlen(hex); len--; /* Iterate over each hex digit */ for(j=0; hex[j]!='\0'; j++) { /* Find the decimal representation of hex[j] */ if(hex[j]>='0' && hex[j]<='9') { val = hex[j] - 48; } else if(hex[j]>='a' && hex[j]<='f') { val = hex[j] - 97 + 10; } else if(hex[j]>='A' && hex[j]<='F') { val = hex[j] - 65 + 10; } decimal += val * pow(16, len); len--; } printf("Hexadecimal number = %s\n", hex); printf("Decimal number = %lld", decimal); return 0; }

Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language: Simple assignment operator. Assigns values from right side operands to left side operand. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

#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:

Relational Operators are the operators used to create a relationship and compare the values of two operands. For example, there are two numbers, 5 and 15, and we can get the greatest number using the greater than operator (>) that returns 15 as the greatest or larger number to the 5. Following are the various types of relational operators in C. Equal To Operator (==) is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal. Not Equal To Operator (!=) is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0.

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.

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.

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.

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.

Get string length. Returns the length of the C string str. The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

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,

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Get string from stdin. Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached. The newline character, if found, is not copied into str. A terminating null character is automatically appended after the characters copied to str. Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

C Program code calculates the largest of two numbers in a given Array. First it accepts an array, then compares the elements and finds which is the largest two element in a given...

The above logic is simple & easy to code. But it's lengthy and not optimal to implement. In the above solution we are performing same task for multiple conditions i.e. print 31 days

Put on the motor. Address of digital control port. Check whether the FDC is ready. Read the status of MSR. Check whether FDC is ready. Beginning of command phase. Input

C language program take the number of days as input. For the number of years, divide the input by 365 and obtain its quotient. For the number of weeks, divide the input by 365 and

Using iteration, finds the Magic Square for a given odd sized number. A magic square is an arrangement of numbers from 1 to N^2 in an [N x N] matrix, with each number occurring...