Friday 5 December 2014

Computer Programming Learning Lessons

Learning Objects: After completing this session you will be able to define variables of different data  types, store integer, float and character values, display text on screen and take input from keyboard and perform arithmetic operations on variables.

Example Program 1:

#include <iostream.h>

void main ()

{

cout << “This is my first program in c++”;

}



Explanation:

#include <iostream.h>  instruction will include the header file iostream into our program. iostream header file is required for cout [console output] and cin [console input].

void main () is function definition for main function. main function is always present in any c/c++ program.

{ and } are known as curly brackets. They define the boundary of main function.

cout << “This is my first program in c++”; statement will display the text enclosed within . You can place any text you want to display within and the program will display your text on the screen.

Task 1:

a)  Display your name on the screen.
b)  Display your hobbies on the screen.
c)  Display your favorite book name on the screen.









http://www.mumair.co.nr, Computer Programming, Iqra University        3


Task 1 Answers: [add additional sheets if required]





(a):

#include <iostream.h>

void main ()
{

cout << “Muhammad Mohsin Azhar”;

}

(b):

#include <iostream.h>

void main ()
{

cout << “Playing Cricket , Browsing Internet  <<”;

}

(c):

#include <iostream.h>

void main ()

{

cout << “Electronic Devices and Circuit Theory by Rober Boylestad”;

}


























































http://www.mumair.co.nr, Computer Programming, Iqra University        4


Example Program 2:

#include <iostream.h>

void main ()

{

int  my_variable;

my_variable = 20;

cout << “The value of my_variable is: “ << my_variable;

}

Explanation:

int   my_variable; will create a variable my_variable of type integer in computers memory. At this  moment my_variable will have some garbage value placed in it. That garbage value could be anything within the range of int data type.

my_variable  =  20;  statement  will  assign  value  20,  which  is  an  integer  value  to variable  my_variable.  Assigning  value  to  variable  in  program  is  known  as  hard coding.

cout << “The value of my_variable is: << my_variable; will display the following text on the screen. The value of my_variable is: 20

Supporting material:

The data type float is used to store decimal values. For example float set=7.8; will place 7.8 into variable set.

The data type char is used to store single character. For example char status=’N;
will store N into variable status.

Task 2:

a)  Create a variable data of type int and assign it the value 60, now display the content of your variable on screen.
b)  Create a variable temperature of type float and assign it the value 36.5, now display the content of your variable on screen.
c)  Create a variable data of type char and assign it the value A, now display the
content of your variable on screen.



http://www.mumair.co.nr, Computer Programming, Iqra University        5


Task 2 Answers: [add additional sheets if required]
































































http://www.mumair.co.nr, Computer Programming, Iqra University        6


Example Program 3:

#include <iostream.h>

void main ()

{

float user_input;

cout << “Please enter value: “;

cin >> user_input;

cout << “You have entered: “ << user_input;

}

Explanation:

cin >>  user_input;  statement  will  take  user input  from keyboard.  cin  is  console input. It is used for taking input from keyboard. You can take input in any type of variable using cin >> varaiable_name; Taking input from keyboard is known as soft coding.

Task 3:

Create a program which declares three variable of type int, float and char each. Display appropriate message on screen and then take input from user then display
the values entered by user on screen.
























http://www.mumair.co.nr, Computer Programming, Iqra University        7


Task 3 Answer: [add additional sheets if required]
































































http://www.mumair.co.nr, Computer Programming, Iqra University        8


Example Program 4:

#include <iostream.h>

void main ()

{

float input1, input2;

cout << “Please enter input 1: “;

cin >> input1;

cout << “Please enter input 2: “;

cin >> input2;

cout << “input1 + input 2 = “ << input1+input2;

}

Task 4:

Line by line, explain the working of program 4?

Create programs which can perform addition (+), subtraction (‐), multiplication (*)
and division (/) on integer and then float values?




























http://www.mumair.co.nr, Computer Programming, Iqra University        9


Task 4 Answers: [add additional sheets if required]












No comments:

Post a Comment