Lec 08
Lec 08
C++
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1, num2;
cout<< “Enter the value of num1”;
cin >> num1;
cout<< “Enter the value of num2”;
cin>>num2;
cout << “the sum of num1 and num2 is” << num1 + num2;
getch();
}
Input Using cin
– cin is predefined object that is used to take data from the
keyboard. cin is predefined in C++ library.
– The operator >> is called the extraction or get-from operator. It
takes the value from the stream object (cin) on its left and
places it in the variable on its right.
Comments
• A comment is the portion of a program that exists only for
the human reader
• Comments are ignored by the compiler
• They are generally used for short explanations or notes
relevant to the code.
• They help the person writing a program, and anyone else
who must read the source file, understand what’s going on.
• Comments can be used to document code
• Types of comments in C++.
– Single line comments begin with // anywhere in the line
– Multiple line comments are enclosed between /* and */
Input With cin : Example
#include <iostream>
#include <conio.h>
using namespace std;
/* This program takes two integer numbers from user through cin statement and
print their sum on the screen. */
int main()
{
int num1, num2;
cout<< “Enter the value of num1”;
cin >> num1;
cout<< “Enter the value of num2”;
cin>>num2;
//prints the sum of the numbers on the screen
cout << “the sum of num1 and num2 is” << num1 + num2;
getch();
}
Escape Sequences
• There are many escape sequences in C++, which allow you to
control the output.
• The name reflects the fact that the backslash causes an
“escape” from the normal way characters are interpreted.
Constant
• Constant cannot be change by program.
• Constant has fixed value.
• Constant cannot be change the value at Run
Time.
• const float PI=3.14; is a constant with a fixed
value and may not be change by the the
program as the program runs.
Defining Constants
There are two simple ways in C++ to define constants:
• Using #define preprocessor.
• Using const keyword.
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
int main()
{
double number,answer, num1;
cout<<"Enter number ";
cin>>number;
cout<<"Enter power of the number";
cin>>num1;
answer=pow(number,num1);
cout<<"The power of the number is "<<answer;
getch();
}
Library Functions
Library Functions