Posts

Showing posts with the label cpp

[ FUN CODE ] Couple Or Friend Finder

Image
This is a fun code which is based on love quotient. The love quotient of the names is calculated. If the difference of the love qoutients of the two names is 0, 4, 16, or 32, the pair is declared a COUPLE or else, they are declared as FRIEND. The bellow code is implemented in C++ To calculate the love qoutient of a name individually, take the product of the letters, considering A as 1, Z as 26 and so on. Then perform modulus 36 with that number. For example, DAVE can be calculated as: D * A * V * E = (4 * 1 * 22 * 5) % 36 = 440 % 36 = 8 Screenshot The Code #include<iostream> #include<string.h> #include<cmath> using namespace std; string upper(string x) { for(int i=0;i<x.size();i++) { x[i]=toupper(x[i]); } return x; } void call(string x, string y) { int yc=1,xc=1; for(int i=0;i<x.size();i++) { xc*=((int)x[i]-64); } for(int i=0;i<y.size();i++) { yc*=((int)y[i]-64); } xc=xc%36; yc=yc%36; int dif=std::abs(xc-yc); if(dif==0 || dif=...

Implementation of Vector in c++

Image
Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. Screenshot :  Code : #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int t; vector<int> a; for(int i=0;i<6;i++) { int t; cin>>t; a.push_back(t); } if(a.empty()) { cout<<"Vector is empty"<<endl; return 0; } sort(a.begin(),a.end()); for(int i=0;i<6;i++) { cout<<a[i]<<" "; } cout<<"\nThe size of vector is "<<a.size(); return 0; }

Simple Factorial program for larger numbers

Image
It is a totally different approach, we are just playing with double. As we know double has very large value 4.9e-324 to 1.8e+308. Then we convert the double into an integer array. Screenshot: OUTPUT The Code: #include<iostream> #include<stdlib.h> #include<math.h> using namespace std; /*Classic factorial function to find the factorial of a number using recursion method*/ double factorial(int n) { if(n==1) return 1; else return (factorial(n-1)*n); } int main() { int n; cout<<"factorial of: "; cin>>n; double r = factorial(n); double s = r; int i=0; /*to find the number of digits of the answer*/ for(i=0; r>=1; i++) { r /= 10; } cout<<"number of digits: "<<i<<endl; /*creating and dynamically allocated array*/ int *a = new int[370]; double t,u; int k; int l=0; /*type casting digit by digit of the answer and symultaneously storing into the array*/ for(int j=i-1; j>0; j--) { ...

Merging two files into one file

Image
Some time we need to merge two or more file written by many ones. Then we can simply use such a lite and small program to merge many files into one. Screenshots: The output of the program View of the file one View the file two View the merged file here The code: #include<iostream> #include<fstream> #include<string.h> using namespace std; int main(int argc, char *argv[]) { if ( argc != 4 ) cout<<"give 3 file as command line argument"<<endl; else { ifstream file1; /*object created for file1*/ file1.open(argv[1]); /*Name of the file1 passed through command line argument*/ string line1; /*String is created to store all the data of the file1*/ char c1 = file1.eof(); /*EOF file1 is stored in a character variable*/ getline(file1,line1,c1); /*scanning and storing the data into the string*/ cout<<"The first file is opening here:\n\n"; cout<<line1<<endl; /*Simil...

File Handling in Cpp [Read & Write]

Image
This is a program which will perform the File Handling operations in Cpp program. In this a txt file, data.txt is created in which the input is stored and then read and displayed. Screenshots [Demo] The Terminal window in which the data is entered and then fetched The file data.txt, generated by the program ! The Code #include<iostream> #include<fstream> using namespace std; int main() { fstream fp; char st[50]; fp.open("data.txt",ios::out|ios::binary); for (int i=0;i<4;i++) { cout<<"Enter THe String: "; gets(st); fp<<st<<endl; } fp.close(); fp.open("data.txt"); cout<<"Displaying the File: "; while (!fp.eof()) { fp>>st; cout<<st<<" "; } fp.close(); return 0; } Got problems, you can comment it !

Constructor overloading in C++

Image
                          Well hello guys, I am back again with a key concept of C++ programming language. This program is all about constructor overloading concept. Even if it is a very simple concept, one should have knowledge on it. Have a look at the concept explained below. Concept:                            Constructor is a special feature in C++ whose task is to initialize objects of its class. It is called constructor because it constructs the values of data members of the class. Constructors carry the same name of the class. When we initialize any object of the class, then we can pass values through the constructor during initialization only. Constructor overloading is that thing when in one class we use constructors with different no. of parameters. i.e. the when creating objects of the class, the number of arguments it will have, accor...

