Skip to content

Instantly share code, notes, and snippets.

@CanadaMike17
Last active May 5, 2016 01:28
Show Gist options
  • Save CanadaMike17/ab2f4d1c9639bd36edab to your computer and use it in GitHub Desktop.
Save CanadaMike17/ab2f4d1c9639bd36edab to your computer and use it in GitHub Desktop.
TC1017 Homeworks
#include<iostream>
#include<math.h>
using namespace std;
int superpower (int a, int b){
int x, s, count=1;
x=a;
do {
s=a*x;
a=s;
count++;
} while(count!=b);
return s;
}
int main ()
{
int x,a,b,count,s;
cout<< "Which is the number you want to raise: ";
cin>> a;
cout<< "Which is the power you want to raise it by: ";
cin>> b;
s=superpower(a,b);
cout<< "The number: "<< a <<" raised to the exponential: "<< b << " is: "<< s <<endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(void){
char word[81],ans;
do{
bool palindrome=true;
cout << "Please enter a word" << endl;
cin>>word;
int length = strlen(word);
for (int i=0; i<length; i++){
word[i] = toupper(word[i]);
}
int(length/2);
if (length>0){
for(int i=0;i<(length);i++)
{
if(word[i]!=word[length-1-i])
palindrome=false;
}
}
if(palindrome==true)
{
cout << "The word is a palindrome" << endl;
}
else
{
cout << "The word is not a palindrome" << endl;
}
cout<< "do you want to repeat? "<<endl;
cin>>ans;
if (ans=='n'){
break;
}
} while (0 != strcmp(word, "END"));
return(0) ;
}
0
0
@batmantec batmantec/project.cpp
Created 2 hours ago
Code
Revisions 1
project.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
// Functions from other programs
int sum(int a, int b){
int c;
c=a+b;
return c;
}
int dif (int a, int b){
int e;
e=a-b;
return e;
}
int product (int a, int b){
int f;
f=a*b;
return f;
}
int divider (int a, int b){
int g;
g=a/b;
return g;
}
int rem (int a, int b){
int h;
h=a%b;
return h;
}
//End of functions of other programs
int simpleCalculator (int num1, int num2){
cout << " The sum of the two numbers is: "<< num1+num2 <<endl;
cout << " The diference of the two numbers is: "<< num1-num2 <<endl;
cout << " The product of the two numbers is: "<< num1*num2 <<endl;
cout << " The integer based division of the two numbers is: "<< num1/num2 <<endl;
cout << " The remainder of the two numbers is: "<< num1%num2 <<endl;
}
int tempConverter (int datain){
int dataout;
cout << " The temperature of "<< datain;
dataout= 5*(datain-32)/9;
cout << " and the temperature in Celsius is "<< dataout <<endl;
if (dataout>=100)
cout << " Water does boil at this temperature."<<endl;
else
cout << " Water does not boil at this temperature."<<endl;
}
int integerSum (int low, int up){
int sum=1, olow;
olow=low;
if (low<=up){
do {
low++;
sum=sum+low;
} while(low!=up);
cout<<" The sum of "<<olow<<" and "<<up<<" numbers is "<<sum<<endl;}
else{
cout<<" The number that you typed was upper than the upper number";
}
return 0;
}
int functionCalc(int a, int b)
{
int d,e,f,g,h;
d=sum (a,b);
e=dif(a,b);
f=product (a,b);
g=divider(a,b);
h=rem(a,b);
cout << endl <<" The sum of the numbers is: "<<d<<endl;
cout << endl <<" The rest of the numbers is: "<<e<<endl;
cout << endl <<" The multiplication of the two numbers is: "<<f<<endl;
cout << endl <<" The division of the two numbers is: "<<g<<endl;
cout << endl <<" The remainder of the division is: "<<h<<endl;
return 0;
}
int main (){
int menu;
cout << endl << "Hey there, this program is a compilation of all course programs!" << endl << endl;
cout << "################################################################" << endl << endl;
cout << "############################# MENU #############################" << endl << endl;
cout << "################################################################" << endl << endl;
cout << "-------- TYPE THE NUMBER OF THE PROGRAM YOU WANT TO USE --------" << endl << endl;
cout << " 1. Simple Calculator (sum, rest, mult, divides and remaninder)" << endl;
cout << " 2. Temperature Converter (converts F° to C°)" << endl;
cout << " 3. Lucky Game" << endl;
cout << " 4. Integer Sum" << endl;
cout << " 5. Function Calculator" << endl;
cout << " 6. Factorial Calculator" << endl;
cout << endl << " Please select an option: ";
cin >> menu;
cout << endl;
switch (menu) {
case 1:
int num1, num2;
cout << " Please enter a integer number: ";
cin >> num1;
cout << " Please enter a second integer number: ";
cin >> num2;
simpleCalculator(num1, num2);
break;
case 2:
int datain;
cout << " Enter the temperature in Fahrenheit: ";
cin >> datain;
tempConverter(datain);
break;
case 3:
int user;
cout << " You have to guess the number between 1 and 100 "<<endl;
cin >> user;
int random, counter;
srand(time(0));
random = rand()%101;
for(counter=1; user!=random; counter++){
if(user!=random){
if(user>random)
cout<<" The number is to high, please try again."<<endl;
if(user<random)
cout<<" The number is to low, please try again."<<endl;
cin>>user;
}
}
if(user==random){
cout<<" ###########################"<<endl;
cout<<" #You get the right number!#"<<endl;
cout<<" # You made it at "<<counter<<" time. #"<<endl;
cout<<" ###########################"<<endl;
}
break;
case 4:
int up, low;
cout<<" This program calculates the sum of 2 integer numbers that you give"<<endl;
cout<<" Please, give me lower number: ";
cin>>low;
cout<<" Please give me upper number: ";
cin>>up;
integerSum(low, up);
break;
case 5:
int a, b;
cout<<" Give me the first integer number: ";
cin>>a;
cout << endl <<" Give me the second integer number: ";
cin>>b;
functionCalc(a, b);
break;
case 6:
long long int n,count,product,sum,fac;
char answ;
cout<<" This program calculate the factorial of a number."<<endl;
do {
cout << endl << " Please type an positive number: ";
cin >> n;
count=1;
product=1;
sum=1;
if (n>1) {
do{
count++;
product=product*(sum+1);
sum++;
fac=product;
}while (count!=n);
cout <<" The factorial of "<<n<<" is: "<<fac<<endl << endl;
cout <<" Would you like to type another number (y/n)? ";
cin >> answ;
} else {
if (n==1) {
cout << endl <<" The factorial of 1 is: 1"<<endl;
cout << endl << " Would you like to type another number (y/n)? ";
cin>>answ;
} else {
cout<<" The number that you enter was negative, please try again."<<endl;
answ='y';
}
}
} while(answ=='y');
break;
default:
cout << endl << " Please select a valid option." << endl;
}
}
#include<iostream>
#include<math.h>
using namespace std;
int superpower (int a, int b){
int x, s, count=1;
x=a;
do {
s=a*x;
a=s;
count++;
} while(count!=b);
return s;
}
int main ()
{
int x,a,b,count,s;
cout<< "Which is the number you want to raise: ";
cin>> a;
cout<< "Which is the power you want to raise it by: ";
cin>> b;
s=superpower(a,b);
cout<< "The number: "<< a <<" raised to the exponential: "<< b << " is: "<< s <<endl;
return 0;
}
#include <iostream>
using namespace std;
void star ( int a){
int count=0;
do {
cout<<"*";
count++;
} while (count!=a);
cout<<endl;
}
int main (){
int x,count,save;
cout<<"How many stars do you want to see?" <<endl;
cin>>a;
star(a);
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
float distance (float x1, float x2, float y1, float y2) {
float dis;
dis=sqrt((x2-x1)*(x2-x1)+((y2-y1)*(y2-y1)));
return dis;
}
int main () {
float x1,x2,y1,y2,dis;
cout<<"Hello fellows, we will now calculate the distance between 2 points in the cartesian plane"<<endl;
cout<<"Insert the first X number"<<endl;
cin>>x1;
cout<<"Lets continue to the Y number"<<endl;
cin>>y1;
cout<<"Insert the second X number"<<endl;
cin>>x2;
cout<<"And now the second Y number"<<endl;
cin>>y2;
dis=distance(x1,x2,y1,y2);
cout<<"The distance between this points is: "<< dis <<endl;
return 0;
}
#include <iostream>
using namespace std;
int fib (int x)
{
int y;
if (x>=2)
{
y=fib(x-1) + fib (x-2);
return y;
}
else
{
if (x==1)
{
return 1;
}
else
{
return 0;
}
}
}
int main ()
{
int x, ans;
cout << "Give me a number: ";
cin >> x;
ans = fib (x);
cout << "The Fibonacci number is: "<<ans<<endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int factorial(int a)
{
if (a == 1 || a == 0)
{
return 1;
}
else
{
return a * factorial(a - 1);
}
}
long double eulerCalc(float b)
{
long double a = 0.0, ans = 0.0;
do
{
ans += 1.0/(factorial(a));
a = a + 1;
}
while(a <= b);
return ans;
}
int main()
{
long double b, ans, a = 0.0;
cout << "Please write a number: \n ";
cin >> b;
cout << "The answer is " << eulerCalc(b) << endl;
}
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(void){
char word[81],ans;
do{
bool palindrome=true;
cout << "Please enter a word" << endl;
cin>>word;
int length = strlen(word);
for (int i=0; i<length; i++){
word[i] = toupper(word[i]);
}
int(length/2);
if (length>0){
for(int i=0;i<(length);i++)
{
if(word[i]!=word[length-1-i])
palindrome=false;
}
}
if(palindrome==true)
{
cout << "The word is a palindrome" << endl;
}
else
{
cout << "The word is not a palindrome" << endl;
}
cout<< "do you want to repeat? "<<endl;
cin>>ans;
if (ans=='n'){
break;
}
} while (0 != strcmp(word, "END"));
return(0) ;
}
#include <iostream>
using namespace std;
int array,module,i;
int arrays[10];
int find_threes(int a){
for (int i=0; i<a; i++){
cout<<"type the number in the array"<<endl;
cin>>arrays[i];
}
cout<<"this numbers can be divivded by 3: "<<endl;
for (int i=0; i<a; i++){
module = arrays[i]%3;
}
}
int main() {
cout<<"type the nunmbers in the array"<<endl;
cin>>array;
find_threes(array);
return 0;
}
#include <iostream>
using namespace std;
int dotProduct(int num1, int num2){
int ans;
ans = num1*num2;
return ans;
}
int main (){
int i, x, save = 0, sum;
cout << "This program calculates the dot product." << endl;
cout << "Type the number of values for each array you will be using: ";
cin >> i;
cout << endl;
int list1[i], list2[i];
for (x = 0; x < i; x++) {
cout << "Type the value " << x << " for the first array: ";
cin >> list1[x];
}
cout << endl;
for (x = 0; x < i; x++) {
cout << "Type the value " << x << " for the second array: ";
cin >> list2[x];
}
for (x = 0; x < i; x++) {
sum = dotProduct(list1[x], list2[x]);
save += sum;
}
cout << "The dot product is :" << save;
}
#include <iostream>
#include<cmath>
using std::cout;
using std::cin;
using std::endl;
int getTotal(float numbers[],float numberofNumbers){
float answer=0.0;
for (int i=0; i<numberofNumbers ; i=i+1){
answer = answer + numbers[i];
}
return answer ;
}
int average(float numbers[], float numberofNumbers) {
float answer=0;
answer=(getTotal(numbers, numberofNumbers))/numberofNumbers;
return answer;
}
int standartdeviation(float numbers[], float numberofNumbers) {
float add=0;
for (int i=0; i<numberofNumbers; i=i+1){
float answer=pow(average(numbers, numberofNumbers)-numbers[i], 2);
add=add+answer; }
float s=sqrt(add/(numberofNumbers-1));
return s;
}
int main () {
const int numberofNumbers = 10;
float numbers, total, sum, average, stddev;
float x[numberofNumbers];
for (int i = 0; i<10; i++){
cout<<"give a number: "<<endl;
cin>>x[i];
}
sum = getTotal(x, numberofNumbers);
cout<<"The addition of all the digits is: "<<sum<<endl;
average=(x, numberofNumbers);
cout<<"The average of the numbers is: "<<average<<endl;
stddev=standartdeviation(x, numberofNumbers);
cout<<"The standart deviation of the data is :"<<" "<<stddev<<endl;
return 0;
}
#include <iostream>
using namespace std;
#include "BigIntegerLibrary.hh"
BigInteger checkReverse(BigInteger num) {
BigInteger reverse = 0;
while(num != 0) {
BigInteger remainder = num%10;
reverse = reverse*10 + remainder;
num/=10;
}
return reverse;
}
int checkNum(BigInteger num){
int ans = 0, i;
BigInteger reverseNum, addition, additionReverse;
reverseNum = checkReverse(num);
if (num == reverseNum) {
ans = 1;
} else {
addition = num + reverseNum;
for (i = 0; i >= 30 || additionReverse != reverseNum; i++) {
additionReverse = checkReverse(addition);
if (additionReverse == addition) {
ans = 2;
break;
}else{
if (i == 30) {
ans = 3;
break;
} else {
addition = addition + additionReverse;
}
}
}
}
return ans;
}
int main (){
int low, sum, upper, lych = 0, nonLych = 0, natural = 0, i = 0, reverse, numCheck, range = 0;
cout << "What is the lower bound of numbers to concider: ";
cin >> low;
cout << "What is the upper bound of numbers to concider: ";
cin >> upper;
if (low<=upper) {
cout << "Continue" << endl;
sum = low;
for (i = low; i <= upper; i++) {
range++;
sum = checkNum(i);
if (sum == 1) {
natural++;
} else {
if (sum == 2) {
nonLych++;
} else {
if (sum == 3) {
lych++;
} else {
cout << "There was an error in the code" << endl;
}
}
}
}
cout << "The range of numbers analysed is: " << range << endl;
cout << "The number of natural palindromes is: " << natural << endl;
cout << "The number of non-Lycherels encountered is: " << nonLych << endl;
cout << "The number of Lycherel number candidates is: " << lych << endl;
}else {
cout << "The lower number was higher than the upper number, please try again.";
}
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int CheckFile(string fileName){
ifstream InList;
InList.open(fileName.c_str());
if (InList.is_open()) {
cout << "File Open" << endl;
cout << endl;
return 1;
} else {
return 0;
}
}
int ReadFile(string word, string fname){
int counter;
string foundWord;
ifstream InList;
InList.open(fname.c_str());
while (!InList.eof()) {
InList >> foundWord;
if (foundWord == word) {
counter++;
}
}
InList.close();
return counter;
}
int main(){
string word, fname;
cout <<"Enter the File name with .txt at the end:" << endl;
cin >> fname;
if (CheckFile(fname)==0) {
cout << "Please check the File name and location." << endl;
}
cout <<"Enter the word to search: " << endl;
cin >> word;
cout << endl << "The word " << '"' << word << '"' <<" was found " << ReadFile(word, fname) << " time(s) in the File." << endl;
}
#include <iostream>
using namespace std;
int main () {
cout <<"Hello World"<< endl;
return 0;
}
#include <iostream>
using namespace std;
int main (){
int num1, num2;
cout << "Insert a integer number: " ;
cin >> num1 ;
cout << "Insert another integer number: " ;
cin >> num2 ;
cout << "If you add them the result is: " << num1+num2 <<endl;
cout << "The difference between them is: "<<num1-num2 << endl;
cout << "The product of this two numbers is: " <<num1*num2 << endl;
cout << "The integer based division of the two numbers,first divided by second is: " <<num1/num2 << endl;
cout << "The remainder of integer division of the two numbers: " << num1%num2 << endl;
return 0;
}
#include <iostream>
using namespace std;
int main (){
int temperature,celsius;
cout << "Please introduce the Farenheit temperature you'd like to know: " ;
cin >> temperature ;
celsius = (5*(temperature-32)/9);
cout << "The temperature in Celsius is: "<<celsius<<endl;
if (celsius>=100)
cout << "Water does boil at this temperature" <<endl;
else
cout << "Water does not boil at this temperature" <<endl;
return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main (){
int num, random, cont;
srand(time(0));
random = rand()%101;
cout << "You will be asked to select a number between 1-100 " << endl;
cout << "Good Luck" <<endl;
cin >> num;
for (cont=1; num!= random; cont++)
{
if (num!=random)
{
if(num<random)
cout << "Your number is smaller than mine...HAHAHAHA" <<endl;
if(num>random)
cout << "Your number is bigger than mine...HAHAHAHA" <<endl;
cin >>num;
}
}
if (num==random)
{
cout << "Lucky you, you finally did it" <<endl;
cout << "But you failed too many chances: " << cont << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main (){
int low, high, sum;
sum =0;
cout<< "insert a lower bound" << endl;
cin >> low
cout<< "insert a higher bound"<<endl;
cin >> high
if (low<=high)
{
do
{
low = low++;
sum = sum+low;
}while (low!=high);
cout << "The sum of the numbers is: " << sum << endl;}
else {
cout << "the numbers are incorrect" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int add (int a, int b)
{
int s;
s=a+b;
return s;
}
int substraction (int a, int b)
{
int r;
r=a-b;
return r;
}
int mult (int a, int b)
{
int m;
m=a*b;
return m;
}
int div (int a, int b)
{
int d;
d=a/b;
return d;
}
int intdiv (int a, int b)
{
int i;
i=a%b;
return i;
}
int main ()
{
int a, b, s, r, m, d, i;
cout<< "Give me a integer number" << endl;
cin>>a;
cout<< "Give me a second integer number" << endl;
cin>>b;
s=add (a,b);
r=substraction (a,b);
m=mult (a,b);
d=div (a,b);
i=intdiv (a,b);
cout << "The addition of the numbers is: "<<s<<endl;
cout << "The substraction of the numbers is: "<<r<<endl;
cout << "The multiplication of the numbers is: "<<m<<endl;
cout << "The integer based division of the two numbers,first divided by second is: "<<d<<endl;
cout << "The remainder of integer division of the two numbers: "<<i<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment