arithmetic operators operation operator example addition + 5 + 4 = 9 subtraction - 5 - 4 = 1 and 4 -...

7
Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer) / 15 / 3 = 5 and 12 / 5 = 2 Modulus % 12 % 5 = 2, 15 % 3 = 0, and 3 % 5 = 3

Upload: shannon-lyons

Post on 23-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer)

Arithmetic Operators

Operation Operator ExampleAddition + 5 + 4 = 9Subtraction - 5 - 4 = 1 and 4 - 5 = -1Multiplication * 5 * 4 = 9Division (integer) / 15 / 3 = 5 and 12 / 5 = 2Modulus % 12 % 5 = 2, 15 % 3 = 0, and 3 % 5 = 3

Page 2: Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer)

Fundamental C++ VariablesType Size in

BytesValues

integer variables

unsigned short int 2 0 to 65,535

short int 2 -32,768 to 32,767

unsigned int 4 0 to 4,294,967,295

int 4 -2,147,483,648 to 2,147,483,647

unsigned long int 4 0 to 4,294,967,295

long int 4 -2,147,483,648 to 2,147,483,647

long long int 8 -9.223.372.036.854 to 9.223.372.036.853

floating-pointvariables

float 4 1.2e-38 to 3.4e38

double 8 2.2e-308 to 1.8e308

logical variable bool 1 true or false

character variable char 1 256 character values

Page 3: Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer)

(Sum of digits)

Make a program that reads a three-digit and then calculates sum of the digits. (Hint: Use the modulus (%) and integer division (/) operators.)

Page 4: Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer)

#include <iostream>using namespace std;int main(){

int num,a,b,c,sum; cout<<"Enter 3 digit integers"<<endl;cin >> num;

a=(num%10);b=(num %100)/10;c=num/100; sum = a+b+c;

cout <<a<<“+”<<b<<“+”<<c<<“=“<<sum<<endl;system("PAUSE");return 0;

}

Page 5: Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer)

Using Text Files as Input and Output

C++ provides two functions to read from a text file and to write into a text file. Those functions are ifstream() and ofstream().

Both functions are declared in the <fstream> header. ifstream opens an existing input file whereas, ofstream creates or recreates and opens the output file. After you have finished with the input and output files you should close them so that their resources become available again for the system. close() function is used to close the open files.

Page 6: Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer)

#include <fstream>using namespace std;int main(){

ifstream fin("numbers.in"); //open input fileofstream fout("numbers.out");//create and open output fileint num1, num2;

fin >>num1 >>num2; //read two integers from the input file.fout <<"sum is "<<num1+num2<<endl;fout <<"difference is "<<num1-num2<<endl;fout <<"product is "<<num1*num2<<endl;fout <<"integer quotient is "<<num1/num2<<endl;fout <<"floating-point quotient is "<<(float)num1/num2<<endl;fin.close(); //close the input filefout.close(); //close the output file

system("PAUSE");return 0;

}

The following program read two integers (num1 and num2) from the file numbers.in. Computes sum, difference, product and quotient of those two numbers and then writes the result into the file numbers.out

Page 7: Arithmetic Operators Operation Operator Example Addition + 5 + 4 = 9 Subtraction - 5 - 4 = 1 and 4 - 5 = -1 Multiplication * 5 * 4 = 9 Division (integer)

HOMEWORK

(Sum of digits)Make a program that reads a four-digit integer from the file "number.in" and then calculates sum of the digits and writes the result into the file "sum.out". (Hint: Use the modulus (%) and integer division (/) operators.)