Hackerrank Sherlock and array solution

Image
The below program is the solution of Sherlock and array in hackerrank. Screenshot[Demo] :  Code : #include<iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int x,l=0,r=0,flag=0; cin>>x; int a[x]; for(int i=0;i<x;i++) { cin>>a[i]; r+=a[i]; } for(int i=0;i<x;i++) { l=l+a[i-1]; r=r-a[i]; cout<<l<<" "<<r<<" "<<i<<endl; if(l==r) { flag=1; break; } } if(flag==1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }

Hackerrank Angry Professor Solution

Image
The below program is the solution of Angry Professor in Implementation sub-domain. You can get the question  HERE . Output : Code : #include<iostream> using namespace std; int main() { int t,x,y,c=0,m; cin>>t; while(t--) { cin>>x>>y; while(x--) { cin>>m; if(m<=0) c++; } if(c>=y) cout<<"NO"<<endl; else cout<<"YES"<<endl; c=0; } return 0; }

Hackerrank Caeser Cipher Solution

Image
The below program is the solution of Caeser Cipher in implementation domain. You can get the program  HERE. Output :  Code : #include<iostream> using namespace std; int main() { int x,t,m; cin>>x; string s; cin>>s; cin>>t; while(t>26) { t=t-26; } for(int i=0;i<s.length();i++) { m=s[i]+t; if(islower(s[i])) { if(m>122) { m=m-122; m+=96; } } else if(isupper(s[i])) { if(m>90) { m=m-90; m+=64; } } //cout<<m; if(isalpha(s[i])) cout<<(char)m; else cout<<s[i]; } return 0; }

Operator Overloading in C++

Image
This is a simple program in c++ in which the operators will be overloaded. In this program add,sub mul and div operator is overloaded ! Screenshots : The Program: #include<iostream> using namespace std; class mat { public: int a; mat(int val) { a=val; } mat() { } mat operator +(mat &b) { return mat(a+b.a); } mat operator -(mat &b) { return mat(a-b.a); } mat operator /(mat &b) { return mat(a/b.a); } mat operator *(mat &b) { return mat(a*b.a); } void disp() { cout<<a<<endl; } }; class inc { public: int x; inc (int val) { x=val; } friend inc operator ++(inc &,int); void disp() { cout<<x<<endl; } }; inc operator ++(inc &a,int) { return inc(a.x+1); } int main() { mat a1(4),a2(6),a3; a3=a2+a1; a3.disp(); a3=a2-a1; a3.disp(); a3=a2*a1; a3.disp(); a3=a2/a1; a3.disp(); inc i1(5); i1++; i1.disp(); } Any Suggestions ? Put them he...

Sort using algorithm header

Image
The below program takes an array as input and prints the sorted array.  The header  <algorithm>  defines a collection of functions especially designed to be used on ranges of elements.sort function takes two arguments. The arguments are the start index and end index of array. This header file does not work in c. It can be only used in C++. Output :  Code : #include<iostream> #include<algorithm> using namespace std; int main() { int n; cout<<"Enter number of elements you want to sort : "; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); for(int i=0;i<n;i++) cout<<a[i]<<" "; return 0; }

Hackerrank Chocolate Feast Solution

Image
This is the solution of Chocolate Feast under implementation sub domain. You can refer the question  Here. Output :   Code : #include<iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int x,y,z; cin>>x>>y>>z; int w,c=x/y; w=c; while(w>=z) { int m=w/z; c+=m; w=w%z; w+=m; } cout<<c<<endl; } } Well this is it for now..! Thanks for stopping by.. I hope you have liked the program. In case if you have any query you can type it in the comment box or you can contact with me, the contact details are in my profile.      Please don't forget to subscribe.. The subscription is free ;)

Hackerrank Cut The Stick Solution

Image
This is the solution of cut the stick problem under implementation sub domain. You can refer the question Here . Output : Code : #include<iostream> #include<algorithm> using namespace std; int main() { int mi,n,c=0; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); int i=0; while(a[n-1]>0) { c=0; int x=a[i]; if(a[i]>0) { for(int i=0;i<n;i++) a[i]=a[i]-x; c=n-i;cout<<c<<endl; } else { i++; } //cout<<endl; } return 0; }

Hackerrank Mark and Toys Solution

Image
This is a solution of Mark and Toys under Greedy sub-domain in Hackerrank. For question you can refer Here Output : Code : #include<bits/stdc++.h> using namespace std; int main() { int k,s=0;int n; cin>>n>>k; //cout<<n<<" "<<k; vector<int> a; for(int i=0;i<n;i++) { int x; cin>>x; a.push_back(x); } sort(a.begin(),a.end()); int c=0; for(int i=0;i<n;i++) { if(s<=k) { s=s+a[i]; c++; } else break; } cout<<c-1; return 0; }

