SlideShare a Scribd company logo
1 of 7
F P 201 - PROGRAMMING FUNDAMENTAL


    LAB 1: VARIABLE, KEYWORD AND DATA TYPES

    Objectives

    By the end of this lab, students should be able to :
    • Describe the structure of C++ programmes
    •   Write, compile and run simple C++ programmes
    •   Identify and list keywords
    •   List and define the various data types
    •   Define variables and constants

    Theory/ Topics

    •   A program must have the function named main().
    •   Structure of C++ programmes
            o The structure of a simple C++ programme is similar to the
                 structure of C.


            Structure                                      Program
< Comment Entry>                        // First C++ program

< Preprocessor directives >             #include <iostream>
                                        #include <string>

main function                           int main()
 {                                        {
 < declaration stat >;                    int a;
 < C++ Statements >;                      cout << "Welcome to
 }                                                  Programming n”;
                                          return 0;
                                          }


    Table 1.1 : Structure of C++ Programme




                                                                       1
F P 201 - PROGRAMMING FUNDAMENTAL



    Consider the code in the given program:
    1. // is used to comment a single line. In addition to // symbol,
          C++ supports /* */ for comment entry operation. /* */ is
          used to comment a set of statements.
    2. #include <iostream> includes the header file for the
          program.
    3. main() is the function where the program is written.
    4. int a; is the variable declaration.
    5. cout is used to display the output statements.
    6.    Every statement is terminated with a semi-colon, similar to
          C.

•   Keywords - have a strict meaning as individual tokens in C++.
    They cannot be redefined or used in other contexts.
•   Identifier - Sequence of letters, digits and the special character "_"
    which is called an underscore. A letter or underscore must be the
    first character of an identifier.

As C++ program is built from C, the C++ compiler supports all the
features of C.

The following are the steps involved in writing, compiling and
executing a C++ program :

    1.     Open Microsoft Visual C++ and type the program.
    2.     Save the file with the corresponding extension
           (filename.cpp)
    3.     Compile the program.
    4.     Build & execute/run the program.




                                                                        2
F P 201 - PROGRAMMING FUNDAMENTAL




Lab 1A

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as lab1A.cpp.

The following program finds the sum of two numbers and displays it.


// Program to add two numbers

#include <iostream>
using namespace std;

void main()
  {
  int a, b, sum;
  a = 5;
  b = 2;
  sum = a + b;
  cout << "The sum is: " << sum << "n";
  }


Lab 1B

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Change the statement cout << "The sum is: " << sum; in line
9 to cout << "The average is: " << sum/2;
Step 4: Save the program as lab1B.cpp.


// Program to find the average of two numbers


                                                                      3
F P 201 - PROGRAMMING FUNDAMENTAL


#include <iostream>
using namespace std;

void main()
  {
  int a, b, sum;
  a = 5;
  b = 2;
  sum = a + b;
  cout << "The sum is: " << sum;
  }

Lab 1C

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as lab1C.cpp.

// The following program illustrates variable and
// constant declaration.

#include <iostream>
using namespace std;

const float PI = 3.14;

void main()
  {
  double radius = 3.0;
  double circumference;
  circumference = 2 * PI * radius;
  cout << "Circumference = " << circumference <<endl;
  }


Lab 1D

Procedure :



                                                         4
F P 201 - PROGRAMMING FUNDAMENTAL


Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as lab1D.cpp.

// The following program illustrates variable and
// constant declaration.

#include <iostream>
#define PI 3.14
using namespace std;

void main()
  {
  double radius = 3.0;
  double circumference;
  circumference = 2 * PI * radius;
  cout << "Circumference = " << circumference <<endl;
 }

Lab 1E

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as lab1E.cpp.

Program to show the declaration and initialization of variables
with float, double, char, int and boolean data type.

#include <iostream>
using namespace std;
void main()
  {
  char grade = 'F';
  float price = 77.01;
  double average = 145525.92;
  bool boolean_variable = true;
  int age = 50;
  cout << price <<"t"<< average <<"t"<<grade<<"t"<<
  boolean_variable <<"t"<<age<<endl;


                                                             5
F P 201 - PROGRAMMING FUNDAMENTAL


  }




Lab 1F

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as lab1F.cpp.

Program to show the declaration and initialization of variables
with string data type.

// my first string
#include <iostream>
#include <string>
using namespace std;

void main ()
{
  string mystring = "This is a string";
  cout << mystring;
}


LAB EXERCISE

1. Describe the functionality of using
   a. #include <string> as Preprocessor directives
   b. int main (void) as main function


2. For each statement below, write doen the suitable variable
      declaration:
      a. the number of month in a year
      b. the sum of x + y if given x = 5 and y = 10



                                                             6
F P 201 - PROGRAMMING FUNDAMENTAL



     3. Based on IPO chart information below:
        a. Declare the variable in C++ by using the appropriate data type
         b.   Transform the Algorithm into C++ code



                           IPO Chart Information
Input                           Processing                    Output
Number of late days = 7         Calculate amount              Display
Number of late charge = 0.2                                   amount
                                Algorithm
                                1. Declare the number of
                                   late days, late charges
                                   and amount

                                 2. Calculate the amount by
                                    multiplying the number
                                    of late days with late
                                    charge

                                 3. Display amount




                                                                        7

More Related Content

What's hot

C++ Overview
C++ OverviewC++ Overview
C++ Overviewkelleyc3
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers Appili Vamsi Krishna
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharDreamtech Labs
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++guestf0562b
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5REHAN IJAZ
 
Lecture01
Lecture01Lecture01
Lecture01Xafran
 
Functions in c
Functions in cFunctions in c
Functions in creshmy12
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variableimtiazalijoono
 

