Académique Documents
Professionnel Documents
Culture Documents
Program 2-1
//A simple C++ program #include <iostream.h>
Main() { cout<< Programming is great fun!; }
Table 2-1
Character // # <> Name double slash Pound sign Opening and closing brackets Opening and closing parenthesis Opening and closing braces Opening and closing quotation marks Semicolon Description Marks the beginning of a comment. Marks the beginning of a preprocessor directive Encloses a filename when used with the #include directive Used in naming a function, as in int main () Encloses a group of statements, such as the contents of a function. Encloses a string of characters, such as a message that is to be printed on the screen
()
{} ""
The cout object is referred to as the standard output object. Its job is to output information using the standard output device
Program 2-2
// An unruly printing program #include <iostream.h>
Program Output
Transistor and diodesbe happy
cout does not produce a newline at the end of a statement To produce a newline, use either the stream manipulator endl or the escape sequence \n
Setw(n) Tokens
6
Program 2-3
// A well-adjusted printing program #include<iostream.h> #include<conio.h> main() { cout<<"Transistor and diodes"<<endl; cout<<"Electronics Components"<<endl; cout<<" Transistor Types"<<endl; cout<<"BJT,Mosfet,FET"<<endl; cout<<"Be happy"<<endl; getch(); }
Program Output
Transistor and diodes Electronics Components Transistor Types BJT,Mosfet,FET Be happy
Program 2-4
// A well-adjusted printing program #include<iostream.h> #include<conio.h> main() { cout<<"Transistor and diodes\n"; cout<<"Electronics Components\n"; cout<<" Transistor Types\n"; cout<<"BJT,Mosfet,FET\n"; cout<<"Be happy\n"; getch(); }
Program Output
Transistor and diodes Electronics Components Transistor Types BJT,Mosfet,FET Be happy
10
Program 2-5
#include<iostream.h> #include<iomanip.h> #include<conio.h> main() { clrscr(); cout<<setw(15)<<"university"<<setw(15)<<"ranking"; getch(); }
11
Program Output
university ranking
12
Program 2-6 #include<iostream.h> #include<iomanip.h> #include<conio.h> main() { clrscr(); cout<<setw(8)<<"20"<<setw(8)<<30<<endl; cout<<setw(8)<<"222"<<setw(8)<<333<<endl; cout<<setw(8)<<"444"<<setw(8)<<666<<endl; getch(); }
13
Program Output
20 222 444 30 333 666
14
15
16
Program 2-7
#include <iostream.h> #include<conio.h> main() { int value; value = 555; cout<<"The value is "<< value <<endl; getch(); }
17
Assignment statements:
Value = 555; //This line is an assignment statement.
The assignment statement explain the expression on the right of the equal sign then stores it into the variable named on the left of the equal sign The data type of the variable was in integer, so the data type of the expression on the right should explain to an integer as well.
18
19
Program 2-8
#include <iostream.h> #include<conio.h> main() { int x= 999,y= 2014; float z = 5.5; char name[15]= "University"; cout<<x<<endl; cout<<y<<endl; cout<<z<<endl; cout<<name<<endl; getch(); }
20
Program Output
21
Program 2-9
#include <iostream.h>
void main (void) { int apples; apples = 25; cout<< Today we sold << apples << bushels\n; cout << of apples.\n; }
22
Program Output
Today we sold 25 bushels of apples. Where are the constants in program 2-10?
23
24
25
Table 2-2
Table 2-5 Integer Data Types, Sizes and Ranges Data Type Size Range short 2 Bytes -32,768 to +32.767 unsigned short 2 Bytes 0 to +65,535 int 4 Bytes -2,147,4833,648 to +2,147,4833,647 unsigned int 4 Bytes 0 to 4,294,967,295 long 4 Bytes -2,147,4833,648 to +2,147,4833,647 unsigned long 4 Bytes 0 to 4,294,967,295
26
Program 2-11
// This program has variables of several of the integer types. #include <iostream.h> #include<conio.h> main() { int checking; unsigned int miles; int years; checking = -20; miles = 500; years = 5; cout << "We have made a long journey of " << miles; cout << " miles.\n"; cout << "Our checking account balance is " << checking; cout << "\nExactly " << years << " years ago I was "; cout << "stood on this computer lab as a student.\n"; getch(); }
27
Program Output
We have made a long journey of 500 miles. Our checking account balance is -20 Exactly 5 years ago I was stood on this computer lab as a student.
28
Program 2-12
// This program shows three variables declared on the same // line. #include <iostream.h> #include<conio.h> main() { int floors,rooms,hall;
floors = 3; rooms = 100; hall = 1; cout << UET Abbottabad boys Hostel has " << floors << " floors\n"; cout << "with " << rooms << " rooms and " << hall; cout << " hall.\n";
}
29
Program Output
UET Abbottabad boys Hostel has 3 floors with 100 rooms and 1 hall. .
30
31
Program 2-13
// This program demonstrates the close relationship between // characters and integers. #include <iostream.h>
main() { char letter; letter = 65; cout << letter << endl; letter = 66; cout << letter << endl; }
33
Program Output
A B
34
Program 2-14
// This program uses character constants #include <iostream.h>
void main(void) { char letter; letter = 'A'; cout << letter << endl; letter = 'B'; cout << letter << endl; }
35
Program Output
A B
36
Program 2-15
// This program uses character constants #include <iostream.h>
37
38
Program 2-16
#include <iostream.h> void main (void) { bool boolValue; boolValue = true; cout << boolValue << endl; boolValue = false; cout << boolValue << endl; }
39
Program Output 1 0 Internally, true is represented as the number 1 and false is represented by the number 0.
40
Program 2-17
#include <iostream.h> void main (void) { int month = 2, days = 28; cout << Month << month << has << days << days.\n; }
41
42
Program 2-18
// This program demonstrates the summing and multiplication between two numbers #include <iostream.h> #include<conio.h>
void main () { int a1,a2,y,z; clrscr(); cout<<"Enter first number"; cin>>a1; cout<<"Enter second number"; cin>>a2; y=a1+a2; z=a1*a2; cout<<"sum of numbers="<<y<<endl; cout<<"product of numbers="<<z<<endl; getch();
}
43
44
45
Program 2-19
#include <iostream.h> #include<conio.h>
void main () { clrscr(); int a1,a2,b,sum; a1=5; a2=10; b=2; sum=a1+a2+(++b); cout<<"sum="<<sum; cout<<"b="<<b; getch(); }
46
47
Program 2-20
// This program demonstrates the power of a number #include<math.h> #include<conio.h> #include<iostream.h> void main() {clrscr(); long int x,n; long int y; cout<<"value of x : "; cin>>x; cout<<"value of n : "; cin>>n; y=pow(x,n); cout<<"answer is : "<<y; getch(); }
48
49
50