SlideShare a Scribd company logo
F2037 PROGRAMMING FUNDAMENTAL WITH C++


    LAB 3: INPUT/OUTPUT

Objectives

By the end of this lab, students should be able to :
• Describe the structure of input-output statement
• Write a program that use the input and output
    statements

Theory/ Topics

•     In C++, << is called stream insertion operator.
•     Output on standard output device (such as monitor) is
      accomplish via the use of cout and operator >>.
•     In C++, >> is called stream extraction operator.
•     Input from the standard input device (such as
      keyboard) is accomplished by using cin.
•     To use cin and cout, the program must include header
      file iostream.
•      A manipulator is used to format output.
•     endl is the simplest manipulator which cause insertion
      point to move to the beginning of next line.
•     In C++,  is called the escape character while n is
      called newline escape sequence.
•     Common escape sequence is shown in the following
      table:
      For this       Use this      Example          Output
                                "Don't do
'                   '                           Don't do that
                                that"
                                "She said
"                   "                           She said "hi"
                                "hi""
                                "Backslash:  Backslash: 
                   
                                "


                                                          1
F2037 PROGRAMMING FUNDAMENTAL WITH C++



                                                1
[newline]          n          "1n2"
                                                2
[horizontal tab] t            "1t2"           12

    •   Manipulators allowed us to format the output in a
        desired way.
    •   To use manipulator we need to include header file
        iomanip.
    •   The following table is showing manipulators that is
        commonly used:
          Syntax                         Purpose
setprecision(n)            Set decimal precision
setfill(c)                 Setfill character
setw(n)                    Set field width


Activity 3A

Procedure :

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

The following program illustrate input name from user and
the usage of usage of header file string.h

#include <iostream>
#include <string >

using namespace std;

void main()
   {

                                                         2
F2037 PROGRAMMING FUNDAMENTAL WITH C++


   string name;
   cout << "Please enter your name : ";
   cin >> name;
   cout << "Your name have " <<
   name.size() << "
   characters.n";
   }

Activity 3B

Procedure :

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

// setfill example
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
  {
  cout << setfill ('x') << setw (10);
  cout << 77 << endl;
  return 0;
  }


Activity 3C

Procedure :

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




                                                         3
F2037 PROGRAMMING FUNDAMENTAL WITH C++


#include <iostream>
#include <iomanip>

using namespace std;

int main()
  {
  const int max = 12;
  const int width = 6;
  for(int row = 1;row <= max;row++)
    {
    for(int col = 1;col <= max;col++)
      {
      cout << setw(width) << row * col;
      }
    cout << endl;
    }
  return 0;
  }

Activity 3D

Procedure :

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

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
        const int max = 12;
        const int width = 6;
        for(int row = 1;row <= max;row++) {
                if(row % 2) {
                  cout << setiosflags(ios::left);
                }


                                                         4
F2037 PROGRAMMING FUNDAMENTAL WITH C++


                  else {
                        cout <<
resetiosflags(ios::left);
                }
                for(int col = 1;col <= max;col+
+) {
                        cout << setw(width) <<
row * col;
                }
                cout << endl;
        }
        return 0;
}


Activity 3D

Procedure :

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

#include <iostream>
#include <iomanip>

using namespace std;

int main()
  {
  double x = 800000.0/81.0;
  cout << setprecision(2) << x;
  return 0;
  }




Exercise 3.1


                                                         5
F2037 PROGRAMMING FUNDAMENTAL WITH C++




  Display the following output using function provided in header
file, iomanip.


        **********
        ********
        *****
        ***
        *

Exercise 3.2

Write a program the will receive an input in $US and
display the corresponding value in RM. (RM3.52 = $US1).


Exercise 3.3

Write a program that will receive 2 inputs (a and b) and
calculate the value of c by using Pythagoras Theorem.

(Hint: Use mathematical library functions)

                                    Check:
               c                    If a = 4, b = 3
                              b     hence c = 5



                   a




                                                              6

More Related Content

DOCX
C++ Question
PPTX
Testing lecture after lec 4
PDF
2Bytesprog2 course_2014_c8_units
PPT
Lecture 3 c++
PPT
DOCX
c plus plus programsSlide
DOCX
Dam31303 dti2143 lab sheet 7
DOCX
Computer Architecture and Organization lab with matlab
C++ Question
Testing lecture after lec 4
2Bytesprog2 course_2014_c8_units
Lecture 3 c++
c plus plus programsSlide
Dam31303 dti2143 lab sheet 7
Computer Architecture and Organization lab with matlab

