CPP

Lecture 3 : Input-Output

Output Instruction

printf("Hello World!"); //in C
cout << "Hello World!"; // in C++

printf("sum of %d and %d is %d", a,b,c);
cout << "sum of "<<a<<" and "<<b<< "is" << c;

printf("%d", a+b);
cout << a+b;

Input Instruction


scanf("%d", &a); // in C
cin >> a; // in C++

scanf("%d%d",&a, &b);
cin >> a >> b;

Remember

Header files

About iostream.h

endl

Sample Program (square.cpp)


#include <iostream.h> //Declaration of cin and cout
#include <conio.h> //Declaration of clrscr(), getch()

//action statements
int main()
{
	clrscr();
	int x; //Declaration after action statement
	cout << "Enter a Number : " << endl;
	cin >> x;
	int s = x*x; //Run-time / Dynamic Initializaton
	cout << "Square of" << x << "is" << s;
	return 0;
}