Programming is instructing a computer to perform a task for you with the help of a programming language.
LEGO MINDSTORMS
Programming on C++
A computer program (software) contains a sequence of instructions for a computer.One program is usually composed of three parts: Input part gets the data from an input device. Our programs, in the book, will get the data from keyboard or from a text file. Process part is the hardest working part of the program. It carries out the algorithm and finds out the desired result. Output part gives the result of the program. Our programs will display the result on the screen or print it into a text file.
OUR 1 st PROGRAM Program will write text on screen: START “Hello world!” END
#include using namespace std; int main() { cout <<"Hello world!"; system("pause"); return 0; }
each C++ statement ends with a semicolon character (';'). Writing comments in the program code- // OR /* */
#include //includes the declarations of the basic standard input-output //library in C++, and its functionality is going to be used later //in the program. using namespace std; //Namespaces are containers that contain the declarations of all //the elements of the standard C++ library int main() //the only function in this program. { cout <<"Hello world!"; //print "Hello world!". cout is //declared in the iostream standard //file within the std namespace system("pause"); //Wait until user hits a key and //displays a message return 0; //the main function ends properly }
START “5+3=“;5+3 END
#include using namespace std; int main() { cout <<"5 + 3 = "<<5+3<<endl; //calculate and print the sum system("pause"); return 0; }
Getting Data from the User (Input) "cin" command is used to read data from the standard input
Calculating sum of numbers START READ num1, num2 END Sum=num1+num2 PRINT Sum
#include using namespace std; int main() { int num1, num2, sum; //num1, num2 and sum are three //variables type of integer. cout<<"Enter two integers:"<<endl; cin >> num1 >> num2; //cin reads two values for //num1 and num2. sum = num1 + num2; //sum gets the value of num1+num2. cout <<"Sum is "<<sum<<endl; system("PAUSE"); return 0; }
HOMEWORK Make a program to calculate area and perimeter of a rectangle. Input: length of side1 and length of side2. Process: area = side1*side2 Perimeter = 2*(side1+side2) Output: area and perimeter