際際滷

際際滷Share a Scribd company logo
C++
Programming language: It is an artificial language
designed to communicate to a machine, particularly
a computer.
Programs: These are set of instructions written in
any programming language to serve some purpose &
are written by following the syntax of the
programming language.
C++: It was developed in 1980s at Bell Laboratory by
Bjarn Stroustrup as an Object Oriented Programming
Language.
OOP Programming: This approach views a problem in
terms of objects involved rather than procedure for
doing it.
Character set: A set of valid characters that a
language can recognize.
Letters: A to Z, a-z.
Digits: 0 to 9
Special Symbols: Space + - * / ^  ( ) { } [ ] = < > ,
  $ . ; : % ! & ? _(Underscore) # <= >= @
White Spaces: Blank Space, Horizontal Tab (),
Carriage return(J), Newline, Form Feed
Other Characters: C++ can process any of the
256 ASCII characters as data or as literals
Tokens: The smallest unit of a program. They are
a. Keywords
b. Identifier
c. Literals(constants)
d. Operators
e. Punctuators
Keywords : they are reserved words meant for
specific purpose. They can not be used for giving
names to variables, arrays, functions etc.
For e.g, If , if else , int , float , case, switch
Identifiers: a name given to program elements such
as variables, arrays, functions etc.
Rule for giving a name to variable :
1. It must start with an alphabet or a character(_).
2. It cannot contain any other character other
than (_) but can contain numbers only after first
character.
Literals : (Often referred to as constants) are data
items that never change their values during a
program run.
Operators: Operators are tokens that trigger some
computation when applied to variables and other
object in an expression. They are
I/o : << and >>
Arithmetic : +, - , * , / , %
Increment/Decrement : ++ , - -
Relational : ==, <, >, <=, >=, !=
Logical : &&, ||, !
Conditional : ?
Punctuators : Also known as separators. They
enhance a programs readability. For eg
[ ] , { } , ( ) , ; , : , * , = , #
C++
A graphic outline of a simplistic approach to show how programs
get developed.
Define the problem to be solvedSTEP1::
Design a solutionSTEP2::
Write a program that implements the solutionSTEP3::
Compile the programSTEP4::
Link object filesSTEP5::
Test & Debug programSTEP6::
/*Program to display area & perimeter of rectangle*/
#include<iostream.h>
void main()
{
int L,B,A,P;
L=6;
cout<<Enter Length<<endl;
cin>>L;
cout<<Enter Breadth<<endl;
cin>>B;
A=L*B;
P=2*(L+B);
cout<<Area is <<A<<endl;
cout<<Perimeter is <<P<<endl;
}
Preprocessor Directives
Comments
Function Declarator
Variable Declaration
Variable Initialisation
Displaying text on screen
Assigning entered value to L
Formula
Displaying result
Function Begin
Function ends
To save a file: F2
To run a program: Ctrl+F9
To view the output screen: Alt+F5
To exit from the TC++ editor: Alt+x
To exit from DOS prompt: Type exit
Next you have a summary of the basic fundamental data types in C++, as
well as the range of values that can be represented with each one:
Name Description Size Range*
char
Character or small
integer.
1
signed: -128 to 127
unsigned: 0 to 255
int Integer. 4
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int
(long)
Long integer. 4
signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
float
Floating point
number.
4 +/- 3.4e +/- 38 (~7 digits)
Comments: are statements which are not compiled & are
used for programmers reference to describe the purpose of
statements or block of statements. It can be done by using
single line comment(//) and multi line comment(/*..*/).
Header files: files that are included in C++ program to
incorporate the use of functions as required. for e.g.,
iostream.h : cin & cout are used to input value through
keyboard & display the message on display device.
Data Types: are the means to identify the type of data &
associated operations of handling it. They are of two types:
fundamental & derived data types.
Fundamental data types are:
char (stores single character) , int ( stores whole numbers) &
float/double (stores real numbers). declaration of
variable(syntax) :
data_type variable_name;
INPUT AND OUTPUT
cout (to be read as SEE OUT): Output Operator(<<) also
called insertion operator. It is used to direct a value to
standard output generally a Monitor.
cin (to be read as SEE IN): input operator(>>) also known as
extraction operator. It is used to read a value from standard
input generally a Keyboard.
//PROGRAM TO CALCULATE AREA OF TRIANGLE
#include<iostream.h>
#include<math.h>
void main ( )
{
float S1 ,S2 ,S3, S, area, perimeter;
cout<<Enter sides of the triangle<<endl;
cin>>S1>>S2>>S3;
S=(S1+S2+S3)/2.0;
area=sqrt(S*(S-S1)*(S-S2)*(S-S3));
perimeter=S1+S2+S3;
cout<<Area=<<Area;
cout<< Perimeter=<<Perimeter;
}
Write a program to :
1. Calculate area & circumference of circle
2. Calculate Sum & Product of two numbers and display them.
3. Input a number in Meters and convert it in Centimeters
4. Calculate the square and cube of a number input by the user
// Area & Circumference of circle
#include<iostream.h>
#include<conio.h>
void main ( )
{
clrscr();
float pi=3.14 ,r , area, cir;
cout<<Enter radius : <<endl;
cin>>r;
area=pi*r*r;
cir=2*pi*r;
cout<<Area=<<area;
cout<<circumference=<<cir;
getch();
}
//Sum & Product of 2 Numbers
#include<iostream.h>
#include<conio.h>
void main ( )
{
clrscr();
float a, b, sum, pr;
cout<<Enter a and b: <<endl;
cin>>a>>b;
sum=a+b;
pr=a*b;
cout<<sum=<<sum;
cout<<product =<<pr;
getch();
}
// Input a number in Meters and convert it in Centimeters
#include<iostream.h>
#include<conio.h>
void main ( )
{
clrscr();
float num, cm;
cout<<Enter a number in meters : <<endl;
cin>>num;
cm = num*100;
cout<<The Entered number is<<cm << centimeters;
getch();
}
To Find Errors in a C++ Program
/* A program to add two numbers
#include<iosteam.h>
void main ( );
{
clrscr();
int a, b, sum;
cout<<Enter Value for a<<endl;
cin>>A;
cout<<Enter Value for b;
cin>>b;
Sum=a+b;
cout<< Sum is =<<sum
}
Errors are:
Comment not terminated / closed
Spelling of iosteam.h
 ;  not to used at the end of main
Header (conio.h) is not included
Variable is a and not A
Variable is sum and not Sum
End of Statement should be  ;
PRACTICE QUESTIONS
Write a program to Convert the following:
1. Meter to Kilometer (1 Km = 1000 Meter)
2. Centigrade to Fahrenheit (F = C*(9/5)+32)
3. Feet into Meters(1 Meter = 3.28 Feet)
4. Hours into Minutes, Seconds (1 Hr = 60 Min = 3600 Sec)
ACTIVITY QUESTIONS 9A( Oct 13)
Write a program to
Get temperature in Centigrade to Fahrenheit
(F = C*9/5+32)
OR
Input three values from the user. Add & multiply them and
then display the output
Save it with your roll no and class. For Eg: 9A424
ACTIVITY QUESTIONS 9B( Oct 13)
Write a program to
Get temperature in Centigrade to Fahrenheit
(F = C*9/5+32)
OR
Input three values from the user. Add & multiply them and
then display the output
Save it with your roll no and class. For Eg: 9B255
ACTIVITY QUESTIONS 9C( Oct 13)
Write a program to
Get length in feet and then convert it into meters
(1 Meter = 3.28 Feet)
OR
Input two values from the user. Add & multiply them and
then display the output
Save it with your roll no and class. For Eg: 9A245
ACTIVITY QUESTIONS 9D( Oct 13)
Write a program to
Get length in feet and then convert it into meters
(1 Meter = 3.28 Feet)
OR
Input two values from the user. Add & multiply them and
then display the output
Save it with your roll no and class. For Eg: 9A245
ACTIVITY QUESTIONS 9E( Oct 13)
Write a program to
Get temperature in Centigrade to Fahrenheit
(F = C*9/5+32)
OR
Input three values from the user. Add & multiply them and
then display the output
Save it with your roll no and class. For Eg: 9B255

More Related Content

C++

  • 2. Programming language: It is an artificial language designed to communicate to a machine, particularly a computer. Programs: These are set of instructions written in any programming language to serve some purpose & are written by following the syntax of the programming language. C++: It was developed in 1980s at Bell Laboratory by Bjarn Stroustrup as an Object Oriented Programming Language. OOP Programming: This approach views a problem in terms of objects involved rather than procedure for doing it.
  • 3. Character set: A set of valid characters that a language can recognize. Letters: A to Z, a-z. Digits: 0 to 9 Special Symbols: Space + - * / ^ ( ) { } [ ] = < > , $ . ; : % ! & ? _(Underscore) # <= >= @ White Spaces: Blank Space, Horizontal Tab (), Carriage return(J), Newline, Form Feed Other Characters: C++ can process any of the 256 ASCII characters as data or as literals
  • 4. Tokens: The smallest unit of a program. They are a. Keywords b. Identifier c. Literals(constants) d. Operators e. Punctuators Keywords : they are reserved words meant for specific purpose. They can not be used for giving names to variables, arrays, functions etc. For e.g, If , if else , int , float , case, switch Identifiers: a name given to program elements such as variables, arrays, functions etc. Rule for giving a name to variable : 1. It must start with an alphabet or a character(_). 2. It cannot contain any other character other than (_) but can contain numbers only after first character.
  • 5. Literals : (Often referred to as constants) are data items that never change their values during a program run. Operators: Operators are tokens that trigger some computation when applied to variables and other object in an expression. They are I/o : << and >> Arithmetic : +, - , * , / , % Increment/Decrement : ++ , - - Relational : ==, <, >, <=, >=, != Logical : &&, ||, ! Conditional : ? Punctuators : Also known as separators. They enhance a programs readability. For eg [ ] , { } , ( ) , ; , : , * , = , #
  • 7. A graphic outline of a simplistic approach to show how programs get developed. Define the problem to be solvedSTEP1:: Design a solutionSTEP2:: Write a program that implements the solutionSTEP3:: Compile the programSTEP4:: Link object filesSTEP5:: Test & Debug programSTEP6::
  • 8. /*Program to display area & perimeter of rectangle*/ #include<iostream.h> void main() { int L,B,A,P; L=6; cout<<Enter Length<<endl; cin>>L; cout<<Enter Breadth<<endl; cin>>B; A=L*B; P=2*(L+B); cout<<Area is <<A<<endl; cout<<Perimeter is <<P<<endl; } Preprocessor Directives Comments Function Declarator Variable Declaration Variable Initialisation Displaying text on screen Assigning entered value to L Formula Displaying result Function Begin Function ends
  • 9. To save a file: F2 To run a program: Ctrl+F9 To view the output screen: Alt+F5 To exit from the TC++ editor: Alt+x To exit from DOS prompt: Type exit
  • 10. Next you have a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one: Name Description Size Range* char Character or small integer. 1 signed: -128 to 127 unsigned: 0 to 255 int Integer. 4 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 long int (long) Long integer. 4 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 float Floating point number. 4 +/- 3.4e +/- 38 (~7 digits)
  • 11. Comments: are statements which are not compiled & are used for programmers reference to describe the purpose of statements or block of statements. It can be done by using single line comment(//) and multi line comment(/*..*/). Header files: files that are included in C++ program to incorporate the use of functions as required. for e.g., iostream.h : cin & cout are used to input value through keyboard & display the message on display device. Data Types: are the means to identify the type of data & associated operations of handling it. They are of two types: fundamental & derived data types. Fundamental data types are: char (stores single character) , int ( stores whole numbers) & float/double (stores real numbers). declaration of variable(syntax) : data_type variable_name;
  • 12. INPUT AND OUTPUT cout (to be read as SEE OUT): Output Operator(<<) also called insertion operator. It is used to direct a value to standard output generally a Monitor. cin (to be read as SEE IN): input operator(>>) also known as extraction operator. It is used to read a value from standard input generally a Keyboard. //PROGRAM TO CALCULATE AREA OF TRIANGLE #include<iostream.h> #include<math.h> void main ( ) { float S1 ,S2 ,S3, S, area, perimeter; cout<<Enter sides of the triangle<<endl; cin>>S1>>S2>>S3; S=(S1+S2+S3)/2.0; area=sqrt(S*(S-S1)*(S-S2)*(S-S3)); perimeter=S1+S2+S3; cout<<Area=<<Area; cout<< Perimeter=<<Perimeter; }
  • 13. Write a program to : 1. Calculate area & circumference of circle 2. Calculate Sum & Product of two numbers and display them. 3. Input a number in Meters and convert it in Centimeters 4. Calculate the square and cube of a number input by the user // Area & Circumference of circle #include<iostream.h> #include<conio.h> void main ( ) { clrscr(); float pi=3.14 ,r , area, cir; cout<<Enter radius : <<endl; cin>>r; area=pi*r*r; cir=2*pi*r; cout<<Area=<<area; cout<<circumference=<<cir; getch(); } //Sum & Product of 2 Numbers #include<iostream.h> #include<conio.h> void main ( ) { clrscr(); float a, b, sum, pr; cout<<Enter a and b: <<endl; cin>>a>>b; sum=a+b; pr=a*b; cout<<sum=<<sum; cout<<product =<<pr; getch(); }
  • 14. // Input a number in Meters and convert it in Centimeters #include<iostream.h> #include<conio.h> void main ( ) { clrscr(); float num, cm; cout<<Enter a number in meters : <<endl; cin>>num; cm = num*100; cout<<The Entered number is<<cm << centimeters; getch(); }
  • 15. To Find Errors in a C++ Program /* A program to add two numbers #include<iosteam.h> void main ( ); { clrscr(); int a, b, sum; cout<<Enter Value for a<<endl; cin>>A; cout<<Enter Value for b; cin>>b; Sum=a+b; cout<< Sum is =<<sum } Errors are: Comment not terminated / closed Spelling of iosteam.h ; not to used at the end of main Header (conio.h) is not included Variable is a and not A Variable is sum and not Sum End of Statement should be ;
  • 16. PRACTICE QUESTIONS Write a program to Convert the following: 1. Meter to Kilometer (1 Km = 1000 Meter) 2. Centigrade to Fahrenheit (F = C*(9/5)+32) 3. Feet into Meters(1 Meter = 3.28 Feet) 4. Hours into Minutes, Seconds (1 Hr = 60 Min = 3600 Sec)
  • 17. ACTIVITY QUESTIONS 9A( Oct 13) Write a program to Get temperature in Centigrade to Fahrenheit (F = C*9/5+32) OR Input three values from the user. Add & multiply them and then display the output Save it with your roll no and class. For Eg: 9A424
  • 18. ACTIVITY QUESTIONS 9B( Oct 13) Write a program to Get temperature in Centigrade to Fahrenheit (F = C*9/5+32) OR Input three values from the user. Add & multiply them and then display the output Save it with your roll no and class. For Eg: 9B255
  • 19. ACTIVITY QUESTIONS 9C( Oct 13) Write a program to Get length in feet and then convert it into meters (1 Meter = 3.28 Feet) OR Input two values from the user. Add & multiply them and then display the output Save it with your roll no and class. For Eg: 9A245
  • 20. ACTIVITY QUESTIONS 9D( Oct 13) Write a program to Get length in feet and then convert it into meters (1 Meter = 3.28 Feet) OR Input two values from the user. Add & multiply them and then display the output Save it with your roll no and class. For Eg: 9A245
  • 21. ACTIVITY QUESTIONS 9E( Oct 13) Write a program to Get temperature in Centigrade to Fahrenheit (F = C*9/5+32) OR Input three values from the user. Add & multiply them and then display the output Save it with your roll no and class. For Eg: 9B255