What's hot (20)

PDF
Recursion
PPT
Circular queues
PPTX
Tail Recursion in data structure
PPTX
MCM FUNCTIONS BLACKBOARD COMPATIBILITY
PPTX
BB - Functions (Operations and Piecewise)
PDF
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
PDF
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
PPT
Removal Of Recursion
PPTX
Function recap
PDF
When RV Meets CEP (RV 2016 Tutorial)
DOCX
Bti1022 lab sheet 9 10
PDF
Introduction to Computer and Programing - Lecture 04
PPTX
My lecture infix-to-postfix
PPTX
Mathematics Function in C ,ppt
PPTX
Pointers
PPTX
Function BPK2
DOCX
C programming Lab 1
PPTX
Functions in c++,
PDF
Infix to Prefix (Conversion, Evaluation, Code)
Recursion
Circular queues
Tail Recursion in data structure
MCM FUNCTIONS BLACKBOARD COMPATIBILITY
BB - Functions (Operations and Piecewise)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
Removal Of Recursion
Function recap
When RV Meets CEP (RV 2016 Tutorial)
Bti1022 lab sheet 9 10
Introduction to Computer and Programing - Lecture 04
My lecture infix-to-postfix
Mathematics Function in C ,ppt
Pointers
Function BPK2
C programming Lab 1
Functions in c++,
Infix to Prefix (Conversion, Evaluation, Code)
Ad

Viewers also liked (20)

PDF
SSRP Self Learning Guide Maths Class 10 - In Hindi
PPTX
Unit i
DOC
Labsheet2
PPT
Oops Concepts
PPTX
Introduction to object oriented language
PDF
Introduction - Imperative and Object-Oriented Languages
PPTX
Object Oriented Programming
PDF
Cbse class 10 hindi course b model answers by candidates 2015
PPT
Object Oriented Language
DOC
Labsheet1stud
PPT
Basics of c++
PPTX
हिन्दी व्याकरण
PPT
OOPs concept and implementation
PPTX
Chapter3: fundamental programming
PDF
4 pillars of OOPS CONCEPT
PPTX
कारक
PPT
02a fundamental c++ types, arithmetic
PPTX
Advance oops concepts
PPSX
Vakya parichay
SSRP Self Learning Guide Maths Class 10 - In Hindi
Unit i
Labsheet2
Oops Concepts
Introduction to object oriented language
Introduction - Imperative and Object-Oriented Languages
Object Oriented Programming
Cbse class 10 hindi course b model answers by candidates 2015
Object Oriented Language
Labsheet1stud
Basics of c++
हिन्दी व्याकरण
OOPs concept and implementation
Chapter3: fundamental programming
4 pillars of OOPS CONCEPT
कारक
02a fundamental c++ types, arithmetic
Advance oops concepts
Vakya parichay
Ad

Similar to Labsheet_3 (20)

