Back to Top

Wednesday, 1 April 2015

Begginer's Guide to C++- Chapter 2: Your First Program

A short one, but I promise, this is even more exciting than the previous one! You will make your first ever program! So without further discussions, let us head straight on to the tutorial.

Learnt in Previous Chapter:
  1. Variables, datatypes, operators
  2. Special Characters
  3. Initializing a variable

Header Files:
Header files are files that contain specific functions, that perform special tasks. To include a specific header file, follow this syntax:
#include<headerfile name>
This line goes when you start writing a program.

Some important header files-
I will only describe the ones that you may need right now. To know about more function, write the name of the header file in C++ and right click to open up the help interface, it will have the names of all functions available.
  1. iostream.h -
    1. It contains all input output functions I will soon describe about. So including this file is important in your programs for now.
  2. conio.h -
    1. clrscr() - Clears the output screen.
    2. getch() - Waits for the user to enter a single character.

Taking input/Giving output:
  1. cin>> - This is used to get input from the user.
Syntax - cin>>variable name;
  1. cout<< - This is used to display the value of a variable or expression.
Syntax - cout<<variable name/expression;
You can also get input of more than 2 variables by cin>>a>>b>>c; similarly, you can display 2 variables/expressions by cout<<a<<b<<a+c;

Eg - If you want to display, “Yay, this guide is great” on the user screen”, you can write -
cout<<”Yay, this guide is great”;
       If you want to display the value of a variable a,
cout<<”The value of a is ”<<a;

void main():
All your code for the program will go below void main() line in {} -
Eg- void main()
{int x;
char y;

We are now ready to write our very first program, it is easy ain’t it? I have solved this one for you, so that you get an approximate idea of how statements are to be used. So let’s get started then.

Q- Write a program to add 2 variables.
void main()
clrscr();                       //Clears the output screen
int x,y;
cout<<”Enter first number \n”;
cout<<”Enter second number \n”;
cout<<”The sum is ”<<x+y;//Alternatively, you can store x+y in another variable and can display it.
getch();                                //Waits for a character input before program termination*

*We need to use getch(), otherwise the program will terminate even before you see the output. You can try it yourself!

Questions for practice-
Q1 - Write a program to find the area of a circle, whose radius is given by the user.
Q2 - Write a program to enter marks in 3 subjects and calculate total and percentage and print them on screen. Assume that the marks are out of 100.
Q3 - Write a program to calculate simple interest. Principal value, time and rate are entered by the user.
Most important question-
Q4 - Write a program to SWAP the values of 2 variables. Eg- if initially a=10 and b=20, after the program is executed, a=20 and b=10. You are permitted to use-
  1. 3 variables
  2. 2 variables.