You are on page 1of 12

CSL-113 : Computer Programming Lab

Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Lab 5
EXERCISE 1
Write a C++ program to print the following pattern on the screen. Ask user to enter a integer
value from 1 to 10. Use while loop.
SOURCE CODE:

#include <iostream>

using namespace std;

int main()

int n;

cout<<"Enter a value from 1 to 10: ";

cin>>n;

for (int a = 1; a <= n; a++)

for (int b = 1; b <= a; b++)

{ cout<<b;}

cout<<endl;

return 0;}

OUTPUT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 2
By applying for loop, write a C++ program that prints

a) A sequence of numbers in ascending order from 1 to 100 (inclusive).

Source code

#include<iostream>
Using namespace std;
int main()
{
int a;

cout<<"required sequence of number is";

for(a=1;a<=100;a++)
{
cout<<a<<"\t";
}
return 0;
}

Output

(b)Modify your program in part (a) to make it prints odd numbers within 1 to 100.

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Source code

#include <iostream>
using namespace std;

int main()
{
//print odd numbers

for (int a = 1; a <=100;a+=2)


cout<<a<<"\t";

return 0;
}

Output

(c)Write a C++ Program that receives a positive integer (N) and prints a series of numbers from 1 to
N (inclusive) in ascending order.
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Source code

#include <iostream>
using namespace std;

int main()
{
//print n numbers
int n;
cout<<"enter number :";
cin>>n;
for (int a = 1; a <=n;a++)
cout<<a<<"\n";

return 0;
}

Output

d) Write a C++ Program that displays a series of alphabets in descending order


from ‘Z’ to ‘A’.
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

source code

using namespace std;

int main()
{
char c;
for(c='z';c>='a';c--)
cout<<c;
}

Output

e) Modify your program in part (d) so that the program will display consonants only,

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

no vowels.

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

f) Write a C++ program that receives start value and end value. Then, your
program will display a series of numbers from the start value to the end value

source code

int v1 ,v2;

cout<<"starting value should be greater than last value\n";

cout<<"enter starting value : ";

cin>>v1;

cout<<"enter last value : ";

cin>>v2;

cout<<endl;

for(v1;v1<=v2;v1++)

{cout<<v1;}

return 0;

Exercise 3

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Write a C++ program that take two numbers from user, min and max. Use do-while to
make sure that the user enters the smaller value first. Then your program will compute the sum
of all integer numbers from min to max. Use do While loop.
Sorce code

#include <iostream>

using namespace std;

int main()

int min, max, sum, v;

do

cout << "Enter min and max value: \n";

cin >> min >> max;

if (min >= max)

cout << "Wrong input please try again." << endl << endl;

while (max <= min);

//cout<<"press any key to get sum :";

v = min;

do

sum = sum + v++;

}
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

while (v <= max);

cout << "sum of values from " << min << " to " << max << " is : " << sum;

return 0;

Source code

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 4
Write a C++ program that takes a positive integer from user and store it in variable
posNumber. Follow these conditions;
If the number is less than 1, print wrong input.
If it is 1, Print its value.
If value is greate than 1, check the value is it Even or Odd.
If it is Even, half it and print.
If it is Odd, multiply it by 3 and print result.
Repeat the whole process until user enter 1.
Source code

#include <iostream>

using namespace std;

int main()

int posNumber;

do {

cout << "Enter a integer value >= to 1:";

cin >> posNumber;

if (posNumber < 1) {

cout << "Wrong input, please try again." << endl;

} else {

if (posNumber == 1) {

cout << "Value is: " << posNumber << endl;

break;

if (posNumber % 2 == 0)

cout << "Value is: " << posNumber / 2 << endl;

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

else

cout << "Value is: " << posNumber * 3 << endl;

} while(1==1);

return 0;

Output

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 5
Write a C++ program that receives the total number of integers (N). Then, the program will ask
for N real numbers. By applying for loop, your program should find and display the largest and
the lowest of the numbers. Give a proper message for invalid user input
source code

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement

You might also like