Task1: simple template calculator to find factorial of the number , sum of two arrays, check number is prime

PHOTO EMBED

Thu Jul 21 2022 06:06:59 GMT+0000 (Coordinated Universal Time)

Saved by @sahmal #c++

#include<iostream>
using namespace std;
template<typename T>
T* addArray(T* array, int size);
template<typename T>
T primeNumber(T number);
template<typename T>
T Factorial(T number);


int main() {

	int IntNum, Size;
	float FloatNum;
	
	cout << "Enter an integer " << endl;
	cin >> IntNum;
	cout << "Enter a float number " << endl;
	cin >> FloatNum;
	cout << "Enter size of the array " << endl;
	cin >> Size;
	int* Array = new int[Size];
	cout << "Enter elements of the array " << endl;
	for (int i = 0; i < Size; i++)
		cin >> Array[i];
	addArray<>(Array, Size);
	if (primeNumber<int>(IntNum) == true);
	cout << IntNum << " is a prime number " << endl;
	if(primeNumber<int>(IntNum) == false)
		cout << IntNum << " is not  a prime number " << endl;
	cout<<"Factorial of the "<<IntNum<<" is "<<Factorial<int>(IntNum);
	
	return 0;
}
template<typename T>
T primeNumber(T number) {
	bool flag = false;
	for (int i = 2; i < number; i++)
		if (number % i == 0)
			return flag;
	flag = true;
	return true;
}

template<typename T>
T Factorial(T number) {
	if (number >=1)
		return number * Factorial(number - 1);
	else {
		return 1;
		
	}
		
}

template<typename T>
T* addArray(T* array, int size) {
	T* newarray = new int[size];
	for (int i = 0; i < size; i++)
		newarray[i] = array[i] + array[i];
	return newarray;
}

content_copyCOPY

Lab task2