DOC
Cs2312 OOPS LAB MANUAL
DOC
Labsheet1 stud
PDF
Functionssssssssssssssssssssssssssss.pdf
DOCX
Programming in c
DOCX
Labsheet2 stud
PPT
Fp201 unit2 1
PDF
Introduction to c programming
PPTX
C BASICS.pptx FFDJF/,DKFF90DF SDPJKFJ[DSSIFLHDSHF
PPTX
Presentation on Function in C Programming
DOCX
UNIT-II CP DOC.docx
PDF
CP MATERIAL.pdf Computer Programming Material
PPT
Cinfo
PPT
Practical basics on c++
PDF
C++ manual Report Full
PDF
Introduction to cpp
PPT
Linux_C_LabBasics.ppt
PPT
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
PDF
Programming Fundamentals Functions in C and types
PPTX
C Programming Homework Help
Cs2312 OOPS LAB MANUAL
Labsheet1 stud
Functionssssssssssssssssssssssssssss.pdf
Programming in c
Labsheet2 stud
Fp201 unit2 1
Introduction to c programming
C BASICS.pptx FFDJF/,DKFF90DF SDPJKFJ[DSSIFLHDSHF
Presentation on Function in C Programming
UNIT-II CP DOC.docx
CP MATERIAL.pdf Computer Programming Material
Cinfo
Practical basics on c++
C++ manual Report Full
Introduction to cpp
Linux_C_LabBasics.ppt
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
Programming Fundamentals Functions in C and types
C Programming Homework Help

More from rohassanie (20)

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

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
KodekX | Application Modernization Development
PDF
Electronic commerce courselecture one. Pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Modernizing your data center with Dell and AMD
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KodekX | Application Modernization Development
Electronic commerce courselecture one. Pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity
Advanced methodologies resolving dimensionality complications for autism neur...
Spectral efficient network and resource selection model in 5G networks
CIFDAQ's Market Insight: SEC Turns Pro Crypto
“AI and Expert System Decision Support & Business Intelligence Systems”
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Modernizing your data center with Dell and AMD
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx

Labsheet_3

  • 1. F2037 PROGRAMMING FUNDAMENTAL WITH C++ LAB 3: INPUT/OUTPUT Objectives By the end of this lab, students should be able to : • Describe the structure of input-output statement • Write a program that use the input and output statements Theory/ Topics • In C++, << is called stream insertion operator. • Output on standard output device (such as monitor) is accomplish via the use of cout and operator >>. • In C++, >> is called stream extraction operator. • Input from the standard input device (such as keyboard) is accomplished by using cin. • To use cin and cout, the program must include header file iostream. • A manipulator is used to format output. • endl is the simplest manipulator which cause insertion point to move to the beginning of next line. • In C++, is called the escape character while n is called newline escape sequence. • Common escape sequence is shown in the following table: For this Use this Example Output "Don't do ' ' Don't do that that" "She said " " She said "hi" "hi"" "Backslash: Backslash: " 1
  • 2. F2037 PROGRAMMING FUNDAMENTAL WITH C++ 1 [newline] n "1n2" 2 [horizontal tab] t "1t2" 12 • Manipulators allowed us to format the output in a desired way. • To use manipulator we need to include header file iomanip. • The following table is showing manipulators that is commonly used: Syntax Purpose setprecision(n) Set decimal precision setfill(c) Setfill character setw(n) Set field width Activity 3A Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as lab3A.cpp. The following program illustrate input name from user and the usage of usage of header file string.h #include <iostream> #include <string > using namespace std; void main() { 2
  • 3. F2037 PROGRAMMING FUNDAMENTAL WITH C++ string name; cout << "Please enter your name : "; cin >> name; cout << "Your name have " << name.size() << " characters.n"; } Activity 3B Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab3B.cpp. // setfill example #include <iostream> #include <iomanip> using namespace std; int main () { cout << setfill ('x') << setw (10); cout << 77 << endl; return 0; } Activity 3C Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab3C.cpp. 3
  • 4. F2037 PROGRAMMING FUNDAMENTAL WITH C++ #include <iostream> #include <iomanip> using namespace std; int main() { const int max = 12; const int width = 6; for(int row = 1;row <= max;row++) { for(int col = 1;col <= max;col++) { cout << setw(width) << row * col; } cout << endl; } return 0; } Activity 3D Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab3C.cpp. #include <iostream> #include <iomanip> using namespace std; int main() { const int max = 12; const int width = 6; for(int row = 1;row <= max;row++) { if(row % 2) { cout << setiosflags(ios::left); } 4
  • 5. F2037 PROGRAMMING FUNDAMENTAL WITH C++ else { cout << resetiosflags(ios::left); } for(int col = 1;col <= max;col+ +) { cout << setw(width) << row * col; } cout << endl; } return 0; } Activity 3D Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab3D..cpp. #include <iostream> #include <iomanip> using namespace std; int main() { double x = 800000.0/81.0; cout << setprecision(2) << x; return 0; } Exercise 3.1 5
  • 6. F2037 PROGRAMMING FUNDAMENTAL WITH C++ Display the following output using function provided in header file, iomanip. ********** ******** ***** *** * Exercise 3.2 Write a program the will receive an input in $US and display the corresponding value in RM. (RM3.52 = $US1). Exercise 3.3 Write a program that will receive 2 inputs (a and b) and calculate the value of c by using Pythagoras Theorem. (Hint: Use mathematical library functions) Check: c If a = 4, b = 3 b hence c = 5 a 6