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

C++ Program Examples: Even/Odd, Digit Check, Astronaut Eligibility, Water Usage

The document contains code snippets from multiple C++ programs. Program 1 checks if a user-input number is even or odd. Program 2 checks if a user-input character is a digit. Program 3 checks if a user's attributes meet requirements to be an astronaut. Program 4 calculates water bill costs based on gallons used, providing a discount for the first 800 gallons.

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)
35 views11 pages

C++ Program Examples: Even/Odd, Digit Check, Astronaut Eligibility, Water Usage

The document contains code snippets from multiple C++ programs. Program 1 checks if a user-input number is even or odd. Program 2 checks if a user-input character is a digit. Program 3 checks if a user's attributes meet requirements to be an astronaut. Program 4 calculates water bill costs based on gallons used, providing a discount for the first 800 gallons.

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

PRACTICAL 5

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

int main(void)
{
int num, even_num, odd_num;

cout << "Please enter a number:";


cin >> num;

if ((num % 2) == 0)
{
cout << num << " is an even number"<<endl;
}
else
{
cout << num << " is an odd number"<<endl;
}
return 0;
}

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

bool isDigit(char ch);

int main(void)
{
char ch;
bool answer;

cout << "Please enter a input: ";


cin >> ch;
answer = isDigit(ch);

if (answer!=0)
{
cout << ch << " is a digit" << endl;
}
else
{
cout << ch << " is not a digit" << endl;
}
return 0;
}

bool isDigit(char ch)


{
return('0' <= ch && ch <= '9');
}

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

int main(void)
{
int age;
double weight;
char smoker;

cout << "Want to be an astronaut?\n";


cout << "Please answer the following questions: " << endl;
cout << "What is your age? ";
cin >> age;
cout << "What is your weight? ";
cin >> weight;
cout << "Are you a smoker (y or n)? ";
cin >> smoker;

if ((35 <= age <= 40) && (50.0 <= weight <= 80.0) && (smoker == 'n'))
{
cout << "Congraturation, we can accept you as an astronaut!"<<endl<<endl;
}
else
{
cout << "Sorry, we cannot accept you as an astronaut!"<<endl<<endl;
}
return 0;
}
OUTPUT
PART D
QUESTION 1
INPUT
#include <iostream>
#include <iomanip>
using namespace std;
#define CHARGE_PER_GALLONS 1000

int main(void)
{
double beginning, ending, gallon_used, total_cost;

cout << "Please enter how many gallons of water in the beginning? ";
cin >> beginning;
cout << "Please enter how many gallons of water in the ending? ";
cin >> ending;

gallon_used = ending - beginning;


cout << "The amount of water used is " << gallon_used;
cout << endl << endl;

if (gallon_used <= 800)


{
cout << "You will get 100% discount, Thank you for saving precious water
resource!\n\n";
}
else
{
total_cost = (((gallon_used - 800)/(CHARGE_PER_GALLONS))*(0.13));
cout << "You will get 100% discount for the first 800 gallons used,";
cout << "the balance you have to pay is RM "
<<fixed<<setprecision(2)<<total_cost;
}

return 0;
}

OR
OUTPUT

You might also like