#include <iostream> #include <string> #include <iomanip> #include <math.h> using namespace std; /* ArithmeticsStrings Assignment 2021-09-17 Anthony Quintero This program calculates the Arithmetics strings that are assigned by the instructor. */ int main() { //number bases int ibase; cout << "Please enter an integer to convert: "; cin >> ibase; cout << endl; cout << "Use iomainips to condition the stream" << endl; cout << "The entered integer in octal form is: " << oct << ibase << endl; cout << "The entered integer hexademical form is: " << hex << ibase << endl; cout << "THe entered integer in decimal form is: " << dec << ibase << endl; cout << endl; cout << "Use iomainips setbase to format what follow in the stream" << endl; cout << "The entered integer in octal form is: " << setbase(8) << ibase << endl; cout << "The entered in hexademical form is: " << setbase(16) << ibase << endl; cout << "The enterd integer in decimal form is: " << setbase(10) << ibase << endl; cout << endl << endl; int ishift01, ishift02; cout << "please enter an integer to shift to multiply by 4 : "; cin >> ishift01; ishift02 = ishift01; cout << "This integer multiplied by 4 with left shift binary operator is: " << (ishift01 << 2) << endl; cout << "This integer divided by 4 with right shift binary operator is: " << (ishift02 >> 2) << endl; cout << endl << endl; //portability //char, short, int, unsigned, long, long long, bool, float, double cout << "All sizes are in bytes : " << endl; cout << "char size: " << sizeof(char) << endl; cout << "short size: " << sizeof(short) << endl; cout << "int size: " << sizeof(int) << endl; cout << "unsigned size: " << sizeof(unsigned) << endl; cout << "long size " << sizeof(long) << endl; cout << "long long size: " << sizeof(long long) << endl; cout << "bool size: " << sizeof(bool) << endl; cout << "float size: " << sizeof(float) << endl; cout << "double size: " << sizeof(double) << endl; cout << endl << endl; int ic; cout << "Please enter a number for a char : "; cin >> ic; char c = ic; cout << "character for the int c is : " << c << endl; cout << endl << endl; unsigned int uInt; cout << "Please enter a negative number : "; cin >> uInt; cout << "The unsigned int is : " << uInt; cout << endl << endl; //portability //asked a question about midterm int i = 1; i = i / i++; //do not apply a post fix operatble with a variable, it will give you undefined cout << "int i =1; i=i / i++" << endl << "i is " << i << endl; cout << endl << endl; i = 0; i = i * ++i; cout << "i = 0; i = i * ++i;" << endl << "The value for i is " << i << endl; i = 0; i = (i = i + 1) + (i = i + 2); cout << "i = 0; i = (i = i + 1) + (i = i + 2);" << endl << "The value for i is : " << i << endl; cout << endl << endl; bool a = true, b = false; a = (b == false) && (b++); cout << "bool a = true, b = false; a = (b = false) && (b++); :" << endl; cout << "a is " << a << endl; cout << "b is " << b << endl; cout << endl << endl; //calculations int int01, int02; cout << "please enter a 1st integer : "; cin >> int01; cout << "please enter a 2nd integer : "; cin >> int02; cout << endl; cout << "The entered 1st integer is : " << int01 << endl; cout << "The entered 2nd integer is : " << int02 << endl; cout << endl; cout << "The sum of the two integers is : " << (int01 + int02) << endl; cout << "The product of the two integer is : " << (int01 * int02) << endl; cout << "The whole number quotient 1st/2nd is : " << (int01 / int02) << endl; cout << "The remainder of 1st/2nd is : " << (int01 % int02) << endl; cout << "The float result of 1st/2nd is : " << (int01 / (float)int02) << endl; cout << "The 1st to the power of the 2nd is : " << (pow(int01, int02)) << endl; cout << endl << endl; float float01, float02; cout << "Please enter a 1st real number : "; cin >> float01; cout << "Please enter a 2nd real number :"; cin >> float02; cout << endl; cout << "The entered 1st real number is: : " << float01 << endl; cout << "The entered 2nd real number is: : " << float02 << endl; cout << endl; cout << "The sum of two real number is : " << (float01 + float02) << endl; cout << "The product of the two real numbers is : " << (float01 * float02) << endl; cout << "The quotient of 1st/2nd is : " << (float01 / float02) << endl; cout << "The whole number part of the 1st number is : " << ((int)float01) << endl; cout << "The whole number part of the 2nd number is : " << ((int)float02) << endl; cout << "The fractional part of the 1st number is : " << (float01 - ((int)float02)) << endl; cout << endl << endl; int anInteger; cout << "Please enter an integer to convert to a float : "; cin >> anInteger; cout << "The integer as a float is " << showpoint << (float)anInteger << endl; cout << endl; //strings string string01, string02; cout << "Please enter 1st string : "; cin >> string01; cout << endl; cout << "The first string is : " << string01 << endl; cout << "The length of this string is : " << string01.length() << endl; cout << "The first letter of this string is : " << string01.at(0) << endl; cout << "The last letter of this string is : " << string01.at(string01.length() - 1) << endl; cout << endl; cout << "Please enter a 2nd string : "; cin >> string02; cout << endl; cout << "The 1st string concentrated with 2nd string : " << string01 + string02 << endl; cout << "The 1st string concentrated with the 2nd string with a space in between is : " << string01 + " " + string02 << endl; cout << endl << endl; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}