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

c++ starting programs of book

Uploaded by

Vasu Goyal
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)
44 views

c++ starting programs of book

Uploaded by

Vasu Goyal
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/ 5

Stone, Paper, Scissors Game

#include <iostream>
using namespace std;

int main() {
int player1, player2;
char playAgain;

do {
cout<<"Player1: enter 1 for scissors, 2 for paper, 3 for stone";
cin>> player1;

cout<<"Player2: enter 1 for scissors, 2 for paper, 3 for stone";


cin>>player2;

if (player1 == player2) {
cout << "It's a tie!" << endl;
} else if ((player1 == 1 && player2 == 2) || (player1 == 2 && player2 == 3)
|| (player1 == 3 && player2 == 1)) {
cout << "Player 1 won!" << endl;
} else {
cout << "Player 2 won!" << endl;
}
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
} while ((playAgain=='y') || (playAgain=='Y'));
cout<<"game over";

return 0;
}

Stone, Paper, Scissors Game using switch


#include <iostream>
using namespace std;

int main() {
int player1, player2;
char playAgain;

do {
// Prompt and input for Player 1
cout << "Player 1, enter 1 for Scissors, 2 for Paper, or 3 for Stone: ";
cin >> player1;

// Prompt and input for Player 2


cout << "Player 2, enter 1 for Scissors, 2 for Paper, or 3 for Stone: ";
cin >> player2;

// Calculate the difference


int difference = player1 - player2;

// Use switch for decision-making


switch (difference) {
case 0:
cout << "It's a tie!" << endl;
break;
case -1:
case 2:
cout << "Player 1 won!" << endl;
break;
default:
cout << "Player 2 won!" << endl;
break;
}

// Ask if players want to play again


cout << "Do you want to play again? (y/n): ";
cin >> playAgain;

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

cout << "Game ended. Goodbye!" << endl;

return 0;
}

Conversion table of grams and ounces


#include <iostream>
using namespace std;

int main() {
double grams;

// Version 2: Display a conversion table for 1 to 10 ounces


cout << "Conversion Table: Ounces to Grams" << endl;
cout << "--------------------------------" << endl;
cout << " Ounces Grams" << endl;

for (int ounces = 1; ounces <= 10; ounces++) {


grams = ounces * 28.3495; // Conversion factor: 1 ounce = 28.3495 grams
cout << " " << ounces << " " << grams << endl;
}

return 0;
}

Conversion table for pounds and dollars


#include <iostream>
using namespace std;

int main() {
double conversionRate;

// Prompt the user to enter the currency conversion rate


cout << "Enter the currency conversion rate (pounds per dollar): ";
cin >> conversionRate;

// Display the table header


cout << "\nDollar\tPound" << endl;
cout << "----------------" << endl;

// Display the conversion table up to 10 pounds


for (int dollars = 1; dollars <= 10; dollars++) {
double pounds = dollars * conversionRate;

// Display the conversion values


cout << dollars << "\t" << pounds << endl;
// Check if the pounds exceed 10, and break out of the loop if true
if (pounds >= 10.0) {
cout << "\nTable stopped at 10 pounds." << endl;
break;
}
}

return 0;
}

Positive integer multiplication table


#include <iostream>
using namespace std;

int main() {
int number;

do {
// Prompt the user for a positive integer
cout << "Enter a positive integer (or 0 to exit): ";
cin >> number;

if (number > 0) {
// Display the times table for the entered integer
cout << "Times Table for " << number << ":" << endl;

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


cout << number << " * " << i << " = " << number * i << endl;
}

cout << endl; // Add a newline for better readability


} else if (number < 0) {
cout << "Please enter a positive integer." << endl;
}

} while (number != 0);

cout << "Program terminated. Goodbye!" << endl;

return 0;
}

Age calculator in 10,20,30,40 and 50 years old


#include <iostream>
using namespace std;

int main() {
int age;

cout << "What is your age right now? ";


cin >> age;

if (age > 18 && age < 40) {


for (int i = 1; i <= 5; i++) {
cout << "Your age in " << (i * 10) << " years will be: " << (age + i *
10) << endl;
}
} else {
cout << "Sorry, this program only operates for ages in the range of 18 to
40." << endl;
}

return 0;
}

Loops and arrays


#include <iostream>
#include <limits> // Required for std::numeric_limits
using namespace std;

int main() {
// Declare and initialize the array
float values[] = {2.6, 3.4, 5.7, 1.855, 10.7, 20.4, 4.621, 7.86, 10.7, 20.56,
13.23, 11.653, 6.48};

// Calculate the total


float total = 0.0;
int size = sizeof(values) / sizeof(values[0]);

for (int i = 0; i < size; i++) {


total += values[i];
}

// Calculate the mean average


float meanAverage = total / size;

// Find the highest and lowest values


float highest = std::numeric_limits<float>::lowest(); // Initialize with lowest
possible float value
float lowest = std::numeric_limits<float>::max(); // Initialize with highest
possible float value

for (int i = 0; i < size; i++) {


if (values[i] > highest) {
highest = values[i];
}

if (values[i] < lowest) {


lowest = values[i];
}
}

// Display the results


cout << "Values: ";
for (int i = 0; i < size; i++) {
cout << values[i] << " ";
}

cout << "\nTotal: " << total << endl;


cout << "Mean Average: " << meanAverage << endl;
cout << "Highest Value: " << highest << endl;
cout << "Lowest Value: " << lowest << endl;

return 0;
}

Function usage:
#include <iostream>
using namespace std;

const int ARRAY_LENGTH = 10;

// Function to limit array values


void Limit(int arr[], int length, int limit) {
for (int i = 0; i < length; ++i) {
if (arr[i] > limit) {
arr[i] = limit;
}
}
}

// Function to display values that exceed the limit


void DisplayBig(int arr[], int length, int limit) {
cout << "Values exceeding " << limit << ": ";
for (int i = 0; i < length; ++i) {
if (arr[i] > limit) {
cout << arr[i] << " ";
}
}
cout << endl;
}

int main() {
// Declare and initialize the array with 10 numbers between 1 and 50
int numbers[ARRAY_LENGTH] = {10, 25, 5, 38, 7, 42, 15, 30, 20, 49};
int limitValue = 30;

// Display values exceeding the limit before applying the Limit function
DisplayBig(numbers, ARRAY_LENGTH, limitValue);

// Call the Limit function with a limiting value


Limit(numbers, ARRAY_LENGTH, limitValue);

// Display values exceeding the limit after applying the Limit function
DisplayBig(numbers, ARRAY_LENGTH, limitValue);

// Display values exceeding the limit after the Limit function (should not
display anything)
DisplayBig(numbers, ARRAY_LENGTH, limitValue);

return 0;
}

You might also like