diff --git a/30 days of code Solutions/Day-0.cpp b/30 days of code Solutions/Day-0.cpp new file mode 100644 index 0000000..fbf069b --- /dev/null +++ b/30 days of code Solutions/Day-0.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +using namespace std; + +//-------------------------------------------------------------------- + + +int main() +{ + + string input_string; + + getline(cin, input_string); + + cout << "Hello, World." << endl; + + cout << input_string << endl; + + + return 0; +} diff --git a/30 days of code Solutions/Day-1.cpp b/30 days of code Solutions/Day-1.cpp new file mode 100644 index 0000000..07eb71d --- /dev/null +++ b/30 days of code Solutions/Day-1.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; + +int main() { + int i = 4; + double d = 4.0; + string s = "HackerRank "; + +//----------------------------------------------------------------- + + + int lo; + string lo2; + double lo3; + cin >> lo >> lo3 >> lo2; + + cout << lo + i << endl; + cout << lo3 + d << endl; + cout << s << lo2 << endl; + +//--------------------------------------------------------------------- + return 0; +} diff --git a/30 days of code Solutions/Day-10.cpp b/30 days of code Solutions/Day-10.cpp new file mode 100644 index 0000000..a317b63 --- /dev/null +++ b/30 days of code Solutions/Day-10.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +using namespace std; + +bool desc(int i, int l){return (i > l);} + +int main(){ + int n; + cin >> n; + vector ones; + bitset<32> m(n); + string ip = m.to_string(); + int cnt = 0; + for(int m = 0; m < ip.size(); ++m){ + if(ip[m] == '1'){ + ++cnt; + } + else{ + ones.push_back(cnt); + cnt = 0; + } + if(m == ip.size() - 1){ + ones.push_back(cnt); + } + } + sort(ones.begin(), ones.end(), desc); + cout << ones.at(0) << endl; + return 0; +} diff --git a/30 days of code Solutions/Day-11.cpp b/30 days of code Solutions/Day-11.cpp new file mode 100644 index 0000000..e89669c --- /dev/null +++ b/30 days of code Solutions/Day-11.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +using namespace std; + +vector >lmao; +vector answers; + +bool desc(int i, int k){return i > k;} + +int TakeInput(int i, int m) +{ + for(int f = 0; f < i; ++f){ + vector temp; + for(int a = 0; a < m; ++a){ + int x; + cin >> x; + temp.push_back(x); + } + lmao.push_back(temp); + } + return 0; +} + +void compute() +{ + for(int i = 0; i < lmao.size() - 2; ++i){ + for(int e = 0; e < lmao[i].size() - 2; ++e){ + int sum = lmao[i][e] + lmao[i][e+1] + lmao[i][e+2] + lmao[i+1][e+1] +lmao[i+2][e] + lmao[i+2][e+1] + lmao[i+2][e+2]; + answers.push_back(sum); + } + } +} +int main() +{ + TakeInput(6, 6); + compute(); + sort(answers.begin(), answers.end(), desc); + cout << answers.at(0) << endl; +} diff --git a/30 days of code Solutions/Day-12.cpp b/30 days of code Solutions/Day-12.cpp new file mode 100644 index 0000000..537fa38 --- /dev/null +++ b/30 days of code Solutions/Day-12.cpp @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; + + +class Person{ + protected: + string firstName; + string lastName; + int id; + public: + Person(string firstName, string lastName, int identification){ + this->firstName = firstName; + this->lastName = lastName; + this->id = identification; + } + void printPerson(){ + cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n"; + } + +}; + +//------------------------------------------------------------------------- + +class Student : public Person{ + private: + vector testScores; + public: + Student(string firstName, string lastName, int identification, vector scores) : Person(firstName, lastName, identification), testScores(scores) { } + + char calculate() { + int average = 0; + for(int i = 0; i < testScores.size(); i++) { + average += testScores[i]; + } + average = average / testScores.size(); + + if(average >= 90) { + return 'O'; // Outstanding + } + else if(average >= 80) { + return 'E'; // Exceeds Expectations + } + else if(average >= 70) { + return 'A'; // Acceptable + } + else if(average >= 55) { + return 'P'; // Poor + } + else if(average >= 40) { + return 'D'; // Dreadful + } + else { + return 'T'; // Troll + } + } +}; + +//------------------------------------------------------------------------- + +int main() { + string firstName; + string lastName; + int id; + int numScores; + cin >> firstName >> lastName >> id >> numScores; + vector scores; + for(int i = 0; i < numScores; i++){ + int tmpScore; + cin >> tmpScore; + scores.push_back(tmpScore); + } + Student* s = new Student(firstName, lastName, id, scores); + s->printPerson(); + cout << "Grade: " << s->calculate() << "\n"; + return 0; +} diff --git a/30 days of code Solutions/Day-13.cpp b/30 days of code Solutions/Day-13.cpp new file mode 100644 index 0000000..28aaadb --- /dev/null +++ b/30 days of code Solutions/Day-13.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +class Book{ + protected: + string title; + string author; + public: + Book(string t,string a){ + title=t; + author=a; + } + virtual void display()=0; + +}; + +//-------------------------------------------------------------------- + +class MyBook : public Book { + protected: + int price; + public: + MyBook(string title, string author, int price) : Book(title, author), price(price) { } + void display(){ + cout << "Title: " << title << endl; + cout << "Author: " << author << endl; + cout << "Price: " << price << endl; + } +}; + +//--------------------------------------------------------------------- + +int main() { + string title,author; + int price; + getline(cin,title); + getline(cin,author); + cin>>price; + MyBook novel(title,author,price); + novel.display(); + return 0; +} diff --git a/30 days of code Solutions/Day-14.cpp b/30 days of code Solutions/Day-14.cpp new file mode 100644 index 0000000..1bfe740 --- /dev/null +++ b/30 days of code Solutions/Day-14.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + +using namespace std; + +class Difference { + private: + vector elements; + + public: + int maximumDifference; + +//------------------------------------------------------------------------------------ + + Difference(vector elm) : elements(elm) {} + void computeDifference(){ + sort(elements.begin(), elements.end(), greater()); + maximumDifference = elements.at(0) - elements.at(elements.size() - 1); + } + +//-------------------------------------------------------------------------------------- + +}; // End of Difference class + +int main() { + int N; + cin >> N; + + vector a; + + for (int i = 0; i < N; i++) { + int e; + cin >> e; + + a.push_back(e); + } + + Difference d(a); + + d.computeDifference(); + + cout << d.maximumDifference; + + return 0; +} diff --git a/30 days of code Solutions/Day-15.cpp b/30 days of code Solutions/Day-15.cpp new file mode 100644 index 0000000..9242a58 --- /dev/null +++ b/30 days of code Solutions/Day-15.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; +class Node +{ + public: + int data; + Node *next; + Node(int d){ + data=d; + next=NULL; + } +}; +class Solution{ + public: + +//------------------------------------------------------------ + + Node* insert(Node *head,int data) + { + if(head == NULL){ + head = new Node(data); + return head; + } + else if(head->next == NULL){ + Node *temp = new Node(data); + head->next = temp; + return head; + } + else{ + insert(head->next, data); + } + return head; + } + +//-------------------------------------------------------------- + + void display(Node *head) + { + Node *start=head; + while(start) + { + cout<data<<" "; + start=start->next; + } + } +}; +int main() +{ + Node* head=NULL; + Solution mylist; + int T,data; + cin>>T; + while(T-->0){ + cin>>data; + head=mylist.insert(head,data); + } + mylist.display(head); + +} diff --git a/30 days of code Solutions/Day-16.cpp b/30 days of code Solutions/Day-16.cpp new file mode 100644 index 0000000..a6107d7 --- /dev/null +++ b/30 days of code Solutions/Day-16.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + + +int main(){ + string S; + cin >> S; + try{ + int no; + no = stoi(S); + cout << no << endl; + } + catch(exception e){ + cout << "Bad String" << endl; + } + return 0; +} diff --git a/30 days of code Solutions/Day-17.cpp b/30 days of code Solutions/Day-17.cpp new file mode 100644 index 0000000..71714b5 --- /dev/null +++ b/30 days of code Solutions/Day-17.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +using namespace std; + +//------------------------------------------------------------------ + +class Calculator{ + public: + int power(int a, int b){ + if(a < 0){ + throw invalid_argument( "n and p should be non-negative" ); + } + if(b < 0){ + throw invalid_argument( "n and p should be non-negative" ); + } + return pow(a, b); + } +}; + +//--------------------------------------------------------------------- + +int main() +{ + Calculator myCalculator=Calculator(); + int T,n,p; + cin>>T; + while(T-->0){ + if(scanf("%d %d",&n,&p)==2){ + try{ + int ans=myCalculator.power(n,p); + cout< +#include +#include + +using namespace std; + +class Solution { + private: + vectorstack; + dequequeue; + int p = 0; + int q = 0; + public: + char pushCharacter(char c){ + stack.push_back(c); + return NULL; + } + char enqueueCharacter(char c){ + queue.push_front(c); + return NULL; + } + char popCharacter(){ + return stack[p++]; + } + char dequeueCharacter(){ + return queue[q++]; + } + +}; diff --git a/30 days of code Solutions/Day-19.cpp b/30 days of code Solutions/Day-19.cpp new file mode 100644 index 0000000..0958735 --- /dev/null +++ b/30 days of code Solutions/Day-19.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +class AdvancedArithmetic{ + public: + virtual int divisorSum(int n)=0; +}; + +//-------------------------------------------------------------------- + +class Calculator : public AdvancedArithmetic{ + public: + int divisorSum(int n){ + vectordivisors; + for(int i = 1; i <= n; ++i){ + if(n % i == 0){ + divisors.push_back(i); + } + } + int sum = 0; + for(auto m : divisors){ + sum += m; + } + return sum; + } +}; + +//--------------------------------------------------------------- + +int main(){ + int n; + cin >> n; + AdvancedArithmetic *myCalculator = new Calculator(); + int sum = myCalculator->divisorSum(n); + cout << "I implemented: AdvancedArithmetic\n" << sum; + return 0; +} diff --git a/30 days of code Solutions/Day-2.cpp b/30 days of code Solutions/Day-2.cpp new file mode 100644 index 0000000..1fe2fce --- /dev/null +++ b/30 days of code Solutions/Day-2.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + + +int main() { + double x, y, z; + cin >> x >> y >> z; + double tip = x * y / 100; + double tax = x * z / 100; + double total = x + tip + tax; + int total2 = total; + cout << "The total meal cost is " << total2 << " dollars."; + +} diff --git a/30 days of code Solutions/Day-20.cpp b/30 days of code Solutions/Day-20.cpp new file mode 100644 index 0000000..ddf9719 --- /dev/null +++ b/30 days of code Solutions/Day-20.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + + +int calculate(vector &ab) +{ + int allswaps = 0; + while(1){ + int tempswaps = 0; + for(int i = 0; i < ab.size() - 1; ++i){ + if(ab[i] > ab[i+1]){ + swap(ab[i], ab[i+1]); + ++tempswaps; + } + } + if(tempswaps == 0){ + break; + } + allswaps += tempswaps; + } + return allswaps; +} + +int main() +{ + int n; + cin >> n; + vector a; + for(int a_i = 0;a_i < n;a_i++){ + int p; + cin >> p; + a.push_back(p); + } + cout << "Array is sorted in " << calculate(a) << " swaps." << endl; + cout << "First Element: " << a.at(0) << endl; + cout << "Last Element: " << a.at(a.size() - 1) << endl; + return 0; +} diff --git a/30 days of code Solutions/Day-21.cpp b/30 days of code Solutions/Day-21.cpp new file mode 100644 index 0000000..f26aa90 --- /dev/null +++ b/30 days of code Solutions/Day-21.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +//--------------------------------------------------------- + +template +void printArray(vector array) { + for(TP i : array){ + cout << i << endl; + } +} + +//----------------------------------------------------- + +int main() { + + vector vInt{1, 2, 3}; + vector vString{"Hello", "World"}; + + printArray(vInt); + printArray(vString); + + return 0; +} diff --git a/30 days of code Solutions/Day-22.cpp b/30 days of code Solutions/Day-22.cpp new file mode 100644 index 0000000..d5b3a16 --- /dev/null +++ b/30 days of code Solutions/Day-22.cpp @@ -0,0 +1,75 @@ +#include +#include + +using namespace std; + +class Node{ + public: + int data; + Node *left; + Node *right; + Node(int d){ + data = d; + left = NULL; + right = NULL; + } +}; +class Solution{ + public: + Node* insert(Node* root, int data) { + if(root == NULL) { + return new Node(data); + } + else { + Node* cur; + if(data <= root->data){ + cur = insert(root->left, data); + root->left = cur; + } + else{ + cur = insert(root->right, data); + root->right = cur; + } + + return root; + } + } + +//--------------------------------------------------------------------- + + int getHeight(Node* root){ + if(root == NULL){ + return -1; + } + int ldepth = getHeight(root->left); + int rdepth = getHeight(root->right); + if(ldepth < rdepth){ + return (rdepth + 1); + }else {return ldepth +1;} + } + +//----------------------------------------------------------------- + +}; //End of Solution + +int main() { + Solution myTree; + Node* root = NULL; + int t; + int data; + + cin >> t; + + while(t-- > 0){ + cin >> data; + root = myTree.insert(root, data); + } + int height = myTree.getHeight(root); + cout << height; + + return 0; +} + + + + diff --git a/30 days of code Solutions/Day-23.cpp b/30 days of code Solutions/Day-23.cpp new file mode 100644 index 0000000..96266dd --- /dev/null +++ b/30 days of code Solutions/Day-23.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +using namespace std; +class Node{ + public: + int data; + Node *left,*right; + Node(int d){ + data=d; + left=right=NULL; + } +}; +class Solution{ + public: + Node* insert(Node* root, int data){ + if(root==NULL){ + return new Node(data); + } + else{ + Node* cur; + if(data<=root->data){ + cur=insert(root->left,data); + root->left=cur; + } + else{ + cur=insert(root->right,data); + root->right=cur; + } + return root; + } + } + +//----------------------------------------------------------------------------------- + + void levelOrder(Node *root){ + int height = MaxHeight(root); + for(int i = 0; i <= height; ++i){ + ThisLevelTransversal(root, i); + } + } + int MaxHeight(Node* root){ + if(root == NULL){ + return -1; + } + int ldepth = MaxHeight(root->left); + int rdepth = MaxHeight(root->right); + if(ldepth < rdepth){ + return (rdepth + 1); + }else {return ldepth +1;} + } + void ThisLevelTransversal(Node *root, int level){ + if(root == NULL){return;} + if (level == 0){ + cout << root->data << " "; + }else if (level > 0){ + ThisLevelTransversal(root->left, level-1); + ThisLevelTransversal(root->right, level-1); + } + + } + +//------------------------------------------------------- + +};//End of Solution +int main(){ + Solution myTree; + Node* root=NULL; + int T,data; + cin>>T; + while(T-->0){ + cin>>data; + root= myTree.insert(root,data); + } + myTree.levelOrder(root); + return 0; +} diff --git a/30 days of code Solutions/Day-24.cpp b/30 days of code Solutions/Day-24.cpp new file mode 100644 index 0000000..6f4b3b1 --- /dev/null +++ b/30 days of code Solutions/Day-24.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +class Node +{ + public: + int data; + Node *next; + Node(int d){ + data=d; + next=NULL; + } +}; +class Solution{ + public: + +//---------------------------------------------------------------------- + + Node* removeDuplicates(Node *head) + { + Node *cur = head; + Node *helper; + if(cur == NULL){ + return head; + } + while(cur->next != NULL){ + if(cur->data == cur->next->data){ + cur->next = cur->next->next; + } + else cur = cur->next; + } + return head; + } + +//--------------------------------------------------------------------------- + + Node* insert(Node *head,int data) + { + Node* p=new Node(data); + if(head==NULL){ + head=p; + + } + else if(head->next==NULL){ + head->next=p; + + } + else{ + Node *start=head; + while(start->next!=NULL){ + start=start->next; + } + start->next=p; + + } + return head; + + + } + void display(Node *head) + { + Node *start=head; + while(start) + { + cout<data<<" "; + start=start->next; + } + } +}; + +int main() +{ + Node* head=NULL; + Solution mylist; + int T,data; + cin>>T; + while(T-->0){ + cin>>data; + head=mylist.insert(head,data); + } + head=mylist.removeDuplicates(head); + + mylist.display(head); + +} diff --git a/30 days of code Solutions/Day-25.cpp b/30 days of code Solutions/Day-25.cpp new file mode 100644 index 0000000..a0bea8f --- /dev/null +++ b/30 days of code Solutions/Day-25.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +using namespace std; + +bool CheckPrime(int n){ + if(n == 1){return false;} + int m; + m = sqrt(n); + bool isprime = true; + for(int i = 2; i <= m; ++i){ + if(n % i == 0){ + isprime = false; + break; + } + } + return isprime; +} + +int main() { + int p; + cin >> p; + for(int i = 0; i < p; ++i){ + int m; + cin >> m; + if(CheckPrime(m)){ + cout << "Prime" << endl; + }else cout << "Not prime" << endl; + } + return 0; +} diff --git a/30 days of code Solutions/Day-26.cpp b/30 days of code Solutions/Day-26.cpp new file mode 100644 index 0000000..812cc15 --- /dev/null +++ b/30 days of code Solutions/Day-26.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + int fine; + int retDay, retMonth, retYear; + int expDay, expMonth, expYear; + cin >> retDay >> retMonth >> retYear; + cin >> expDay >> expMonth >> expYear; + if(retYear < expYear){ + fine = 0; + }else if(retYear == expYear){ + if(retMonth < expMonth){ + fine = 0; + }else if(retMonth == expMonth){ + if(retDay <= expDay){ + fine = 0; + }else fine = (retDay - expDay) * 15; + }else fine = (retMonth - expMonth) * 500; + }else fine = 10000; + cout << fine << endl; + return 0; +} diff --git a/30 days of code Solutions/Day-27.cpp b/30 days of code Solutions/Day-27.cpp new file mode 100644 index 0000000..ba6f790 --- /dev/null +++ b/30 days of code Solutions/Day-27.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int minimum_index(vector seq) { + if (seq.empty()) { + throw invalid_argument("Cannot get the minimum value index from an empty sequence"); + } + int min_idx = 0; + for (int i = 1; i < seq.size(); ++i) { + if (seq[i] < seq[min_idx]) { + min_idx = i; + } + } + return min_idx; +} + +class TestDataEmptyArray { +public: + static vector get_array() { + // complete this function + vector v; + return v; + } + +}; + +class TestDataUniqueValues { +public: + static vector get_array() { + // complete this function + vector v={1,2,3}; + return v; + } + + static int get_expected_result() { + // complete this function + return 0; + } + +}; + +class TestDataExactlyTwoDifferentMinimums { +public: + static vector get_array() { + // complete this function + vector v={1,1,2,3}; + return v; + + } + + static int get_expected_result() { + // complete this function + return 0; + } + +}; + + +void TestWithEmptyArray() { + try { + auto seq = TestDataEmptyArray::get_array(); + auto result = minimum_index(seq); + } catch (invalid_argument& e) { + return; + } + assert(false); +} + +void TestWithUniqueValues() { + auto seq = TestDataUniqueValues::get_array(); + assert(seq.size() >= 2); + + assert(set(seq.begin(), seq.end()).size() == seq.size()); + + auto expected_result = TestDataUniqueValues::get_expected_result(); + auto result = minimum_index(seq); + assert(result == expected_result); +} + +void TestWithExactlyTwoDifferentMinimums() { + auto seq = TestDataExactlyTwoDifferentMinimums::get_array(); + assert(seq.size() >= 2); + + auto tmp = seq; + sort(tmp.begin(), tmp.end()); + assert(tmp[0] == tmp[1] and (tmp.size() == 2 or tmp[1] < tmp[2])); + + auto expected_result = TestDataExactlyTwoDifferentMinimums::get_expected_result(); + auto result = minimum_index(seq); + assert(result == expected_result); +} + +int main() { + TestWithEmptyArray(); + TestWithUniqueValues(); + TestWithExactlyTwoDifferentMinimums(); + cout << "OK" << endl; + return 0; +} + diff --git a/30 days of code Solutions/Day-28.cpp b/30 days of code Solutions/Day-28.cpp new file mode 100644 index 0000000..99a536a --- /dev/null +++ b/30 days of code Solutions/Day-28.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +using namespace std; + +vectorsorted; + +bool ascending(string i, string m){return i < m;} + +void compute(vector email, vector name){ + for(int i = 0; i < email.size(); ++i){ + if(email[i].find("@gmail.com") != std::string::npos){ + sorted.push_back(name[i]); + } + } + sort(sorted.begin(), sorted.end(), ascending); +} + +int main(){ + vectormails; + vectornames; + int N; + cin >> N; + for(int a0 = 0; a0 < N; a0++){ + string firstName; + string emailID; + cin >> firstName >> emailID; + names.push_back(firstName); + mails.push_back(emailID); + } + compute(mails, names); + for(auto m : sorted){ + cout << m << endl; + } + return 0; +} diff --git a/30 days of code Solutions/Day-29.cpp b/30 days of code Solutions/Day-29.cpp new file mode 100644 index 0000000..235896a --- /dev/null +++ b/30 days of code Solutions/Day-29.cpp @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; + +int outAnd(int a, int b){ + vectorlol; + for(int i = 1; i <= a; ++i){ + lol.push_back(i); + } + int max = 0; + for(int i = 0; i < lol.size(); ++i){ + for(int p = i + 1; p < lol.size(); ++p){ + int anded = lol[i] & lol[p]; + if((anded > max) && (anded < b) ){ + max = anded; + } + } + } + return max; +} + +int main(){ + int t; + cin >> t; + for(int a0 = 0; a0 < t; a0++){ + int n; + int k; + cin >> n >> k; + cout << outAnd(n, k) << endl; + } + return 0; +} diff --git a/30 days of code Solutions/Day-3.cpp b/30 days of code Solutions/Day-3.cpp new file mode 100644 index 0000000..fc374f3 --- /dev/null +++ b/30 days of code Solutions/Day-3.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; + + +int main(){ + int N; + cin >> N; + if(N%2 != 0 || (N%2 == 0) && N >= 6 && N <= 20){ + cout << "Weird" << endl; + }else cout << "Not Weird" << endl; + return 0; +} diff --git a/30 days of code Solutions/Day-4.cpp b/30 days of code Solutions/Day-4.cpp new file mode 100644 index 0000000..b6b53ab --- /dev/null +++ b/30 days of code Solutions/Day-4.cpp @@ -0,0 +1,56 @@ +using namespace std; +#include + +//-------------------------------------------------------------------------------------------------- + +class Person{ + public: + int age; + Person(int initialAge); + void amIOld(); + void yearPasses(); + }; + + Person::Person(int initialAge){ + if(initialAge < 0){ + age = 0; + cout << "Age is not valid, setting age to 0." << endl; + }else age = initialAge; + + } + + void Person::amIOld(){ + if(age < 13){ + cout << "You are young." << endl; + } + else if(age >= 13 && age < 18){ + cout << "You are a teenager." << endl; + }else cout << "You are old." << endl; + + } + + void Person::yearPasses(){ + age += 1; + + } + +//------------------------------------------------------------------------- + +int main(){ + int t; + int age; + cin >> t; + for(int i=0; i < t; i++) { + cin >> age; + Person p(age); + p.amIOld(); + for(int j=0; j < 3; j++) { + p.yearPasses(); + } + p.amIOld(); + + cout << '\n'; + } + + return 0; +} diff --git a/30 days of code Solutions/Day-5.cpp b/30 days of code Solutions/Day-5.cpp new file mode 100644 index 0000000..e19d585 --- /dev/null +++ b/30 days of code Solutions/Day-5.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + + +int main(){ + int n; + cin >> n; + for(int i = 1; i <= 10; ++i){ + cout << n << " x " << i << " = " << n * i << endl; + } + return 0; +} diff --git a/30 days of code Solutions/Day-6.cpp b/30 days of code Solutions/Day-6.cpp new file mode 100644 index 0000000..1396941 --- /dev/null +++ b/30 days of code Solutions/Day-6.cpp @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + + +int main() { + int n; + cin >> n; + for(int i = 0; i < n; ++i){ + string m; + cin >> m; + for(int k = 0; k < m.size(); k +=2){ + cout << m[k]; + } + cout << " "; + for(int t = 1; t < m.size(); t +=2){ + cout << m[t]; + } + cout << endl; + } + return 0; +} diff --git a/30 days of code Solutions/Day-7.cpp b/30 days of code Solutions/Day-7.cpp new file mode 100644 index 0000000..e6f2a96 --- /dev/null +++ b/30 days of code Solutions/Day-7.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace std; + + +int main(){ + int n; + cin >> n; + vector arr(n); + for(int arr_i = 0;arr_i < n;arr_i++){ + cin >> arr[arr_i]; + } + for (int i = arr.size() - 1; i >= 0; --i){ + cout << arr[i] << " "; + } + cout << endl; + return 0; +} diff --git a/30 days of code Solutions/Day-8.cpp b/30 days of code Solutions/Day-8.cpp new file mode 100644 index 0000000..b15deba --- /dev/null +++ b/30 days of code Solutions/Day-8.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +using namespace std; + + +int main() { + int x; + cin >> x; + map all; + for(int i = 0; i < x; ++i){ + string s, p; + cin >> s >> p; + all[s] = p; + } + string h; + while(cin >> h){ + if(all.find(h) == all.end()){ + cout << "Not found" << endl; + } + else{ + cout << h << "=" << all.at(h) << endl; + } + } +} diff --git a/30 days of code Solutions/Day-9.cpp b/30 days of code Solutions/Day-9.cpp new file mode 100644 index 0000000..91b41fe --- /dev/null +++ b/30 days of code Solutions/Day-9.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int factorial(int y) +{ + if (y == 1){ + return 1; + } + else{ + return y * factorial(y - 1); + } +} + +int main() { + int x; + cin >> x; + cout << factorial(x) << endl; + return 0; +}