Basic C++ Program Example



Write a program to accept a number and display its square.

Code:

#include<iostream.h>
#include<conio.h>
Void main()
{
 int x, y;
cout<< “Enter a number”;
cin >> x;
 y = x *x;
cout << “The square of” << x << “is” <<y;
getch();
}

Output:
Enter a number 5
The square of 5 is 25

Write a program to accept two numbers and display the product.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
 int x, y, z;
cout<< “Enter first number”;
cin >> x;
cout<< “Enter second number”;
cin >> y;
 z = x * y;
cout << “The product of” << x << “and “ << y << “is” <<z;
getch();
}
Output:
  Enter first number 5
  Enter second number 8
  The product of 5 and 8 is 40