0% found this document useful (0 votes)
52 views11 pages

Part B Input

The document contains C++ code for several programs: 1) A program that calculates the average and total of a set of user-input numbers between 1-100. 2) A program that calculates the product of numbers from 10 to 1. 3) A program that calculates the product of user-input numbers from 10 to 1.

Uploaded by

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

Part B Input

The document contains C++ code for several programs: 1) A program that calculates the average and total of a set of user-input numbers between 1-100. 2) A program that calculates the product of numbers from 10 to 1. 3) A program that calculates the product of user-input numbers from 10 to 1.

Uploaded by

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

PART B

QUESTION 1
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int number; /* number to be added */
int count; /* count of numbers to be added */
int i; /* loop counter variable */
int total; /* total of the set of numbers */
double average; /* average of the set of numbers */

cout << "How many numbers to process? ";


cin >> count;

total = 0; //(a)(iii)

for (i = 1; i <= count; i++)


{
do
{
cout << "Enter number " << i << " : ";
cin >> number;
if (number < 1 || number >100)
cout << "Number must between 1 to 100\n";
} while (number < 1 || number >100);

total += number; //(a)(iv)


}
average = (double)total / count; //(a)(v)
cout << "Total is " << total << endl; //(a)(i)
cout << "Average is " << average << endl; //(a)(ii)

return 0;
}

OUTPUT
PART B
QUESTION 2
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int i, total;
total = 1;

for (i = 10; i >= 1; i--)


{
total *= i;
}

cout << "Total is " << total << endl; //(a)(i)


return 0;
}

OUTPUT
PART B
QUESTION 3
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int i, total,number;
total = 1;

for (i = 10; i >= 1; i--)


{
cout << "Enter number " << i << " : ";
cin >> number;

total *= number;
}

cout << "Total is " << total << endl; //(a)(i)


return 0;
}

OUTPUT
PART B
QUESTION 4
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int product, number, input;
product = 1;

cout << "Enter a number to multiply\n";


cout << "Press -1 if no more numbers\n";

cout << "Enter the first number: ";


cin >> number;

while (number != -1)


{
product *= number;
cout << "Enter next number: ";
cin >> number;
}

cout << "Product is " << product << endl;


return 0;
}

OUTPUT
PART B
QUESTION 5
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int i, total, number;
char option;
total = 1;
do
{
cout << "Enter number : ";
cin >> number;
total *= number;
cout << "Do you have anymore number?\n";
cin >> option;

} while (option == 'Y' || option == 'y');

cout << "Total is " << total << endl; //(a)(i)


return 0;
}

OUTPUT
PART B
QUESTION 6
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int product,number,status;
product = 1;
cout << "Enter the numbers to multiply.\n";
cout << "Press cntrl-z when no more number.\n";

cout << "Enter number : ";


cin >> number;

while (cin)
{
product *= number;
cout << "Enter next number: ";
cin >> number;

cout << "Product is " << product << endl;


return 0;
}

OUTPUT
PART B
QUESTION 7 AND QUESTION 8
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int lower, upper, total, i;
total = 0;

do
{
cout << "Please enter lower limit: ";
cin >> lower;
cout << "Please enter upper limit: ";
cin >> upper;

if (lower >= upper)


cout << "lower limi must be less than upper limit\n";
} while (lower >= upper);

for (i = lower; i <= upper;i++)


total += i;
cout << "Sum of the numbers from" << lower << "to" << upper << "is" << total <<
endl;

return 0;
}

OUTPUT
PART B
QUESTION 9
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int max,i, number;

cout << " Enter 10 number\n";

for (i = 1; i <= 10; i++)


{
cout << "Enter number " << i << ":";
cin >> number;

if (i == 1)
max = number;
else
if (max < number)
max = number;
}

cout << "The largest number is"<<max << endl;

return 0;
}

OUTPUT
PART D
QUESTION 1
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int average, number, total=0,counter=0, min,max;

cout << "Enter a series of integer\n";


cout << "Press 0 if no more numbers\n";

cout << "Enter the number: ";


cin >> number;
while (number != 0)
{
if (counter == 0)
{
max = number;
min = number;
}

else
{
if (number > max)
max = number;
else if (number < min)
min = number;
}

counter++;
total += number;
cin >> number;
}

cout << "\n You have entered " << counter << "integers\n";
if (counter > 0)
{
cout << "The average is" << (double)total / counter << endl;
cout << "The maximum is" << max << endl;
cout << "The minimum is" << min << endl;
}

return 0;
}
OUTPUT
PART D
QUESTION 2
INPUT
#include<iostream>
#include<iomanip>
using namespace std;

int main(void)
{
int i, j, k;
for (i = 1;i <= 5;i++)
{
for (k = 5 - i;k >= 1;k--)
cout << " ";
for (j = i;j >= 1;j--)
cout << setw(3) << j;
cout << endl;
}
for (i = 4;i >= 51;i--)
{
for (k = 5 - i;k >= 1;k--)
cout << " ";
for (j = i;j >= 1;j--)
cout << setw(3) << j;
cout << endl;
}
return 0;
}

OUTPUT

You might also like