C Programming Code Examples
C > If Else and Switch Case Code Examples
C program to find maximum between three numbers
/* C program to find maximum between three numbers
Write a C program to find maximum between three numbers using ladder if else or nested if. How to find maximum or minimum between three numbers using if else in C programming.
In this program we will continue our discussion and we will write program to find maximum between three numbers.
Step by step descriptive logic to find maximum between three numbers.
Input three numbers from user. Store it in some variable say j1, j2 and j3.
Compare first two numbers i.e. j1 > j2. If the statement is true then j2 is surely not max value. Perform one more comparison between j1 with j3 i.e. if(j1 > j3), then j1 is max otherwise j3.
If the statement j1 > j2 is false. Which indicates that j1 is not max. Hence, this time compare j2 with j3. If the statement j2 > j3 is true then j2 is max otherwise j3. */
#include <stdio.h>
int main()
{
int j1, j2, j3, max;
/* Input three numbers from user */
printf("Enter three numbers: ");
scanf("%d%d%d", &j1, &j2, &j3);
if(j1 > j2)
{
if(j1 > j3)
{
/* If j1 > j2 and j1 > j3 */
max = j1;
}
else
{
/* If j1 > j2 but j1 > j3 is not true */
max = j3;
}
}
else
{
if(j2 > j3)
{
/* If j1 is not > j2 and j2 > j3 */
max = j2;
}
else
{
/* If j1 is not > j2 and j2 > j3 */
max = j3;
}
}
/* Print maximum value */
printf("Maximum among all three numbers = %d", max);
return 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.
#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,
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.
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 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.
Step by step descriptive logic to find roots of Quadratic Equation using Switch Case. Input coefficients of quadratic equation. Store it in some variable say a, b, c. Find discriminant of
C Program code to Check Whether a number is even or odd using if else. A number exactly divisible by 2 leaving no remainder, is known as even number. Any number modulo divided
Prints 3 characters forward and backward to demonstrate character printing in C program code example. Define the first character, define the second character, define the third
C Program code Uses Recursive function and searches for an element in an unsorted list & display it's position of occurrence. enters the element needed to be searched. Printing the
C program code to input a number from user and find reverse of the given number using for loop. Input number to find reverse. Store it in some variable say j. Declare & initialize...
Display the content from the same file you are writing the source code. Open the file you are currently writing using statement fopen and assign it to the pointer fp. Scan the every
Delete all duplicate elements from an array. C program code to remove all duplicate integer values from an array. After performing delete operation array should only contain unique...