0% found this document useful (0 votes)
19 views9 pages

Looping

The document contains a lab assignment for CSC126, focusing on fundamental algorithms and problem-solving using C++. It includes ten programming problems that require the use of loops (while, do-while, and for) to perform various tasks such as displaying even numbers, printing messages, calculating averages, and finding the highest and lowest numbers from user input. Each problem is accompanied by a C++ code solution and expected output.

Uploaded by

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

Looping

The document contains a lab assignment for CSC126, focusing on fundamental algorithms and problem-solving using C++. It includes ten programming problems that require the use of loops (while, do-while, and for) to perform various tasks such as displaying even numbers, printing messages, calculating averages, and finding the highest and lowest numbers from user input. Each problem is accompanied by a C++ code solution and expected output.

Uploaded by

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

CSC126: Fundamentals of Algorithms & Computer Problem Solving

LAB

Name: SOFIA A’LIYA BINTI HAMIDON


Matric No: 2022483156
Group: CEEC1102F
Date: 10/5/2023

Instruction: Solve all problems below. Upload to google classroom if finish.

Problem 1

Using While, design a program that displays every even numbers from 10 through 20.

C++ Program

#include <iostream>
using namespace std;

int main ()
{
int count = 10;

while (count<=20)

{
cout<<count<<endl;
count+=2;
}

return 0;

Output
Problem 2

Using While, design a program that prints ‘I AM THE BEST’ for 10 times.

C++ Program
#include <iostream>
using namespace std;

int main() {
int count = 1;
while (count <= 10) {
cout << "I AM THE BEST\n";
count++;
}
return 0;
}

Output
Problem 3

Using While, write a program that displays multiples of 5 from 5 through 50. (5 10 15
20 ... 50)

C++ Program
#include <iostream>
using namespace std;

int main() {
int i = 5; // starting value
while (i <= 50) { // loop until 50 is reached
cout << i << " "; // display current value
i += 5; // increment by 5 for next value
}
return 0;
}

Output

Problem 4

Using While, write a program that displays the average of 10 numbers entered by user.

C++ Program
#include <iostream>
using namespace std;

int main() {
int count = 1;
double sum = 0;
double num;

while (count <= 10) {


cout << "Enter number " << count << ": ";
cin >> num;
sum += num;
count++;
}

double average = sum / 10;


cout << "The average of the 10 numbers is: " << average << endl;

return 0;
}

Output

Problem 5

Using While, design a program that calculates total of 5 numbers entered by user that can
be divisible by 5.

C++ Program
#include <iostream>
using namespace std;

int main() {
int count = 1;
int total = 0;
int num;

while (count <= 5) {


cout << "Enter number " << count << ": ";
cin >> num;
if (num % 5 == 0) {
total += num;
count++;
}
}

cout << "The total of the 5 numbers divisible by 5 is: " << total << endl;

return 0;
}

Output

Problem 6

Using While, write a program that counts the positive and negative numbers from 20
numbers entered by user.

C++ Program
#include <iostream>
using namespace std;

int main() {
int count = 1;
int positiveCount = 0;
int negativeCount = 0;
int num;

while (count <= 20) {


cout << "Enter number " << count << ": ";
cin >> num;
if (num > 0) {
positiveCount++;
} else if (num < 0) {
negativeCount++;
}
count++;
}

cout << "The total positive numbers entered is: " << positiveCount << endl;
cout << "The total negative numbers entered is: " << negativeCount << endl;

return 0;
}

Output

Problem 7

Using While, write a program to find the highest and lowest number among 10 numbers
entered by user.

C++ Program
#include <iostream>
using namespace std;

int main() {
int count = 1;
int highest = INT_MIN; // initialize highest to smallest possible integer value
int lowest = INT_MAX; // initialize lowest to largest possible integer value
int num;

while (count <= 10) {


cout << "Enter number " << count << ": ";
cin >> num;
if (num > highest) {
highest = num;
}
if (num < lowest) {
lowest = num;
}
count++;
}

cout << "The highest number entered is: " << highest << endl;
cout << "The lowest number entered is: " << lowest << endl;

return 0;
}

Output

Problem 8

Using While, design a coding for a program that prints numbers in reverse order as
follows: 100 90 80 70 60 50.

C++ Program
#include <iostream>
using namespace std;

int main() {
int num = 100;
while (num >= 50) {
cout << num << " ";
num -= 10;
}

return 0;
}
Output

Problem 9

Using do … while, design a coding for a program that prints numbers in reverse order as
follows: 100 90 80 70 60 50.

C++ Program
#include <iostream>
using namespace std;

int main() {
int num = 100;

do {
cout << num << " ";
num -= 10;
} while (num >= 50);

return 0;
}

Output
Problem 10

Using for, design a coding for a program that prints numbers in reverse order as follows:
100 90 80 70 60 50.

C++ Program
#include <iostream>
using namespace std;

int main() {
for(int num = 100; num >= 50; num -= 10) {
cout << num << " ";
}

return 0;
}

Output

You might also like