Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > For Loops and While Loops Code Examples

program to convert Hexadecimal to Binary number system

/* program to convert Hexadecimal to Binary number system Write a C program to input hexadecimal number from user and convert to binary number system. How to convert hexadecimal number system to binary number system in C program. Hexadecimal number system Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all numbers i.e. 0123456789ABCDEF Binary number system Binary number system is a base 2 number system. It uses 2 symbols to represent all numbers i.e. 0 and 1. Hexadecimal to binary conversion is divided in three steps. Extract each hex digits separately. Find the binary of each extracted hex digit. Store the binary equivalent of extracted hexadecimal number to final bin variable. Repeat the above three steps till all hexadecimal digits are processed. Hexadecimal to binary conversion table Decimal Binary Hexadecimal 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F */ #include <stdio.h> #include <string.h> int main() { char hex[17], bin[65] = ""; int j = 0; /* Input hexadecimal number from user */ printf("Enter any hexadecimal number: "); gets(hex); /* Extract first digit and find binary of each hex digit */ for(j=0; hex[j]!='\0'; j++) { switch(hex[j]) { case '0': strcat(bin, "0000"); break; case '1': strcat(bin, "0001"); break; case '2': strcat(bin, "0010"); break; case '3': strcat(bin, "0011"); break; case '4': strcat(bin, "0100"); break; case '5': strcat(bin, "0101"); break; case '6': strcat(bin, "0110"); break; case '7': strcat(bin, "0111"); break; case '8': strcat(bin, "1000"); break; case '9': strcat(bin, "1001"); break; case 'a': case 'A': strcat(bin, "1010"); break; case 'b': case 'B': strcat(bin, "1011"); break; case 'c': case 'C': strcat(bin, "1100"); break; case 'd': case 'D': strcat(bin, "1101"); break; case 'e': case 'E': strcat(bin, "1110"); break; case 'f': case 'F': strcat(bin, "1111"); break; default: printf("Invalid hexadecimal input."); } } printf("Hexademial number = %s\n", hex); printf("Binary number = %s", bin); return 0; }

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.

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,

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).

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.

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

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.

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

Concatenate strings. Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination. Destination and source shall not overlap.






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


C program find Perfect numbers between 1 to n. Input Upper Limit to find Perfect numbers. Store it in a variable say end. Run a loop from 1 to end, increment 1 in each iteration. The...

Program Input number and nth bit position to clear from user. Store it in some variable say j and n. Left shift 1, n times i.e. 1 << n. Perform bitwise complement with the above result...

In this c program code, a structure Distance is defined. The structure has two members inch (a float) and feet (an integer). Two variables (distance1 and distance2) are created which...

C language program take the letter as input and store it in the variable grade. Convert the input letter into its uppercase using function toupper(). Using switch statement, verify the