C Programming Code Examples
C > For Loops and While Loops Code Examples
convert Decimal to Hexadecimal number system
/* convert Decimal to Hexadecimal number system
Write a C program to input decimal number from user and convert to Hexadecimal number system. How to convert Decimal to Hexadecimal number system in C programming.
Decimal number system
Decimal number system is a base 10 number system. Decimal number system uses 10 symbols to represent all number i.e. 0123456789
Hexadecimal number system
Hexadecimal number system is a base 16 number system. Hexadecimal number system uses 16 symbols to represent all numbers i.e. 0123456789ABCDEF
Algorithm to convert decimal to hexadecimal number system */
Algorithm Conversion from Decimal to Hexadecimal
begin:
read (decimal);
hex = NULL; rem = 0;
HEXVALUES[] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F;
While(decimal != 0)
begin:
rem = decimal % 16;
hex = hex + HEXVALUES[rem];
decimal = decimal / 16;
end;
Reverse(hex);
print('Hexadecimal = ' hex);
end;
#include <stdio.h>
#include <string.h>
int main()
{
char HEXVALUE[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
long long decimal, tempDecimal;
char hex[65];
int index, rem;
/* Input decimal number from user */
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
index = 0;
/* Decimal to hexadecimal conversion */
while(tempDecimal !=0)
{
rem = tempDecimal % 16;
hex[index] = HEXVALUE[rem];
tempDecimal /= 16;
index++;
}
hex[index] = '\0';
strrev(hex);
printf("\nDecimal number = %lld\n", decimal);
printf("Hexadecimal number = %s", hex);
return 0;
}
The strrev(string) function returns reverse of the given string. An array of characters is called a string. This function is used for reversing a string. The reversed string is stored in the same string. strrev() is a non-standard C library function, sometimes found in <string.h> strrev() is one of the inbuilt string function in c programming which is used to reverse a given string.
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.
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.
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.
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.
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.
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.
#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:
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,
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read. If the file offset is at or past the end of file, no bytes are read, and read() returns zero. If count is zero, read() may detect the errors described below. In the absence of any errors, or if read() does not check for errors, a read() with a count of 0 returns zero and has no other effects. According to POSIX.1, if count is greater than SSIZE_MAX, the result is implementation-defined; see NOTES for the upper limit on Linux.
C program code input radius of a circle from user and find diameter, circumference and area of the circle. How to calculate diameter, circumference and area of a circle whose...
C Program to find Length of string with using user 'Defined function'. By using user-defined function write a code to find Length of String. After accepting the string from the user we're
Program code input binary number from user & convert binary number to decimal number system. The Binary Number system is a base 2 number system and uses only 2 symbols to