Skip to content

Commit

Permalink
C++ code for day1 (#177)
Browse files Browse the repository at this point in the history
* factorial in c++

* fibonacci recursion

* c++ code for day14

* C++ solution for day15 added

* day1 FizzBuzz Code in C++

* C++ code for day2
  • Loading branch information
Karthikn2099 authored and MadhavBahl committed Jan 15, 2019
1 parent bcf5557 commit 0ce465a
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Day1/C++/FizzBuzz_Program.cpp
@@ -0,0 +1,77 @@
/**
* @author: Karthick < karthikn2099@gmail.com >
* @github: https://github.com/karthikn2098
* @date: 15/01/2018
*/

#include <iostream>
using namespace std;

void fizzBuzz1(int N) {

for( int i = 1 ; i <= N ; i++ ) {

if( (i % 3 == 0) && (i % 5 == 0) ) {
cout << "FizzBuzz ";
}
else if( i % 3 == 0 ) {
cout << "Fizz ";
}
else if( i % 5 == 0 ) {
cout << "Buzz ";
}
else {
cout << i << " ";
}

}
}

void fizzBuzz2(int N) {

for( int i = 1 ; i <= N ; i++ ) {

string str = "";

if(i % 3 == 0) str += "Fizz";

if(i % 5 == 0) str += "Buzz";

if(str == "") {
cout << i << " ";
}
else {
cout << str << " ";
}

}

}

int main(void) {

int N;

cout << "Enter N: ";
cin >> N;

fizzBuzz1(N);
cout << endl << endl;

fizzBuzz2(N);
cout << endl << endl;

return 0;
}

/**
---------------------
Sample Input/Output
---------------------
Enter N: 25
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
*/
66 changes: 66 additions & 0 deletions Day2/C++/string_reverse_palindrome.cpp
@@ -0,0 +1,66 @@
/**
* @author: Karthick < karthikn2099@gmail.com >
* @github: https://github.com/karthikn2098
* @date: 15/01/2018
*/

#include <iostream>
#include <string>
using namespace std;

//function to reverse the string.
string reverse_string( string str ) {

int i = 0 , j = str.length() - 1;

while(i <= j) {
swap(str[i++] , str[j--]);
}

return str;
}

//function that checks the string is palindrome or not.
bool checkPalindrome( string str ) {

int i = 0 , j = str.length() - 1;

while( i <= j ) {
if( str[i++] != str[j--] ) {
return false;
}
}

return true;
}


int main(void) {

cout << "Reverse of abcd: " << reverse_string("abcd");

cout << "\n\nReverse of abcde: " << reverse_string("abcde");

cout << endl;

if( checkPalindrome("amma") ) {
cout << "\nPalindrome\n";
}
else {
cout << "\nNot a palindrome\n";
}


return 0;
}

/**
Output:
Reverse of abcd: dcba
Reverse of abcde: edcba
Palindrome
*/

0 comments on commit 0ce465a

Please sign in to comment.