0% found this document useful (0 votes)
5 views4 pages

Lab 02 Solution

The document contains solutions to practice tasks from a Programming Fundamentals lab. The tasks include: 1) Writing a program to print numbers separated by spaces to represent the number of characters in a name, in three different ways. 2) Displaying a favorite poem. 3) Printing a box and arrow shape using escape sequences. The solutions demonstrate using stream insertion operators, escape sequences, and cout statements to output the requested shapes and text.

Uploaded by

njaukk.msmggg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Lab 02 Solution

The document contains solutions to practice tasks from a Programming Fundamentals lab. The tasks include: 1) Writing a program to print numbers separated by spaces to represent the number of characters in a name, in three different ways. 2) Displaying a favorite poem. 3) Printing a box and arrow shape using escape sequences. The solutions demonstrate using stream insertion operators, escape sequences, and cout statements to output the requested shapes and text.

Uploaded by

njaukk.msmggg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Merit-Quality-Excellence

Sukkur IBA University Mirpurkhas Campus

Programming Fundamentals (Fall 2021)


LAB No: 02
Solution
Instructor: Sanam Fayaz
Objective of Lab No. 2:
After performing lab1, students will be able to:
o Use escape sequence in their programs
o Use the countable datatypes o Use the
measurable datatypes

Practice Task No. 1:


Write a C++ program to print numbers from 1 to N (where N is the number of
characters in your first name) on the same line with each pair of adjacent numbers
separated by space in the following ways:

Suppose your full name is Ahsan Nazeer hence, your first name is Ahsan which
contains five characters.
12 34 5

Write a program to achieve the aforementioned task in following three ways. a.


Using one statement with one stream insertion operator.
b. Using N statements
c. Using one statement with N stream insertion operators.

Solution (a)
// Suppose name is Ahsan Nazeer
#include <iostream> using
namespace std;
int main ()
{
cout<<"1 2 \t 3 4 \t 5 \n" ;
system("pause");
return 0;
}
Solution (b)
// Suppose name is Ahsan Nazeer
#include <iostream> using
namespace std;
int main ()
{
cout<<"1" ;
cout<<" 2" ; cout
<<"\t 3" ;
cout<<" 4" ;
cout<<"\t 5 \n" ;
system("pause");
return 0;
}

Solution (c)
// Suppose name is Ahsan Nazeer
#include <iostream>
#include <stdlib.h> using
namespace std;
int main ()
{
cout<<"1" <<" 2" <<"\t 3" <<" 4" <<"\t 5 \n" ;
system ("PAUSE") ;
return 0;
}

Practice Task No. 2:


Write a program that displays your favorite poem.

Solution:
#include <iostream> using
namespace std;
int main ()
{
cout<<"\t \t \t \t \t poem \n" ;
cout<<"\t \t \t I wandered lonely as a cloud \n" ; cout<<"\t \t \t That floats on
high o'er vales and hills \n" ; cout<<"\t \t \t When all at once I saw a crowd \n"
; cout<<"\t \t \t A host, of golden daffodils \n" ; cout<<"\t \t \t Beside the
lake, beneath the trees \n" ; cout<<"\t \t \t Fluttering and dancing in the
breeze \n\n\n\n\n\n\n\n\n\n" ; system ("PAUSE") ;
return 0;
}

Practice Task No. 3:


Write a program to print a box, an arrow of asterisks using escape sequences:

Solution (box) #include


<iostream> using
namespace std;
int main ()
{
cout<<" **********\n" ;
cout<<"\n *\t * \n" ; cout<<"\n
*\t * \n" ; cout<<"\n *\t * \n" ;
cout<<"\n *\t * \n" ; cout<<"\n
*\t * \n" ; cout<<"\n *\t * \n" ;
cout<<"\n *\t * \n" ; cout<<"\n
**********\n" ; system("pause");
return 0;
}

Solution (Arrow)

#include <iostream> using


namespace std;
int main ()
{
cout<<" \t\t*" ;
cout<<"\n\t ***" ;
cout<<"\n\t *****" ; cout<<"\n
\t\t*" ; cout<<"\n \t\t*" ;
cout<<"\n \t\t*" ; cout<<"\n
\t\t*" ; cout<<"\n \t\t*" ;
cout<<"\n \t\t*" ;
cout<<"\n\n\n\n\n\n\n\n" ;
system("pause");
return 0;
}

You might also like