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 > If Else and Switch Case Code Examples

check whether triangle is equilateral, scalene or isosceles

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/* check whether triangle is equilateral, scalene or isosceles Write a C program to input sides of a triangle and check whether a triangle is equilateral, scalene or isosceles triangle using if else. How to check whether a triangle is equilateral, scalene or isosceles triangle in C programming. Properties of triangle A triangle is said Equilateral Triangle, if all its sides are equal. If a, b, c are three sides of triangle. Then, the triangle is equilateral only if a == b == c. A triangle is said Isosceles Triangle, if its two sides are equal. If a, b, c are three sides of triangle. Then, the triangle is isosceles if either a == b or a == c or b == c. A triangle is said Scalene Triangle, if none of its sides are equal. Logic to check equilateral, scalene or isosceles triangle Input sides of a triangle from user. Store it in some variables say j1, j2 and j3. Check if(j1 == j2 && j2 == j3), then the triangle is equilateral. If it is not an equilateral triangle then it may be isosceles. Check if(j1 == j2 || j1 == j3 || j2 == j3), then triangle is isosceles. If it is neither equilateral nor isosceles then it scalene triangle. */ #include <stdio.h> int main() { int j1, j2, j3; /* Input sides of a triangle */ printf("Enter three sides of triangle: "); scanf("%d%d%d", &j1, &j2, &j3); if(j1==j2 && j2==j3) { /* If all sides are equal */ printf("Equilateral triangle."); } else if(j1==j2 || j1==j3 || j2==j3) { /* If any two sides are equal */ printf("Isosceles triangle."); } else { /* If none sides are equal */ printf("Scalene triangle."); } 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,

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.

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.

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

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.

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.

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.



Program that merges lines alternatively from 2 files & print result. Getting the names of file to be concatenated. Opening the files in read only mode. Opening a new file in write mode.





C program search for an element in the linked list using recursion. Uses recursive function & search for an element in a Linked List. Linked List is an Ordered set of Data Elements, each


C Program code remove 'trailing white space' characters is 'lot more easier' and faster than removing leading white space characters. As in this case we need not to perform any 'char'