c++ functions

#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;

int hexDec(const string& hex);
int hexCharToDecimal(char ch);
int main()
{
	cout << "enter a hex Number ";
	string hex;
	cin >> hex;

	cout << "The Decimal Number For Hex Is  " << hex
		<< " is " << hexDec(hex) << endl;
}
int hexDec(const string&  hex)
{
	int decimalValue = 0;
	for (unsigned i = 0; i < hex.length(); i++)
	{
		decimalValue = decimalValue * 16 + hexCharToDecimal(hex[i]);
	}
	return decimalValue;
	
}
int hexCharToDecimal(char ch)
{
	ch = toupper(ch);
	if (ch >= 'A' && ch <= 'F')
		return 10 + ch - 'A';
	else
		return ch - '0';
}
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;


int compute_profit(int quantity);

int main()
{
	int quantity = 1000;
	cout << "Quantity " << setw(9) << "Profit-per-item (in $)"<<endl;
    for ( ; quantity <= 10000; quantity+=1000)
    {
            cout<<quantity<< setw(9)<<compute_profit(quantity)<<endl;
    }
   
}
int compute_profit(int quantity)
{
    int profit = 0;
   

   
    if (quantity < 1000)
        profit += quantity; // * 1
    else
    {
        profit += 1000;
        quantity -= 1000;
        if (quantity < 4000)
            profit += quantity * 2;
        else
        {
            profit += 8000; // 4000 * 2
            quantity -= 4000;
            profit += quantity * 5;
        }
    }
    
    return profit;
}


#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;

int LCM(int n1, int n2);

int main()
{
	cout << "Enter first integer: ";
	 int n1;
	 cin >> n1;
	
		 cout << "Enter second integer: ";
	 int n2;
	 cin >> n2;
	
	
	
		cout << "Least Common Multiple for " << n1 <<
			" and " << n2 << " is " << LCM(n1,n2)<< endl;
}

 int LCM(int n1, int n2)
 {
	 int lcm = 1; 
	 int max = (n1 > n2) ? n1 : n2;
	
		 while (true)
		 {
			 if (max % n1 ==0 && max % n2 == 0)
			 {
				 lcm = max;
				 break;
			 }
			 else
				 max++;

		 }
	
		return lcm; // Return gcd
	 }




	
   




#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;

bool isArmstrong(int n);
int reversNumber(int n);
void isReversedArmstrong(int n);

int main()
{
	cout << "Enter first integer: ";
	int n1;
	cin >> n1;
	isReversedArmstrong(n1);
	

}

bool isArmstrong(int n)
{
	int temp = 0,armstrong, total = 0;
	armstrong = n;
	while (armstrong != 0)
	{
		temp = armstrong % 10;
		total += pow(temp, 3);
		armstrong /= 10;
	}
	if (total == n)
		return true;
	else
		return false;
 }
int reversNumber(int n)
{
	int reverse = 0, rem = 0;
	while (n != 0)
	{
		rem = n % 10;
		reverse = reverse * 10 + rem;
		n /= 10;
	}
	return reverse;
}
void isReversedArmstrong(int n)
{
	if (isArmstrong(n))
	{
		if(isArmstrong(reversNumber(n)))
			cout << "the Reversed "<< reversNumber(n)<<" number IS an Armstrong number ";
		else
			cout << "the Reversed " << reversNumber(n) << " number is NOT Armstrong number ";
	}
	else
		cout << "the number entered is NOT Armstrong number ";

}


	
   




#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;
bool isEven(int number);
bool isPalindrom(int number);

int main()
{
	
	int count = 0, number = 1;
	
	while (count <= 50)
	{

		if(isEven(number) == true && isPalindrom(number) == true)
		{
			count++;
			if (count % 5 == 0)
				cout << setw(8) << number << endl;
			else
				cout << setw(8) << number;
		}
		number++;
	}
	
		
	

}

bool isEven(int number)
{
	if (number % 2 == 0)
		return true;
	else
		return false;
	
}
bool isPalindrom(int number)
{
	int reverse = 0, rem = 0;
	int num = number;
	while (number != 0)
	{
		rem = number % 10;
		reverse = reverse * 10 + rem;
		number /= 10;
	}
	if (reverse == num)
		return true;
	else
		return false;
}


	
   




#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;


bool primeNumber(int n);
bool Emirp(int n);
int reversal(int n);
int recursive(int a, int b);