Hackerrank Grid Challenge Solution

Image
This is the solution of Grid Challenge in Greedy sub-domain in Hackerrank. For the question you refer Here . Output : Code : #include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { int n; cin>>n; vector<vector<char>> s; for(int j=0;j<n;j++) { vector<char> t; for(int i=0;i<n;i++) { char ch; cin>>ch; t.push_back(ch); } s.push_back(t); } for(int i=0;i<n;i++) { sort(s[i].begin(),s[i].end()); } int flag=0; for(int i=0;i<n;i++) { for(int j=0;j<n-1;j++) { if(s[j][i]>s[j+1][i]) { flag=1; break; } } } if(flag==0) cout...

Sort using vector in c++

Image
The below program prints the input numbers in a sorted order.The program is implemented using c++. Output : Code : #include<bits/stdc++.h> using namespace std; int main() { vector<int> s; int n; cout<<"Enter number of elements : "; cin>>n; for(int i=0;i<n;i++) { int t; cin>>t; s.push_back(t); } sort(s.begin(),s.end()); cout<<"Sorted Order :"<<endl; for(int i=0;i<n;i++) cout<<s[i]<<" "; return 0; }

Create a Tic tac toe game using c++

Image
The following program is the source code of a game called tic tac toe. The program is implemented using c++. Code : #include<iostream> #include<windows.h> using namespace std; char a[9]={'1','2','3','4','5','6','7','8','9'}; char c1,c2; void draw_board() { cout<<"\n\n"; cout<<" "<<a[0]<<" | "<<a[1]<<" | "<<a[2]<<" "; cout<<endl<<" | | "; cout<<"\n-------|-------|-------"; cout<<endl<<" "<<a[3]<<" | "<<a[4]<<" | "<<a[5]<<" "; cout<<endl<<" | | "; cout<<"\n-------|-------|-------"; cout<<endl<<" "<<a[6]<<" ...

Find out the factorial of large number

Image
The following program prints the factorial of large number. The program is implemented using c++ and uses Dynamic Memory! Code : #include<iostream> using namespace std; int multiply(int x, int res[], int res_size); void factorial(int n) { int *res= new int[n]; res[0] = 1; int res_size = 1; for (int x=2; x<=n; x++) res_size = multiply(x, res, res_size); cout << "Factorial of "<<n<<" number is : "; for (int i=res_size-1; i>=0; i--) cout << res[i]; } int multiply(int x, int res[], int res_size) { int ca = 0; for (int i=0; i<res_size; i++) { int prod = res[i] * x + ca; res[i] = prod % 10; ca = prod/10; } while (ca) { res[res_size] = ca%10; ca = ca/10; res_size++; } return res_size; } int main() { int n; cout<<"Enter a number : "; cin>>n; factorial(n); return 0; }...

A Simple Encryption and Decryption Program [Cryptography]

Image
In this post you can learn the basic concept of Cryptography. This program accepts a string as the input and performs encryption with the key provided by the programmer. This is a basic Encryption method so it just involves changing each single character of the String with some simple algebric operations with the key. It can be added with each and every single character of the string, so can be subtracted or divided. But note Your have to apply reverse method. Sample Outputs: The Code: #include<iostream> #include<cstring> using namespace std; void encrypt(string *s,int key) { int i=0; while((*s)[i]!=NULL) { (*s)[i]=(int)(*s)[i]-key; //cout<<(*s)[i]<<endl; //Remove The Comments to perform step by step analysis ! i++; } } void decrypt(string *s,int key) { int i=0; while((*s)[i]!=NULL) { (*s)[i]=(int)(*s)[i]+key; //cout<<(*s)[i]<<endl; //Remove The Comments to perform step by step analysis ! i++; } } int main() { strin...

Check whether a number is perfect or not

Image
The below program takes a integer as input and checks whether the input is a perfect number or not. Perfect number is a positive integer which is the sum of its proper positive divisor which means the sum of its positive divisors excluding the number itself. Ex: 6 is a perfect number as it is sum of proper positive number. 6=1+2+3 28 is a perfect number as it is sum of proper positive number. 28=1+2+4+7+14 Code: #include<iostream> using namespace std; int main() { int n,r=0; cout<<"Enter a number : "; cin>>n; for(int i=1;i<n;i++) { if(n%i==0) r=r+i; } if(r==n) cout<<n<<" is a perfect number."; else cout<<n<<" is not a perfect number."; return 0; } Output: