Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to Swap two numbers without third variable #60 #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Code to Swap Two Numbers without using third variable
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* C++ Program to Swap Two Numbers without using third variable */

#include <iostream>
using namespace std;

int main()
{

int a,b ;

cout<<"Enter 1st number :: ";
cin>>a;
cout<<"\nEnter 2nd number :: ";
cin>>b;

cout << "\nBefore swapping, Numbers are :: " << endl;
cout << "\n\ta = " << a << ", b = " << b << endl;

a = a + b;
b = a - b;
a = a - b;

cout << "\nAfter swapping, Numbers are :: " << endl;
cout << "\n\ta = " << a << ", b = " << b << endl;

return 0;
}
55 changes: 55 additions & 0 deletions Sorting names in an alphabetical order.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bits/stdc++.h>
using namespace std;

//function to print the array
void print(vector<string> names){
printf("printing ........\n");
for(int i=0;i<names.size();i++)
cout<<names[i]<<endl;
printf("\n");
}

bool mycomp(string a, string b){
//returns 1 if string a is alphabetically
//less than string b
//quite similar to strcmp operation
return a<b;
}

vector<string> alphabaticallySort(vector<string> a){
int n=a.size();
//mycomp function is the defined function which
//sorts the strings in alphabatical order
sort(a.begin(),a.end(),mycomp);
return a;
}

int main()
{
int n;
printf("enter number of names to be added: ");
scanf("%d",&n);

//creating a vector of strings
//vector to store strings(names)
vector<string> names;
string name;
printf("enter names: \n");
//taking input
for(int i=0;i<n;i++){
cin>>name;
//insert names into the vector
names.push_back(name);
}

printf("\nbefore sorting\n");
print(names);

//function to sort names alphabetically
names=alphabaticallySort(names);

printf("after alphabetical sorting\n");
print(names);

return 0;
}