int main()
{
	
	int count = 0;
	int number = 13;
	while (count <= 100)
	{
		if (Emirp(number))
		{
			count++;
			if (count % 10 == 0)
				cout << setw(7) << number << endl;
			else
				cout << setw(7) << number;
		}
		number++;
	}
		

}
bool primeNumber(int n) {
	
		for (int i = 2; i <= n / 2; i++) {
			
			if (n % i == 0) {

				return false;
			}
		}
		return true;
}

bool Emirp(int n) {
	

	return primeNumber(n) && primeNumber(reversal(n));
	
}

int reversal(int n) {
	
		if (n < 10) {
			
				return n;
			
		}
		return recursive(n % 10, n / 10);
	
}

int recursive(int a, int b) {

	if (b < 1) {

		return a;

	}
	return recursive(a * 10 + b % 10, b / 10);
	
}



	
   




#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;


bool primeNumber(int n);
bool additivePrime(int n);
int sumOfDigits(int n);



int main()
{
	
	int count = 0;
	int number = 13;
	cout << setw(3) << "Prime number" << setw(20) << "Sum of its digits" << endl;
	for (int i = 2; i <= 70; i++)
	{
		if (primeNumber(i) && additivePrime(i))
			cout <<setw(1)<< i<<setw(15)<<sumOfDigits(i)<<endl;
	}
		

}
bool primeNumber(int n) {
	
		for (int i = 2; i <= n / 2; i++) {
			
			if (n % i == 0) {

				return false;
			}
		}
		return true;
}

bool additivePrime(int n)
{
	
	if (primeNumber(sumOfDigits(n)))
		return true;
	else
		return false;
}
int sumOfDigits(int n)
{
	int sum = 0, digit = 0;
	while (n != 0)
	{
		digit = n % 10;
		sum += digit;
		n /= 10;
	}
	return sum;
}






	
   




#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;



double sqrt(int n);


int main()
{
	
	long n;
	cin >> n;
	cout << "the approximated sqrt of the number " << n << " is " << sqrt(n);
}
double sqrt(long n)
{
	double lastGuess = 1;
	double nextGuess = (lastGuess + n / lastGuess) / 2;

	while (nextGuess - lastGuess > 0.0001)
	{
		nextGuess = lastGuess;
		(lastGuess + (n / lastGuess)) / 2;
	}
	lastGuess = nextGuess;
	return nextGuess = (lastGuess + n / lastGuess) / 2;

}
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;



double avgOfEvenPlaces(int number);
int reversal(int n);


int main()
{
	
	int number;
	cin >> number;
	cout << fixed<<setprecision(3)<<avgOfEvenPlaces(reversal(number));
}
double  avgOfEvenPlaces(int n)
{
	
	int digit, count = 1.0, countOfEven = 0.0;
	double sumOfEven = 0.0;
	while (n != 0)
	{
		digit = n % 10;
		n /= 10;
		
		if (count % 2 == 0)
		{
		  
			sumOfEven += digit; 
			countOfEven++;
		}
			count++;
	}
	
		return  sumOfEven / countOfEven;
	
}
int reversal(int n)
{
	int digits, revers = 0.0;
	while (n != 0)
	{
		digits = n % 10;
		revers = revers * 10 + digits;
		n /= 10;
	}
	return revers;
 }


#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;




string dayNameOfWeek(int dayOfWeek);
int numberOfLeapYearsSince1970(long year);
int getMonthFromDays(int year, int days);
int getNumOfDaysToReachThatMonth(int year, int month);
int getStartDay(int year, int month);
int getTotalNumberOfDays(int year, int month);
int getNumberOfDaysInMonth(int year, int month);
bool isLeapYear(int year);
string getMonthName(int month);




int main()
{
    

    // Obtain the total seconds since midnight, Jan 1, 1970
    long totalSeconds = time(0);

    // Compute the current second in the minute in the hour
    long currentSecond = (int)(totalSeconds % 60);

    // Obtain the total minutes
    long totalMinutes = totalSeconds / 60;

    // Compute the current minute in the hour
    long currentMinute = (int)(totalMinutes % 60);

    // Obtain the total hours
    long totalHours = totalMinutes / 60;

    // Compute the current hour
    long currentHour = (int)(totalHours % 24);

    long totalDays = totalHours / 24;

    // current year
    int currentYear = (int)(totalDays / 365) + 1970;


    long daysInCurrentYear = (totalDays - numberOfLeapYearsSince1970(currentYear)) % 365;
    if (currentHour > 0) daysInCurrentYear++; // add today

    // get current month number
    int currentMonthNum = getMonthFromDays(currentYear, (int)daysInCurrentYear);

    // getting current month name
    string month = getMonthName(currentMonthNum);

    // getting day of current month
    int daysTillCurrentMonth = getNumOfDaysToReachThatMonth(currentYear, currentMonthNum);

    int startDay = getStartDay(currentYear, currentMonthNum);
    int today = (int)daysInCurrentYear - daysTillCurrentMonth + 1;
    string dayOfWeek = dayNameOfWeek(((startDay + today) % 7));

    // Display results
   cout<<" Current date and time: " <<dayOfWeek << " " + month << " " << today << ", " <<currentYear
        << " " << currentHour << ":"
        << currentMinute << ":" << currentSecond;

}

