Day 5 Sample code
Using do-while to repeat program code
int main() {
int x, y;
char done;
do {
cout << "Input two numbers:";
cin >> x >> y;
cout << "Sum:" << x + y << endl;
cout << "Do again? (Type y to repeat, anything else to stop):";
cin >> done;
} while (done == 'y');
return 0;
}
Prime 1 - not quite right
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num){
if(num%i==0) { // checks if the number is fully divisible or not
count++;
}
i++;
}
if(count==0) {
cout<<"It is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
}
}
Prime correct (for 1)
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num){
if(num%i==0) { // checks if the number is fully divisible or not
count++;
}
i++;
}
if(num < 2 || count>0) {
cout << num <<" is not a Prime Number \n";
} else {
cout << num <<" is a Prime Number \n";
}
}
Prime faster
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num && num%i!=0){
i++;
}
if (i==num) {
cout<<"It is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
cout<<"The Number " << num << " is divisable by " << i << "\n";
}
}
Prime with multiway
#include<iostream>
using namespace std;
int main() {
int num,count=0;
cout<<"Enter the number to check : ";
cin>>num;
int i = 2;
while (i<num && num%i!=0){
i++;
}
if (num<2) {
cout << num << " is not prive by definition";
} else if (i==num) {
cout << num <<" is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
cout<<"The Number " << num << " is divisable by " << i << "\n";
}
}
Prime with for statement
#include<iostream>
using namespace std;
int main() {
int num,count=0;
bool prime = true;
cout<<"Enter the number to check : ";
cin>>num;
for(int i=2;i<num;i++){
if(num%i==0) { // checks if the number is fully divisible or not
prime = false;
}
}
if(prime) {
cout<<"It is a Prime Number \n";
} else {
cout<<"It is not a Prime Number \n";
}
}
Prime with break
#include<iostream>
using namespace std;
int main() {
int num,count=0;
bool prime = true;
cout<<"Enter the number to check : ";
cin>>num;
if (num > 1) {
for(int i=2;i<num;i++){
if(num%i==0) { // checks if the number is fully divisible or not
prime = false;
break;
}
}
if(prime) {
cout<<num << " is a Prime Number \n";
} else {
cout<<num << " is not a Prime Number \n";
}
} else {
cout << num << " is not prime by definition";
}
}
Prime using square root
#include<iostream>
using namespace std;
#include <math.h>
int main() {
int num,count=0;
bool prime = true;
cout<<"Enter the number to check : ";
cin>>num;
int max = sqrt(num);
if (num > 1) {
for(int i=2;i<=max;i++){
if(num%i==0) { // checks if the number is fully divisible or not
prime = false;
break;
}
}
if(prime) {
cout<<num << " is a Prime Number \n";
} else {
cout<<num << " is not a Prime Number \n";
}
} else {
cout << num << " is not prime by definition";
}
}
Prime list
#include<iostream>
using namespace std;
#include <math.h>
int main() {
int max,count=0;
bool prime = true;
cout<<"Enter the maximum number to check : ";
cin>>max;
for (int j=2 ; j<=max; j++) {
prime = true;
for(int i=2;i<= sqrt(j);i++){
if(j%i==0) {
prime = false;
break;
}
}
if(prime) {
cout << j << " ";
}
}
cout << "\nDone listing primes less than or equal to " << max << ".\n";
}
Find Factors
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n;
bool prime = true;
cout << "Enter a number to find it's factors:";
cin >> n;
cout << "Factors: ";
for (int i = 2; i<= sqrt(n) ; i++) {
if (n%i == 0) {
cout << i << " ";
prime = false;
}
}
if (prime) {
cout << "Prime, no factor other then 1 and " << n << endl;
}
return 0;
}
Switch Statement (grades)
DISPLAY 3.6 A switch Statement
//Program to illustrate the switch statement.
#include <iostream>
using namespace std;
int main( )
{
char grade;
cout << "Enter your midterm grade and press Return: ";
cin >> grade;
switch (grade)
{
case 'A':
cout << "Excellent. "
<< "You need not take the final.\n";
break;
case 'B':
cout << "Very good. ";
grade = 'A';
cout << "Your midterm grade is now "
<< grade << endl;
break;
case 'C':
cout << "Passing.\n";
break;
case 'D':
case 'F':
cout << "Not good. "
<< "Go study.\n";
break;
default:
cout << "That is not a possible grade.\n";
}
cout << "End of program.\n";
return 0;
}
A Menu
//DISPLAY 3.7 A Menu
//Program to give out homework assignment information.
#include <iostream>
using namespace std;
int main( )
{
int choice;
do
{
cout << endl
<< "Choose 1 to see the next homework assignment.\n"
<< "Choose 2 for your grade on the last assignment.\n"
<< "Choose 3 for assignment hints.\n"
<< "Choose 4 to exit this program.\n"
<< "Enter your choice and press Return: ";
cin >> choice;
switch (choice)
{
case 1:
//code to display the next assignment on screen would go here.
break;
case 2:
//code to ask for a student number and give the corresponding
//grade would go here.
break;
case 3:
//code to display a hint for the current assignment would go
//here.
break;
case 4:
cout << "End of Program.\n";
break;
default:
cout << "Not a valid choice.\n"
<< "Choose again.\n";
}
}while (choice != 4);
return 0;
}
Local Variables
//DISPLAY 3.8 Block with a Local Variable
//Program to compute bill for either a wholesale or a retail purchase.
#include <iostream>
using namespace std;
const double TAX_RATE = 0.05; //5% sales tax.
int main( )
{
char sale_type;
int number;
double price, total;
cout << "Enter price $";
cin >> price;
cout << "Enter number purchased: ";
cin >> number;
cout << "Type W if this is a wholesale purchase.\n"
<< "Type R if this is a retail purchase.\n"
<< "Then press Return.\n";
cin >> sale_type;
if ((sale_type = = 'W') || (sale_type = = 'w'))
{
total = price * number;
}
else if ((sale_type = = 'R') || (sale_type = = 'r'))
{
double subtotal;
subtotal = price * number;
total = subtotal + subtotal * TAX_RATE;
}
else
{
cout << "Error in input.\n";
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << number << " items at $" << price << endl;
cout << "Total Bill = $" << total;
if ((sale_type = = 'R') || (sale_type = = 'r'))
cout << " including sales tax.\n";
return 0;
}
For Statement
//DISPLAY 3.12 A for Statement
//Illustrates a for loop.
#include <iostream>
using namespace std;
int main( )
{
int sum = 0;
for (int n = 1; n <= 10; n++) //Note that the variable n is a local
sum = sum + n; //variable of the body of the for loop!
cout << "The sum of the numbers 1 to 10 is "
<< sum << endl;
return 0;
}
break Statement
//DISPLAY 3.14 A break Statement in a Loop
//Sums a list of ten negative numbers.
#include <iostream>
using namespace std;
int main( )
{
int number, sum = 0, count = 0;
cout << "Enter 10 negative numbers:\n";
while (++count <= 10)
{
cin >> number;
if (number >= 0)
{
cout << "ERROR: positive number"
<< " or zero was entered as the\n"
<< count << "th number! Input ends "
<< "with the " << count << "th number.\n"
<< count << "th number was not added in.\n";
break;
}
sum = sum + number;
}
cout << sum << " is the sum of the first "
<< (count - 1) << " numbers.\n";
return 0;
}
Explicitly Nested Loops
//DISPLAY 3.15 Explicitly Nested Loops
//Determines the total number of green-necked vulture eggs
//counted by all conservationists in the conservation district.
#include <iostream>
using namespace std;
void instructions ();
int main()
{
cout << "This program tallies conservationist reports\n"
<< "on the green-necked vulture.\n"
<< "Each conservationist's report consists of\n"
<< "a list of numbers. Each number is the count of\n"
<< "the eggs observed in one๎
<< "green-necked vulture nest.\n"
<< "This program then tallies
<< "the total number of eggs.\n";
int number_of_reports;
cout << "How many conservationist reports are there? ";
cin >> number_of_reports;
int grand_total = 0, subtotal, count;
for (count = 1; count <= number_of_reports; count++)
{
cout << endl << "Enter the report of "
<< "conservationist number " << count << endl;
cout << "Enter the number of eggs in each nest.\n"
<< "Place a negative integer at the end of your list.\n๎;๎
subtotal = 0;
int next;
cin >> next;
while (next >=0)
{
subtotal = subtotal + next;
cin >> next;
}
cout << "Total egg count for conservationist "
<< " number " << count << " is "
<< subtotal << endl;
grand_total = grand_total + subtotal;
}
cout << endl << "Total egg count for all reports = "
<< grand_total << endl;
return 0;
}
x
x
#include<iostream>
using namespace std;
int main() {
int score;
cout << "Enter the grade score? ";
cin >> score;
if (score >= 90) {
cout << "Excellent! you got an A!!!!!!" << endl;
} else if (score >= 80) {
cout << "Pretty good. YOu got a B!" << endl;
} else if (score >= 70) {
cout << "Ok - you got a C" << endl;
} else if (score >= 60) {
cout << "Just passing, you got a D :(" << endl;
} else {
cout << "Bad news, failure F" << endl;
}
}