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

while-loop-example

The document contains three C++ programs. The first program calculates and prints the cubic root of the first 10 integers using the pow function. The second program reads positive integers less than 1000, calculating their sum and average until the user inputs 1000, while the third program simulates a vending machine that accepts 5, 10, and 20 coins, resetting the total to zero when it reaches 100 and exiting on invalid input.

Uploaded by

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

while-loop-example

The document contains three C++ programs. The first program calculates and prints the cubic root of the first 10 integers using the pow function. The second program reads positive integers less than 1000, calculating their sum and average until the user inputs 1000, while the third program simulates a vending machine that accepts 5, 10, and 20 coins, resetting the total to zero when it reaches 100 and exiting on invalid input.

Uploaded by

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

Write a program that prints the cubic root of the first 10 integers using the pow

function in the <cmath> header. Tabulate your result with appropriate headers.

solution:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int integer;
double root;

cout<<"Enter integer: ";


cin>>integer;

while (integer--){
root=pow(integer,0.333);
cout<<root<<endl;
}

Write a program that reads a list of positive integers less than 1000 from a
keyboard. The program prints the sum and the average of the numbers. The
program will stop when the user types 1000 as a sentinel.

solution:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int integer, count=0, sum=0,ave=0;

while (integer!=1000){
cout<<"Enter another integer: ";
cin>> integer;

if(integer>=0&&integer<1000){
sum=sum+=integer;
count++;
ave=sum/count;
}

cout<<"\nSum: "<<sum<<endl<<"\nAverage: "<<ave;

A vending machine accepts only 5, 10, and 20 coins, accumulating their total until
it reaches 100, at which point it resets to zero and if an invalid coin is
inserted, the machine exits.

#include <iostream>
using namespace std;

int main() {
int coin;
int total = 0;

while (true) {
cout << "Insert coin (5, 10, 20): ";
cin >> coin;

// Check if the inserted coin is valid


if (coin == 5 || coin == 10 || coin == 20) {
total += coin; // Accumulate valid coins

cout << "Total: " << total << endl;

// Check if total reaches or exceeds 100


if (total >= 100) {
cout << "Limit reached! Resetting to zero." << endl;
total = 0; // Reset total
}
} else {
cout << "Invalid bill. Exiting program." << endl;
break; // Exit if an invalid coin is inserted
}
}

return 0;
}

You might also like