Exercise 1: Source Code
Exercise 1: Source Code
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>
int main()
int n;
cin>>n;
{ cout<<b;}
cout<<endl;
return 0;}
OUTPUT
Exercise 2
By applying for loop, write a C++ program that prints
Source code
#include<iostream>
Using namespace std;
int main()
{
int a;
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.
Source code
#include <iostream>
using namespace std;
int main()
{
//print odd numbers
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
source code
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,
no vowels.
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;
cin>>v1;
cin>>v2;
cout<<endl;
for(v1;v1<=v2;v1++)
{cout<<v1;}
return 0;
Exercise 3
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>
int main()
do
cout << "Wrong input please try again." << endl << endl;
v = min;
do
}
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)
cout << "sum of values from " << min << " to " << max << " is : " << sum;
return 0;
Source code
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>
int main()
int posNumber;
do {
if (posNumber < 1) {
} else {
if (posNumber == 1) {
break;
if (posNumber % 2 == 0)
else
} while(1==1);
return 0;
Output
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