C++ Programming Code Examples
C++ > Mathematics Code Examples
C++ calculator code for beginner
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* C++ calculator code for beginner
Write a simple C++ program which have four different functions for basic operations like addition, subtraction, division and multiplication.
Program should be menu based asking user to enter option and if user enters a wrong option a proper message to enter right option should be displayed.
Program has four different function for each calculator operation
do while loop is used to display menu options
if else statement is used to check option
Program has two variables on which all basic operations are applied
Every functions receives two parameters and return result */
#include <iostream>
using namespace std;
int addition (int number1, int number2);
int subtraction (int number1, int number2);
int division (int number1, int number2);
int multiplication(int number1, int number2);
int main()
{
char option;
int number1, number2, result;
cout<<"\n\t\t\tSimple calculator"<<endl;
do
{
cout<<"Enter + for Addtion\nEnter - for Subtraction\n
Enter / for Division\nEnter * for Multiplication\n
Enter E to EXIT\n";
cout<<"\n\t\t\tEnter Option: ";
cin>>option;
if(option!='e'&&option!='E')
{
cout<<"\n\t\t\tEnter First Number: ";
cin>>number1;
cout<<"\n\t\t\tEnter Second Number: ";
cin>>number2;
}
if(option!='+'&&option!='-'&&option!='/'
&&option!='*'&&option!='e'&&option!='E')
{
cout<<"\n\t\t\tselect the valid option\n";
}
else if(option=='+')
{
result=addition(number1,number2);
cout<<"\n\t\t\t"<<number1<<" + "<<number2<<" = "<<result<<endl;
}
else if(option=='-')
{
result=subtraction(number1,number2);
cout<<"\n\t\t\t"<<number1<<" - "<<number2<<" = "<<result<<endl;
}
else if(option=='/')
{
result=division(number1,number2);
cout<<"\n\t\t\t"<<number1<<" / "<<number2<<" = "<<result<<endl;
}
else if(option=='*')
{
result=multiplication(number1,number2);
cout<<"\n\t\t\t"<<number1<<" X "<<number2<<" = "<<result<<endl;
}
}while(option!='e'&&option!='E');
cout<<"\n\t\t\tProgram EXIT Successfully.......\n\t\t\t";
return 0;
}
int addition(int number1, int number2)
{
return (number1+number2);
}
int subtraction(int number1, int number2)
{
return (number1-number2);
}
int division(int number1, int number2)
{
if(number2==0)
{
cout<<"\n\t\t\tDivide by ZERO not allowed :"<<endl;
return 0;
}
return (number1/number2);
}
int multiplication(int number1, int number2)
{
return (number1*number2);
}
#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.
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.
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.
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.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
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.
A predefined object of the class called iostream class is used to insert the new line characters while flushing the stream is called endl in C++. This endl is similar to \n which performs the functionality of inserting new line characters but it does not flush the stream whereas endl does the job of inserting the new line characters while flushing the stream. Hence the statement cout<<endl; will be equal to the statement cout<< '\n' << flush; meaning the new line character used along with flush explicitly becomes equivalent to the endl statement in C++.
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
In graph theory, an "edge coloring" of a graph is an assignment of colors to the edges of the graph so that no two adjacent edges have the same color. Any "2 edges" connected to same
To "count total number" of words used in any sentence in C++, you have to ask to enter the sentence. And then, to count total number of words present in the string, search for spaces
Program demonstrates the implementation of Sorted linked list to Balanced BST. Counts the number of nodes in linked list. Construct balanced BST and returns root of it. Returns