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

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
Week 1 Algorithmization and Programming Languages.
Beginning C++ Through Game Programming, Second Edition
C++ Programming: Basic Elements of C++.
First steps Jordi Cortadella Department of Computer Science.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
COMP102 Lab 071 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
1. 2 Review How many bits are in a byte? a) 4 b) 8 c) 16 d) 256.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
C++ for Engineers and Scientists Second Edition
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic file operations in C++
Computing Fundamentals
Introduction to C++ October 2, 2017.
Data Streams.
More About File Reading
Computers & Programming Languages
Basic File I/O and Stream Objects
File Streams and File I/O
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
CS150 Introduction to Computer Science 1
Fundamental Programming
Programming with Data Files
ifstreams and ofstreams
Reading from and Writing to Files Part 2
Presentation transcript:

Arithmetic Operators Operation Operator Example Addition = 9 Subtraction = 1 and = -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

Fundamental C++ Variables TypeSize in Bytes Values integer variables unsigned short int20 to 65,535 short int2-32,768 to 32,767 unsigned int40 to 4,294,967,295 int4-2,147,483,648 to 2,147,483,647 unsigned long int40 to 4,294,967,295 long int4-2,147,483,648 to 2,147,483,647 long long int to floating-point variables float41.2e-38 to 3.4e38 double82.2e-308 to 1.8e308 logical variablebool1true or false character variablechar1256 character values

(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.)

#include 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; }

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

#include using namespace std; int main() { ifstream fin("numbers.in"); //open input file ofstream fout("numbers.out");//create and open output file int 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 file fout.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

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