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

Add files via upload #93

Open
wants to merge 1 commit 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
71 changes: 30 additions & 41 deletions BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@

#include <iostream>
using namespace std;

template<class T>
int bSearch(T arr[],int l, int r,T ele)
{
int mid = (l+r)/2;
if(l<r)
{
if(arr[mid] > ele)
bSearch(arr,l,mid-1,ele);
else if(arr[mid] < ele)
bSearch(arr,mid+1,r,ele);
else
return mid;
}

using namespace std;
int bin_search(int a[],int n,int num){
int first=0,last=n-1;
int flag=0;
while(first<=last){
int mid=(first+last)/2;
if(a[mid]<num)
first=mid+1;
else if(a[mid]>num)
last=mid-1;
else{
flag=1;
return flag;
}
}
return flag;
}

int main()
{
int pos,n,key,i;
cout<<"Enter the size of the array : ";
cin>>n;
int arr[n];
cout<<"Enter the "<<n<<" elements : ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter the element to be found : ";
cin>>key;
pos = bSearch(arr,0,n-1,key);
if(pos != -1)
cout<<"Element is present at "<<pos<<" position";
else
cout<<"Element is not present";

return 0;
}


int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int num;
cin>>num;
int flag=bin_search(a,n,num);
if(flag==1)
cout<<"Found";
else cout<<"Not Found";
return 0;
}
Binary file added BinarySearch.exe
Binary file not shown.
Binary file added BinarySearch.o
Binary file not shown.
42 changes: 13 additions & 29 deletions LeapYear.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
#include <iostream>
using namespace std;

int main()
{
int year;

cout<<"Program to Verify Leap Year...\n";

cout << "Enter a Year: ";
cin >> year;

if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << "Year " << year << " is a Leap Year.";

else
cout << "Year " << year << " is NOT a Leap Year.";
}
else
cout << "Year " << year << " is a Leap Year.";
}
else
cout << "Year " << year << " is not a Leap Lear.";

return 0;

}
int main(){
int yr;
cin>>yr;
if(yr%4==0 && yr%100==0){
if(yr%400==0)
cout<<"Leap Year";
else cout<<"Not leap year";
}
if(yr%4==0 && yr%100!=0){
cout<<"Leap Year";
}
return 0;
}
Binary file added LeapYear.exe
Binary file not shown.
Binary file added LeapYear.o
Binary file not shown.
52 changes: 17 additions & 35 deletions Palindrome.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
/*
The following code checks if the given number is a palindrome or not.
*/


#include <iostream>

using namespace std;

int main()
{

// o --> original number
// r --> reversed number

int n, r= 0, remainder, o;
cout<<"Enter an number: ";
cin>>n;
o = n;

while (n != 0)
{
remainder = n % 10;
r = r * 10 + remainder;
n /= 10;
}


if (o == r)
cout<<o<<" is a Palindrome.";
else
cout<<o<<" is not a Palindrome.";

return 0;
}
#include<iostream>
using namespace std;
int main(){
int num;
cin>>num;
int save=num;
int sum=0;
while(num!=0){
int t=num%10;
sum=10*sum+t;
num/=10;
}
if(save==sum)
cout<<"Palindrome";
else cout<<"Not Palindrome";
return 0;
}
Binary file added Palindrome.exe
Binary file not shown.
Binary file added Palindrome.o
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Cpp-Programs
This repository contains all the basic c++ programs.
13 changes: 5 additions & 8 deletions Reverse a string.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "string";


reverse(str.begin(), str.end());

cout << str;
int main(){
string s;
getline(cin,s);
reverse(s.begin(),s.end());
cout<<s;
return 0;
}
Binary file added Reverse a string.exe
Binary file not shown.
Binary file added Reverse a string.o
Binary file not shown.
30 changes: 8 additions & 22 deletions Reversing An Array.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{

int arr[] = { 1, 45, 54, 71, 76, 12 };


int n = sizeof(arr) / sizeof(arr[0]);


cout << "Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";


reverse(arr, arr + n);


cout << "\nReversed Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=n-1;i>=0;i--)
cout<<a[i]<<"\t";
return 0;
}
Binary file added Reversing An Array.exe
Binary file not shown.
Binary file added Reversing An Array.o
Binary file not shown.
42 changes: 18 additions & 24 deletions SumofRows.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
#include <iostream>
using namespace std;

int main()
{
int i,j, m, n;
cout<<"\nEnter number of rows and coloumns : ";
cin>>m>>n;
int arr[m][n];
cout<<"\nEnter elements of matrix : \n";
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin>>arr[i][j];

cout << "\nFinding Sum of each row:";
int sum=0;

for (i = 0; i < m; ++i) {s
for (j = 0; j < n; ++j) {
sum = sum + arr[i][j];
}
cout<< "\nSum of the row "<< i << " = " << sum << endl;
sum = 0;
}

int main(){
int m,n;
cin>>m>>n;
int a[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++)
cin>>a[i][j];
}
int sum=0;
int i=0;
for(int i=0;i<m;i++){
sum=0;
for(int j=0;j<n;j++){
sum=sum+a[i][j];
}
cout<<sum<<"\t";
}
return 0;
}
}
Binary file added SumofRows.exe
Binary file not shown.
Binary file added SumofRows.o
Binary file not shown.
36 changes: 19 additions & 17 deletions fibonacci.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#include <iostream>
using namespace std;

int main(){
int num1=0,num2=1,num3,i,n;
cout<<"\nEnter the number of elements of Fibonacci Series : ";
cin>>n;
cout<<num1<<' '<<num2<<' '; //First print 0 and 1

for(i=2;i<n;++i){
num3=num1+num2;
cout<<num3<<' ';
num1=num2;
num2=num3;
}
return 0;
}
#include <iostream>
using namespace std;
int fib(int N){
int a=0,b=1;
int sum=0;
cout<<a<<"\t"<<b<<"\t";
for(int i=0;i<N;i++){
sum=a+b;
cout<<sum<<"\t";
a=b;
b=sum;
}
}
int main(){
int n;
cin>>n;
fib(n-2);
return 0;
}
Binary file added fibonacci.exe
Binary file not shown.
Binary file added fibonacci.o
Binary file not shown.
64 changes: 32 additions & 32 deletions kthLargestElement.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
#include <iostream>

using namespace std;

int main()
{
int a[50] , n , temp, k , i , j;
cout<<"Enter the size of the array : ";
cin>>n;
cout<<"Enter the value of k : ";
cin>>k;
for(i=0;i<n;i++)
{
cout<<"Enter the "<<i+1<<" element : ";
cin>>a[i];
}

for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
cout<<k<<" th largest number is "<<a[n-k];
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int get_max(int a[], int n) {
int maxm=INT_MIN;
for (int i = 0; i < n; i++) {
maxm = max(maxm, a[i]);
}
for(int i=0;i<n;i++){
if(maxm==a[i])
return i;
}
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
int k;
cout << "which largest element:";
cin >> k;
for (int i = 1; i <= k - 1; i++) {
int t = get_max(a, n);
a[t]=INT_MIN;
}
int maxm = INT_MIN;
for (int i = 0; i < n; i++) {
maxm = max(maxm, a[i]);
}
cout << endl;
cout << maxm;
}
Binary file added kthLargestElement.exe
Binary file not shown.
Binary file added kthLargestElement.o
Binary file not shown.