string dayNameOfWeek(int dayOfWeek) {

    switch (dayOfWeek) {
    case 2: return "Monday";
    case 3: return "Tuesday";
    case 4: return "Wednesday";
    case 5: return "Thursday";
    case 6: return "Friday";
    case 7: return "Saturday";
    case 1: return "Sunday";
    }
}

int numberOfLeapYearsSince1970(long year) {

    int count = 0;
    for (int i = 1970; i <= year; i++) {
        if (isLeapYear(i))count++;
    }
    return count;
}

int getMonthFromDays(int year, int days) {
    int dayTracker = 0;
    for (int i = 1; i <= 12; i++) {

        dayTracker += getNumberOfDaysInMonth(year, i);

        if (dayTracker > days) return i;
    }
    return 12;
}

 int getNumOfDaysToReachThatMonth(int year, int month) {
    int dayTracker = 0;
    for (int i = 1; i < month; i++) {

        dayTracker += getNumberOfDaysInMonth(year, i);


    }
    return dayTracker;
}

/** Get the start day of month/1/year */
int getStartDay(int year, int month) {
    const int START_DAY_FOR_JAN_1_1800 = 3;
    // Get total number of days from 1/1/1800 to month/1/year
    int totalNumberOfDays = getTotalNumberOfDays(year, month);

    // Return the start day for month/1/year
    return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
}

/** Get the total number of days since January 1, 1800 */
 int getTotalNumberOfDays(int year, int month) {
    int total = 0;

    // Get the total days from 1800 to 1/1/year
    for (int i = 1800; i < year; i++)
        if (isLeapYear(i))
            total = total + 366;
        else
            total = total + 365;

    // Add days from Jan to the month prior to the calendar month
    for (int i = 1; i < month; i++)
        total = total + getNumberOfDaysInMonth(year, i);

    return total;
}

/** Get the number of days in a month */
int getNumberOfDaysInMonth(int year, int month) {

    if (month == 1 || month == 3 || month == 5 || month == 7 ||
        month == 8 || month == 10 || month == 12)
        return 31;

    if (month == 4 || month == 6 || month == 9 || month == 11)
        return 30;

    if (month == 2) return isLeapYear(year) ? 29 : 28;

    return 0; // If month is incorrect
}

/** Determine if it is a leap year */
 bool isLeapYear(int year) {
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}


/** Get the English name for the month 1-12 */
string getMonthName(int month) {
    string monthName = "";
    switch (month) {
    case 1: monthName = "January"; break;
    case 2: monthName = "February"; break;
    case 3: monthName = "March"; break;
    case 4: monthName = "April"; break;
    case 5: monthName = "May"; break;
    case 6: monthName = "June"; break;
    case 7: monthName = "July"; break;
    case 8: monthName = "August"; break;
    case 9: monthName = "September"; break;
    case 10: monthName = "October"; break;
    case 11: monthName = "November"; break;
    case 12: monthName = "December";
    }

    return monthName;
}

#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;




string format(int number, int width);
int numberWidthF(int number);

int main()
{

	
	int number,width;
	cin >> number >> width;
	cout << format(number, width);
	
}
string format(int number, int width)
{
	string zeros = " ";
	string num = to_string(number);
	
	int numberWidth = numberWidthF(number);
	
	for (int  i = 1; i <= width-numberWidth; i++)
	{
		zeros += "0";
	}
	return zeros+num;
}
int numberWidthF(int number)
{
	int digits, numberWidth = 0;
	while (number != 0)
	{
		digits = number % 10;
		number /= 10;
		numberWidth++;
	}
	return numberWidth;
}
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;

int octal2Dec(string& octal);
bool validity(int number);
int stringToNum(string number);


int main()
{
	string number;
	getline (cin , number);

	
	cout << octal2Dec(number);
	return 0;
}