What's hot (20)

C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
C++ book
C++ bookC++ book
C++ book
 
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
C programming
C programmingC programming
C programming
 
# And ## operators in c
# And ## operators in c# And ## operators in c
# And ## operators in c
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Lecture01
Lecture01Lecture01
Lecture01
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
 

Similar to C++ Programming Fundamentals Lab: Variables, Keywords, Data Types

Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************Emad Helal
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 studrohassanie
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201rohassanie
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...bhargavi804095
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to cHattori Sidek
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)akmalfahmi
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...RSathyaPriyaCSEKIOT
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Devry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-startedDevry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-startednoahjamessss
 
Devry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-startedDevry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-startedgovendaagoovenda
 

Similar to C++ Programming Fundamentals Lab: Variables, Keywords, Data Types (20)

Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Devry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-startedDevry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-started
 
Devry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-startedDevry cis-170-c-i lab-1-of-7-getting-started
Devry cis-170-c-i lab-1-of-7-getting-started
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 

More from rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3rohassanie
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2) rohassanie
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3rohassanie
 

More from rohassanie (20)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 

C++ Programming Fundamentals Lab: Variables, Keywords, Data Types

  • 1. F P 201 - PROGRAMMING FUNDAMENTAL LAB 1: VARIABLE, KEYWORD AND DATA TYPES Objectives By the end of this lab, students should be able to : • Describe the structure of C++ programmes • Write, compile and run simple C++ programmes • Identify and list keywords • List and define the various data types • Define variables and constants Theory/ Topics • A program must have the function named main(). • Structure of C++ programmes o The structure of a simple C++ programme is similar to the structure of C. Structure Program < Comment Entry> // First C++ program < Preprocessor directives > #include <iostream> #include <string> main function int main() { { < declaration stat >; int a; < C++ Statements >; cout << "Welcome to } Programming n”; return 0; } Table 1.1 : Structure of C++ Programme 1
  • 2. F P 201 - PROGRAMMING FUNDAMENTAL Consider the code in the given program: 1. // is used to comment a single line. In addition to // symbol, C++ supports /* */ for comment entry operation. /* */ is used to comment a set of statements. 2. #include <iostream> includes the header file for the program. 3. main() is the function where the program is written. 4. int a; is the variable declaration. 5. cout is used to display the output statements. 6. Every statement is terminated with a semi-colon, similar to C. • Keywords - have a strict meaning as individual tokens in C++. They cannot be redefined or used in other contexts. • Identifier - Sequence of letters, digits and the special character "_" which is called an underscore. A letter or underscore must be the first character of an identifier. As C++ program is built from C, the C++ compiler supports all the features of C. The following are the steps involved in writing, compiling and executing a C++ program : 1. Open Microsoft Visual C++ and type the program. 2. Save the file with the corresponding extension (filename.cpp) 3. Compile the program. 4. Build & execute/run the program. 2
  • 3. F P 201 - PROGRAMMING FUNDAMENTAL Lab 1A Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as lab1A.cpp. The following program finds the sum of two numbers and displays it. // Program to add two numbers #include <iostream> using namespace std; void main() { int a, b, sum; a = 5; b = 2; sum = a + b; cout << "The sum is: " << sum << "n"; } Lab 1B Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Change the statement cout << "The sum is: " << sum; in line 9 to cout << "The average is: " << sum/2; Step 4: Save the program as lab1B.cpp. // Program to find the average of two numbers 3
  • 4. F P 201 - PROGRAMMING FUNDAMENTAL #include <iostream> using namespace std; void main() { int a, b, sum; a = 5; b = 2; sum = a + b; cout << "The sum is: " << sum; } Lab 1C Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as lab1C.cpp. // The following program illustrates variable and // constant declaration. #include <iostream> using namespace std; const float PI = 3.14; void main() { double radius = 3.0; double circumference; circumference = 2 * PI * radius; cout << "Circumference = " << circumference <<endl; } Lab 1D Procedure : 4
  • 5. F P 201 - PROGRAMMING FUNDAMENTAL Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as lab1D.cpp. // The following program illustrates variable and // constant declaration. #include <iostream> #define PI 3.14 using namespace std; void main() { double radius = 3.0; double circumference; circumference = 2 * PI * radius; cout << "Circumference = " << circumference <<endl; } Lab 1E Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as lab1E.cpp. Program to show the declaration and initialization of variables with float, double, char, int and boolean data type. #include <iostream> using namespace std; void main() { char grade = 'F'; float price = 77.01; double average = 145525.92; bool boolean_variable = true; int age = 50; cout << price <<"t"<< average <<"t"<<grade<<"t"<< boolean_variable <<"t"<<age<<endl; 5
  • 6. F P 201 - PROGRAMMING FUNDAMENTAL } Lab 1F Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as lab1F.cpp. Program to show the declaration and initialization of variables with string data type. // my first string #include <iostream> #include <string> using namespace std; void main () { string mystring = "This is a string"; cout << mystring; } LAB EXERCISE 1. Describe the functionality of using a. #include <string> as Preprocessor directives b. int main (void) as main function 2. For each statement below, write doen the suitable variable declaration: a. the number of month in a year b. the sum of x + y if given x = 5 and y = 10 6
  • 7. F P 201 - PROGRAMMING FUNDAMENTAL 3. Based on IPO chart information below: a. Declare the variable in C++ by using the appropriate data type b. Transform the Algorithm into C++ code IPO Chart Information Input Processing Output Number of late days = 7 Calculate amount Display Number of late charge = 0.2 amount Algorithm 1. Declare the number of late days, late charges and amount 2. Calculate the amount by multiplying the number of late days with late charge 3. Display amount 7