SlideShare una empresa de Scribd logo
1 de 7
F2037 PROGRAMMING FUNDAMENTAL OF C++


    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

       Consider the code in the given program:



                                                          1
F2037 PROGRAMMING FUNDAMENTAL OF C++


    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
F2037 PROGRAMMING FUNDAMENTAL OF C++


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.




                                                         3
F2037 PROGRAMMING FUNDAMENTAL OF C++


// Program to find the average of 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;
 }

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;
 }




                                                         4
F2037 PROGRAMMING FUNDAMENTAL OF C++


Lab 1D

Procedure :

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()

                                                         5
F2037 PROGRAMMING FUNDAMENTAL OF C++


 {
 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;
 }

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;
}




                                                         6
F2037 PROGRAMMING FUNDAMENTAL OF C++


   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, state either variable or
       constant and find a suitable variable and constant
       name
       a. the number of month in a year
       b. the sum of x + y if given x = 5 and y = 10

   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 =                           amount
0.2                      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

Más contenido relacionado

La actualidad más candente

Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
TAlha MAlik
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 

La actualidad más candente (20)

Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
C++ book
C++ bookC++ book
C++ book
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in C
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
c++
 c++  c++
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)
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 

Destacado

Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
rohassanie
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
farhan amjad
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
dheva B
 

Destacado (20)

Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
SSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In HindiSSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In Hindi
 
Oops
OopsOops
Oops
 
Unit i
Unit iUnit i
Unit i
 
Oops Concepts
Oops ConceptsOops Concepts
Oops Concepts
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
 
Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
हिन्दी व्याकरण
हिन्दी व्याकरणहिन्दी व्याकरण
हिन्दी व्याकरण
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programming
 
OOPs concept and implementation
OOPs concept and implementationOOPs concept and implementation
OOPs concept and implementation
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
कारक
कारककारक
कारक
 
Working with and Leading People, Lecture 1
Working with and Leading People, Lecture 1Working with and Leading People, Lecture 1
Working with and Leading People, Lecture 1
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Vakya parichay
Vakya parichayVakya parichay
Vakya parichay
 

Similar a Labsheet1stud

Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
rohassanie
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
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
 

Similar a Labsheet1stud (20)

Introduction to C++ lecture ************
Introduction to C++ lecture ************Introduction to C++ lecture ************
Introduction to C++ lecture ************
 
C++ Question
C++ QuestionC++ Question
C++ Question
 
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...
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Programming in C
Programming in CProgramming in C
Programming in C
 
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++
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 
C Programming- Harsh Sharma
C Programming- Harsh SharmaC Programming- Harsh Sharma
C Programming- Harsh Sharma
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
 
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...
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 

Más de rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
rohassanie
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
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 201
rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
rohassanie
 

Más de 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
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
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
 

Último

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 

Último (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 

Labsheet1stud

  • 1. F2037 PROGRAMMING FUNDAMENTAL OF C++ 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 Consider the code in the given program: 1
  • 2. F2037 PROGRAMMING FUNDAMENTAL OF C++ 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. F2037 PROGRAMMING FUNDAMENTAL OF C++ 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. 3
  • 4. F2037 PROGRAMMING FUNDAMENTAL OF C++ // Program to find the average of 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; } 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; } 4
  • 5. F2037 PROGRAMMING FUNDAMENTAL OF C++ Lab 1D Procedure : 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() 5
  • 6. F2037 PROGRAMMING FUNDAMENTAL OF C++ { 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; } 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; } 6
  • 7. F2037 PROGRAMMING FUNDAMENTAL OF C++ 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, state either variable or constant and find a suitable variable and constant name a. the number of month in a year b. the sum of x + y if given x = 5 and y = 10 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 = amount 0.2 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