int octal2Dec(string& octal)
{
	int digits, decimal = 0, count = 0,oct = stringToNum(octal);
	while (oct != 0)
	{
	
		digits = oct % 10;
		if(validity(digits) == true)
		  decimal += digits* pow(8,count);
		else
		{
			return NAN;
		}
		oct /= 10;
		count++;
		
	}
	return decimal;
}
bool validity(int number)
{
	return number % 8 == 0 || number % 9 == 0? false : true;
		
}
int stringToNum(string number)
{
	int convertedNumber = 0;
	for (int i = 0; i < number.length(); i++)
	{
		convertedNumber = (convertedNumber * 10) + static_cast<int>(number[i] - '0');
	}
	return convertedNumber;
}
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;

int octal2Dec(string& octal);
bool validity(int number);
int stringToNum(string number);
int bin2Dec(int binNumber);
int dec2octal(const string& number);




int main()
{
	string binNumber;
	getline(cin, binNumber);
	cout << dec2octal(binNumber);
	return 0;
}
int dec2octal(const string& number)
{
	int binNumber = stringToNum(number);
	int decimalNumber = bin2Dec(binNumber);
	int octal = 0, placeValue = 1;
	while (decimalNumber != 0)
	{
		octal += (decimalNumber % 8) * placeValue;
		decimalNumber /= 8;
		placeValue *= 10;
	}
	return octal;
}

int bin2Dec(int binNumber)
{
	int digits, decimal = 0, count = 0, bin = binNumber;
	while (bin != 0)
	{
		digits = bin % 10;
		  decimal += digits* pow(2,count);

		  bin /= 10;
		count++;
		
	}
	return decimal;
}
int stringToNum(string binNumber)
{
	int convertedNumber = 0;
	for (int i = 0; i < binNumber.length(); i++)
	{
		
		convertedNumber = (convertedNumber * 10) + static_cast<int>(binNumber[i] - '0');
	}
	return convertedNumber;
}
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
// CPP program to check if a string is 
// substring of other. 

using namespace std;

// Returns true if s1 is substring of s2 
int isSubstring(string s1, string s2)
{
	int M = s1.length();
	int N = s2.length();

	/* A loop to slide pat[] one by one */
	for (int i = 0; i <= N - M; i++) {
		int j;

		/* For current index i, check for
pattern match */
		for (j = 0; j < M; j++)
			if (s2[i + j] != s1[j])
				break;

		if (j == M)
			return i;
	}

	return -1;
}

/* Driver program to test above function */
int main()
{
	string s1 ,s2 ;
	getline(cin, s1);
	getline(cin, s2);
	int res = isSubstring(s1, s2);
	if (res == -1)
		cout << "Not present";
	else
		cout << "Present at index " << res;
	return 0;
}

Similiar Collections

Python strftime reference pandas.Period.strftime python - Formatting Quarter time in pandas columns - Stack Overflow python - Pandas: Change day - Stack Overflow python - Check if multiple columns exist in a df - Stack Overflow Pandas DataFrame apply() - sending arguments examples python - How to filter a dataframe of dates by a particular month/day? - Stack Overflow python - replace a value in the entire pandas data frame - Stack Overflow python - Replacing blank values (white space) with NaN in pandas - Stack Overflow python - get list from pandas dataframe column - Stack Overflow python - How to drop rows of Pandas DataFrame whose value in a certain column is NaN - Stack Overflow python - How to drop rows of Pandas DataFrame whose value in a certain column is NaN - Stack Overflow python - How to lowercase a pandas dataframe string column if it has missing values? - Stack Overflow How to Convert Integers to Strings in Pandas DataFrame - Data to Fish How to Convert Integers to Strings in Pandas DataFrame - Data to Fish create a dictionary of two pandas Dataframe columns? - Stack Overflow python - ValueError: No axis named node2 for object type <class 'pandas.core.frame.DataFrame'> - Stack Overflow Python Pandas iterate over rows and access column names - Stack Overflow python - Creating dataframe from a dictionary where entries have different lengths - Stack Overflow python - Deleting DataFrame row in Pandas based on column value - Stack Overflow python - How to check if a column exists in Pandas - Stack Overflow python - Import pandas dataframe column as string not int - Stack Overflow python - What is the most efficient way to create a dictionary of two pandas Dataframe columns? - Stack Overflow Python Loop through Excel sheets, place into one df - Stack Overflow python - How do I get the row count of a Pandas DataFrame? - Stack Overflow python - How to save a new sheet in an existing excel file, using Pandas? - Stack Overflow Python Loop through Excel sheets, place into one df - Stack Overflow How do I select a subset of a DataFrame? — pandas 1.2.4 documentation python - Delete column from pandas DataFrame - Stack Overflow