Programming Lab 9
Question 1
#include <iostream>
using namespace std;
int main()
int num = 12;
do{
cout << num<<endl;
num = num + 2;
while(num<=28);
return 0;
Question 2
#include <iostream>
using namespace std;
int main()
int num = 12;
while (num<=28)
cout<<num<<endl;
num = num + 2;
}
return 0;
Question 3
#include <iostream>
using namespace std;
int main()
cout<<"Enter the number of times to output the statement, 'I am sorry'";
int numtimes;
cin >> numtimes;
int x = 0;
while (numtimes>x){
x = x + 1;
cout<<"I am sorry"<<endl;
return 0;
Question 4
#include <iostream>
using namespace std;
int main()
int age;
cout << "Enter your age: ";
cin >> age;
int x = 1;
do {
cout << "Happy Birthday! (Year " << x << ")"<<endl;
x = x+1;
} while (x <= age);
return 0;
Question 5
#include <iostream>
using namespace std;
int main()
int sum = 0;
int x = 73;
do
sum = sum + x;
x= x +1;
while (x <= 415);
cout << "Sum for the do-while loop: " << sum << endl;
sum = 0;
for (int x = 73; x <= 415; x=x+1) {
sum = sum+x ;
}
cout << "Sum for the for loop: " << sum << endl;
return 0;
Question 6
#include <iostream>
using namespace std;
int main ()
for(int K = 5, W = -2, P; W<= K; W = W + 2, K--)
P=W+K;
cout << P << endl;
return 0;
Question 7
#include <iostream>
using namespace std;
int main()
int num;
int product = 1;
do
cout << "Enter a number: ";
cin >> num;
if (num > 0) {
product = product * num;
while (num > 0);
cout << "The product of all your positive numbers is " << product << "." << endl;
return 0;
Question 8
#include <iostream>
using namespace std;
int main()
int num;
int sum = 0;
double count = 0;
do
{
cout << "Enter a positive integer (or 0 to end the operation): ";
cin >> num;
if (num < 0) {
cout << "ERROR! Kindly enter a positive integer." << endl;
else if (num > 0) {
sum = sum + num;
count= count + 1;
} while (num != 0);
if (count > 0) {
cout << "Average of positive integers: " << (sum) / count << endl;
} else {
cout << "NO AVERAGE" << endl;
return 0;
Question 9
#include <iostream>
using namespace std;
int main()
const int correctpin = 7251;
int attempttrials = 0;
int input_pin;
while (attempttrials < 4) {
cout << "Input your PIN number: ";
cin >> input_pin;
switch (input_pin) {
case correctpin:
cout << "PIN verified. Welcome!" << endl;
return 0;
default:
attempttrials= attempttrials + 1;
if (attempttrials == 4) {
cout << "Limit expired. The program will exit now." << endl;
return 0;
} else {
cout << "Pin is incorrect. Please try again." << endl;
break;
if (input_pin == correctpin) {
break;
return 0;