Intro to C++
 Session 6- Four Sample C++ Programs
By Coding-Bootcamps.com
###########################################
###########################################
###########################################
for, while, do while and nested loop examples
###########################################

Program # 1  (for loop) : Day of Date 
//Program to find out the day of given date starting from Jan. 2001
#include <iostream>
using namespace std;
int main()
{
	// Collect data from user
		int day, month, year;
		cout << "Enter the day dd(01)= ";
		cin >> day;
		cout << "Enter the month mm(01)= ";
		cin >> month;
		cout << "Enter the year yy from (2001)= ";
		cin >> year;
	// Loop through given year's months passed so far and count days
		int dayCount = 0;
		for (int i = 1; i < month; i++)
		{
			// Month(s) with 31 days
			if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
				dayCount = dayCount + 31;
			// Month(s) with 28 days
			else if (i == 2 && year % 4 != 0)
				dayCount = dayCount + 28;
			// Month(s) with 29 days (leap)
			else if (i == 2 && year % 4 == 0)
				dayCount = dayCount + 29;
			// Month(s) with 30 days
			else if (i == 4 || i == 6 || i == 9 || i == 11)
				dayCount = dayCount + 30;
		}
	// Loop through years and count leap years
		int leapYearCount = 0;
		for (int yearIterator = 2001; yearIterator < year; yearIterator++)
		{
			if (yearIterator % 4 == 0)
				leapYearCount = leapYearCount + 1;
		}
	// Count years
		int yearCount;
		yearCount = (year-2000)-1;
	// Calculate total day count
		int totalDayCount, resultingDayNum;
		/* Add up:
		- days in current month
		- dayCount from previously elapsed days in the year
		- leap year count
		- year count * 365 days */
		totalDayCount = day + dayCount + leapYearCount + (yearCount * 365);
		resultingDayNum = totalDayCount % 7;
	// Translate day number to text
		switch (resultingDayNum)
		{
		case 0: cout << "The day is Sun"; break;
		case 1: cout << "The day is Mon"; break;
		case 2: cout << "The day is Tue"; break;
		case 3: cout << "The day is Wed"; break;
		case 4: cout << "The day is Thu"; break;
		case 5: cout << "The day is Fri"; break;
		case 6: cout << "The day is Sat"; break;
		}
	return 0;
}
###########################################

Program # 2 (while loop): Colorful Text
//Program to display the text in different colors
#include <stdlib.h>
#include <iostream>
using namespace std;

int main() {
	int i = 0;
	while (i < 20) {
		if (i % 2 == 0) {
			system("Color F2");
		}
		else {
			system("Color F1");
		}
		cout << "The value of the iterator tracker i is: " << i << endl;
		i++;
	}
	return 0;
}
###########################################

Program # 3 (do while loop): Prime Number
//Program to find out the given number is prime (only divisible by 1 and itself)
#include <iostream>
using namespace std;

int main() {
	int number, countOfNumbersDivisibleBy = 0;
	cout << "Please enter a number (any number): ";
	cin >> number;
	int i = 1;
	do {
		cout << "Checking if " << number << " is divisible by " << i << endl;
		if (number % i == 0) {
			countOfNumbersDivisibleBy++;
			cout << "It is divisible!" << endl;
		}
		i++;
	} while(i <= number);
	if (countOfNumbersDivisibleBy == 2) {
		cout << "The number is prime." << endl;
	}
	else {
		cout << "The number is not prime." << endl;
	}
	return 0;
}
###########################################

Program # 4 (Nested Loop): Factors
//Program to find out the factors of given number
#include <iostream>
using namespace std;
int main()
{
	int number;
	cout << "Enter any number = ";
	cin >> number;
	// LOOP 1
	// Test out different factors
	for (int i = 1; i <= 100; i++) {
		// Track loop 1
		cout << "Loop i value: " << i << endl;
		// LOOP 2
			// Loop through 2 to the number of the factor
			for (int j = 2; j <= i; j++) {
				//Track loop 2
				cout << "Loop j value: " << j << endl;
				// Check if number is evenly divisible if so then factor found
				cout << "Checking if " << number << " is divisible by " << j << "." << endl;
				if (number % j == 0)
				{
					cout << j << " is a factor!" << endl;
					number = number / j;
				}
				if (number == 1) {
					cout << "1 is a factor!" << endl;
					break;
				}
			}
			if (number == 1) {
				cout << "1 is a factor!" << endl << "Factor search over.";
				break;
			}
	}
	return 0;
}


###########################################

