0% found this document useful (0 votes)
31 views

Answers: Source Code (Edit The Source Code Below and Paste Your Updated Program)

The document provides instructions for a programming assignment to write a C++ program that allows a user to enter a number and perform various calculations and outputs based on their choice of transaction. The program is to include a switch statement to direct the user to different cases for ascending, descending, summation, and product calculations from 1 to the entered number using do-while and while loops. The student is provided a template code to edit and asked to output screenshots of the results for all cases from 1 to 8.
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)
31 views

Answers: Source Code (Edit The Source Code Below and Paste Your Updated Program)

The document provides instructions for a programming assignment to write a C++ program that allows a user to enter a number and perform various calculations and outputs based on their choice of transaction. The program is to include a switch statement to direct the user to different cases for ascending, descending, summation, and product calculations from 1 to the entered number using do-while and while loops. The student is provided a template code to edit and asked to output screenshots of the results for all cases from 1 to 8.
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/ 5

ASSESSMENT TASK: Iterative or Repetition Control Structure using switch, while and do while loop

The following question support the attainment of Course Intended Learning Outcomes (CILO): Design
computing based solution using control structures, functions, array and other statements

INSTRUCTION: Write a program the will let the user to enter a number and display the output based
on CHOICES below.

ANSWERS: SOURCE CODE (EDIT THE SOURCE CODE BELOW AND PASTE YOUR UPDATED PROGRAM)

#include <iostream>

using namespace std;

main()
{
int num, ctr, trans = 0;
int sum, prod;

cout << "****************************\n";


cout << "* Name: Rachelle Lynne Cui *\n";
cout << "* Section: IT11S6 *\n";
cout << "****************************\n";

cout << "\n\n Enter a number: ";


cin >> num;
cout << "\n Transaction";
cout << "\n 1.Display ascending order from 1 to " << num << " using do while";
cout << "\n 2.Display ascending order from 1 to " << num << " using while";
cout << "\n 3.Display descending order from " << num << " to 1 using while";
cout << "\n 4.Display descending order from " << num << " to 1 using do while";
cout << "\n 5.Display all numbers divisible by 3 from 1 to " << num << " using do while";
cout << "\n 6.Display all numbers divisible by 8 from 1 to " << num << " using while";
cout << "\n 7.Display the sum of 1 to " << num << " using do while";
cout << "\n 8.Display the product of 1 to " << num << " using while";

do{
cout << "\n\n Enter transaction: ";
cin >> trans;
switch (trans){
case 1:
ctr = 1;
do{
cout << ctr << " ";
ctr++;
} while (ctr <= num);
break;
case 2:
ctr = 1;
while (ctr <= num){
cout << ctr << " ";
ctr++;
}
break;
case 3:
ctr = num;
while (ctr >= 1){
cout << ctr << " ";
ctr--;
}
break;
case 4:
ctr = num;
do{
cout << ctr << " ";
ctr--;
} while (ctr >= 1);
break;
case 5:
ctr = 1;
do{
if (ctr % 3 == 0){
cout << ctr << " ";
}
ctr++;
} while (ctr <= num);
break;
case 6:
ctr = 1;
while (ctr <= num){
if (ctr % 8 == 0){
cout << ctr << " ";
}
ctr++;
}
break;
case 7:
ctr = 1;
sum = 0;
do{
sum = sum + ctr;
ctr++;
} while (ctr <= num);
cout << "The sum is " << sum;
break;
case 8:
ctr = 1;
prod = 1;
while (ctr <= num){
prod = prod * ctr;
ctr++;
}
cout << "The product is " << prod;
break;
default:
cout << "Invalid";
} //end of switch
} while (trans <= 8); // end of do while
} //end of main

OUTPUT (SCREEN SHOT all output from case 1 to 8)

You might also like