C Programming Code Examples
C > Conversions Code Examples
C Program to Convert Binary to Octal Number System
/* C Program to Convert Binary to Octal Number System
In this 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 program, you should be familiar with the following C programming concepts: */
#include <stdio.h>
#include <math.h>
//This function converts binary number to octal number
int binaryToOctal(long binarynumber)
{
int octalnumber = 0, decimalnumber = 0, i = 0;
/* This while loop converts binary number "binarynumber" to the
* decimal number "decimalnumber"
*/
while(binarynumber != 0)
{
decimalnumber = decimalnumber + (binarynumber%10) * pow(2,i);
i++;
binarynumber = binarynumber / 10;
}
//i is re-initialized
i = 1;
/* This loop converts the decimal number "decimalnumber" to the octal
* number "octalnumber"
*/
while (decimalnumber != 0)
{
octalnumber = octalnumber + (decimalnumber % 8) * i;
decimalnumber = decimalnumber / 8;
i = i * 10;
}
//Returning the octal number that we got from binary number
return octalnumber;
}
int main()
{
long binarynumber;
printf("Enter a binary number: ");
scanf("%ld", &binarynumber);
// calling the function here
printf("Equivalent octal value: %d", binaryToOctal(binarynumber));
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.
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.
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.
#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:
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.
Program code to find equivalent resistance of parallel combination of resistive circuits. Enter the number of resistances. Enter value of each resistance. Printing equivalent series
Program performs a Pattern Searching using "KMP" 'Pattern Searching Algorithm'. Pattern searching is used for Finding the Existence of a Substring in a String. The pattern searching
C Area of Circle. Ellipse in which the two axes are of equal length. Plane curve generated by one point moving at a constant distance from a fixed point. You can compute area of circle.