C Programming Code Examples
C > For Loops and While Loops Code Examples
C program to convert Decimal to Binary number system
/* C program to convert Decimal to Binary number system
Write a C program to input decimal number from user and convert to binary number system. How to convert from decimal number to binary number system in C program.
Decimal number system
Decimal number system is a base 10 number system. Decimal number system uses only 10 symbols to represent all number i.e. 0123456789
Binary number system
Binary number system is a base 2 number system. Binary number system uses only 2 symbols to represent all numbers i.e. 0 and 1
Algorithm to convert from decimal to binary
Algorithm Decimal to Binary conversion
begin:
read (decimal);
binary = 0; place = 1; rem = 0;
while (decimal > 0) do
begin
rem = decimal % 2;
binary = (rem * place) + binary;
place = place * 10;
decimal = decimal / 2;
end
write('Binary = ' binary)
end */
#include <stdio.h>
int main()
{
long long decimal, tempDecimal, binary;
int rem, place = 1;
binary = 0;
/* Input decimal number from user */
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
/* Decimal to binary conversion */
while(tempDecimal > 0)
{
rem = tempDecimal % 2;
binary = (rem * place) + binary;
tempDecimal /= 2;
place *= 10;
}
printf("Decimal number = %lld\n", decimal);
printf("Binary number = %lld", binary);
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.
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.
Write to a file descriptor. write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered, or the call was interrupted by a signal handler after having written less than count bytes. For a seekable file (i.e., one to which lseek may be applied, for example, a regular file) writing takes place at the current file offset, and the file offset is incremented by the number of bytes actually written. If the file was opened with O_APPEND, the file offset is first set to the end of the file before writing. The adjustment of the file offset and the write operation are performed as an atomic step.
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.
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.
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.
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.
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.
#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:
Create list, add in the list, delete in the list, append two list, show list, add in beginning, add in end, add after a given element, return to main menu, delete in beginning, delete in
Picks a random value withing range low-high. Value returned seeds the rand() function. Print version information and exit. First seed the random function. Add line, to the array...
C input length and width of a rectangle and calculate perimeter of the rectangle. How to find perimeter of a rectangle in C language. Input length and width of the rectangle using
C program code to find the first Capital letter that exists in string, using recursion. We have included ctype.h in order to make use of "int isupper(char);" function that's defined inside
For Loop in C. First initialization happens and the counter variable gets initialized. Second step the condition is checked, where counter variable is tested for the given condition, if...
tmpfile: opens a temporary binary file for read/write operations and returns a pointer to the stream