Programming in C++ - Module 02
Programming in C++ - Module 02
Partha Pratim
Das Module 02: Programming in C++
Objectives & Programs with IO & Loop
Outline
Hello World
Add numbers
Square Root
Standard Library
Sum Numbers Partha Pratim Das
Using bool
Summary
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
[email protected]
Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan
Module 02
Summary
Module 02
printf("Sq. Root of %lf is:", x); cout << "Sq. Root of " << x;
printf(" %lf\n", sqrt_x); cout << " is: " << sqrt_x << endl;
return 0; return 0;
} }
Input number: Input number:
2 2
Square Root of 2.000000 is: 1.414214 Square Root of 2 is: 1.41421
• Math Header is math.h (C Standard Library) • Math Header is cmath (C Standard Library in C++)
• Formatting (%lf) needed for variables • Formatting is derived from type (double) of variables
• sqrt function from C Standard Library • sqrt function from C Standard Library
• Default precision in print is 6 • Default precision in print is 5 (different)
Partha Pratim • All names are global • All names are within std namespace
Das
• stdout, stdin, printf, scanf • std::cout, std::cin
Objectives &
• Use
Outline
Hello World using namespace std;
Add numbers
Square Root
to get rid of writing std:: for every standard
Standard Library
library name
Sum Numbers
Using bool
std::cout << "Hello World in C++" cout << "Hello World in C++"
<< std::endl; << endl;
return 0; return 0;
} }
Module 02
Summary for (i = 0; i <= n; ++i) for (int i = 0; i <= n; ++i) // Local Decl.
sum = sum + i; sum = sum + i;
return 0; return 0;
} }
• Using int and #define for bool • stdbool.h included for bool • No additional headers required
• May use Bool (C99) • Bool type & macros (C99):
bool which expands to Bool bool is a built-in type
true which expands to 1 true is a literal
false which expands to 0 false is a literal
Module 02
Module 02
Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outline Tanwi Mallick, TA [email protected] 9674277774
Hello World Srijoni Majumdar, TA [email protected] 9674474267
Add numbers
Square Root Himadri B G S Bhuyan, TA [email protected] 9438911655
Standard Library
Sum Numbers
Using bool
Summary