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;
}