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++ > Mathematics Code Examples

Progam that gives all details of a Triangle given the lengths of its sides

/* Progam that gives all details of a Triangle given the lengths of its sides */ #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> main() { clrscr(); float a,b,c,S,D,A,B,C,Area,R; printf("Enter the lengths of the three sides of the triangle : "); scanf("%f%f%f",&a,&b,&c); S = (a+b+c)/2.0; // S is the semiperimeter of the triangle D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle if(D<=0) { printf(" The triangle cannot be formed"); getch(); exit(0); } if((a==b || b==c || c==a) && !(a==b && b==c && c==a)) // this complex logic is to eliminate interpretting a triangle with all three // sides equal as both isosceles and equilateral. printf(" The triangle is ISOSCELES "); if(a==b && b==c && c==a) printf(" The triangle is EQUILATERAL "); if(a!=b && b!=c && c!=a) printf(" The triangle is SCALENE "); Area = sqrt(D); R = (a*b*c)/(4.0*Area); printf("PERIMETER = %.2f units ",(2.0*S)); printf("AREA = %.2f sq.units ",Area); printf("CIRCUM RADIUS = %.2f units ",R); // using sine rule,we get... A = (180.0/3.1415926)*asin(a/(2.0*R));// value of pi should be upto 7 B = (180.0/3.1415926)*asin(b/(2.0*R));// decimal places of accuracy and also C = (180.0/3.1415926)*asin(c/(2.0*R));// note that the 7th decimal place is // 6 and not 7 as it had to be if were if(A==90.0 || B==90.0 || C==90.0) // approximated to 7 decimal places printf(" The triangle is RIGHT ANGLED "); if(A<90.0 && B<90.0 && C<90.0) printf(" The triangle is ACUTE ANGLED "); if(A>90.0 || B>90.0 || C>90.0) printf(" The triangle is OBTUSE ANGLED "); printf(" The angles are as follows : "); printf("A = %.2f degrees ",A); printf("B = %.2f degrees ",B); printf("C = %.2f degrees ",C); printf(" Where A,B,C stand for angles opposite to sides %.2f,%.2f,%.2f",a,b,c); printf(" respectively "); getch(); return 0; }

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

Compute sine. Returns the sine of an angle of x radians. sin() function is a library function of cmath header, it is used to find the sine of the given number (angle), it accepts a number (x) and returns the sine of angle x radians. Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type). This function is also overloaded in <complex> and <valarray> (see complex sin and valarray sin).

Arithmetic Operator is used to performing mathematical operations such as addition, subtraction, multiplication, division, modulus, etc., on the given operands. For example: 6 + 3 = 9, 5 - 3 = 2, 3 * 4 = 12, etc. are the examples of arithmetic operators. Let's discuss the different types of Arithmetic Operators in the C programming. Plus Operator is a simple Plus (+) Operator used to add two given operands. We can use Plus Operator with different data types such as integer, float, long, double, enumerated and string type data to add the given operand. The minus operator is denoted by the minus (-) symbol. It is used to return the subtraction of the first number from the second number. The data type of the given number can be different types, such as int, float, double, long double, etc., in the programing language.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

Compute arc sine. Returns the principal value of the arc sine of x, expressed in radians. In trigonometrics, arc sine is the inverse operation of sine. Calculates the inverse sine of a number. The asin() function is used to find the arc sine of a number means give a sin value to this function it will return the angle in radian corresponding to that value. In trigonometrics, arc sine is the inverse operation of sine. asin() implements the inverse sine function, commonly called arc sine. The argument x must be between -1 and 1, inclusive: -1 ≤ x ≤ 1. If x is outside the function's domain-that is, if x is greater than 1 or less than -1-the function incurs a domain error.

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

Compute square root. Returns the square root of x. The sqrt() function in C++ returns the square root of a number. This function is defined in the cmath header file. There are various functions available in the C++ Library to calculate the square root of a number. Most prominently, sqrt is used. It takes double as an argument. The <cmath> header defines two more inbuilt functions for calculating the square root of a number (apart from sqrt) which has an argument of type float and long double. Therefore, all the functions used for calculating square root in C++ are. Mathematically, sqrt(x) = √x.

'Virtual Function' is a special form of member function that is 'declared within a base class' and 'redefined' by a derived class. "virtual" is used to create a virtual function, precede the

A 'function implementing' Binary search on a 'Sorted array'. Every time this function called, counted as a iteration of 'binary search'. So if value is "Less than Value" at start index more

To convert decimal number to binary number in C++, you have to enter the decimal number to convert it into 'binary number' to print the equivalent value in binary format as shown in

Learn to "multiply two matrices" and display it using user defined function. This program asks user to enter the size of the matrix (rows and columns). Then, it asks the user to enter