diff --git a/Decimal_2_Binary b/Decimal_2_Binary new file mode 100644 index 0000000..9779878 --- /dev/null +++ b/Decimal_2_Binary @@ -0,0 +1,20 @@ +#include +using namespace std; + +string decimal_to_binary(int n) +{ + string ans; + while(n>0){ + int current_bit = n&1; + ans+=current_bit+'0'; + n>>=1; + } + reverse(ans.begin(),ans.end()); + return ans; +} +int main() +{ + string binary = decimal_to_binary(244); + cout << binary << endl; + return 0; +} diff --git a/Jump_Search b/Jump_Search new file mode 100644 index 0000000..82e0e94 --- /dev/null +++ b/Jump_Search @@ -0,0 +1,52 @@ +#include +using namespace std; + +int jumpSearch(int arr[], int x, int n) +{ + // Finding block size to be jumped + int step = sqrt(n); + + // Finding the block where element is + // present (if it is present) + int prev = 0; + while (arr[min(step, n)-1] < x) + { + prev = step; + step += sqrt(n); + if (prev >= n) + return -1; + } + + // Doing a linear search for x in block + // beginning with prev. + while (arr[prev] < x) + { + prev++; + + // If we reached next block or end of + // array, element is not present. + if (prev == min(step, n)) + return -1; + } + // If element is found + if (arr[prev] == x) + return prev; + + return -1; +} + + +int main() +{ + int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, + 34, 55, 89, 144, 233, 377, 610 }; + int x = 55; + int n = sizeof(arr) / sizeof(arr[0]); + + // Find the index of 'x' using Jump Search + int index = jumpSearch(arr, x, n); + + // Print the index where 'x' is located + cout << "\nNumber " << x << " is at index " << index; + return 0; +} diff --git a/LinkedList_Pallindrome b/LinkedList_Pallindrome new file mode 100644 index 0000000..fb75f31 --- /dev/null +++ b/LinkedList_Pallindrome @@ -0,0 +1,85 @@ +#include +using namespace std; +class node +{ + public: + int data; + node* next; + node(){} + node(int val) + { + data=val; + next=NULL; + } +}; +node* insertlist(node* head) +{ + int c=1,i=1,val;node* temp=head; + while(c!=0) + { + cout<<"ENTER DATA OF NODE "<>val; + node *newnode=new node(val); + temp->next=newnode; + temp=newnode; + cout<<"ENTER 1 IF YOU WANT MORE NODES ELSE 0: "; + cin>>c; + i++; + } + head=head->next; + return head; +} +node* reverselist(node* head,node* newlist) +{ + node* temp=head->next;node* prev=head; + while(temp!=NULL) + { + node* head2=new node(); + head2->data=prev->data; + head2->next=newlist; + newlist=head2; + prev=temp; + temp=temp->next; + } + node* head2=new node(); + head2->data=prev->data; + head2->next=newlist; + newlist=head2; + return newlist; +} +void printlist(node* head) +{ + node* temp=head; + while(temp->next!=NULL) + { + cout<data<<"->"; + temp=temp->next; + } + cout<data; +} +void pallindromecheck(node* head,node* newlist) +{ + node* temp=head;node* temp2=newlist;int c=0; + while(temp!=NULL&&c==0) + { + if(temp->data!=temp2->data) + { + c++; + } + temp=temp->next; + temp2=temp2->next; + } + if(c==0) + cout<<"THE NUMBER IS PALLINDROME"; + else + cout<<"THE NUMBER IS NOT PALLINDROME"; +} +int main() +{ + node* newlist=NULL; + node *head=new node(0); + head=insertlist(head); + newlist=reverselist(head,newlist); + //printlist(head); + pallindromecheck(head,newlist); +} diff --git a/Ternary_Search b/Ternary_Search new file mode 100644 index 0000000..8fa1796 --- /dev/null +++ b/Ternary_Search @@ -0,0 +1,14 @@ +double ternary_search(double l, double r) { + double eps = 1e-9; //set the error limit here + while (r - l > eps) { + double m1 = l + (r - l) / 3; + double m2 = r - (r - l) / 3; + double f1 = f(m1); //evaluates the function at m1 + double f2 = f(m2); //evaluates the function at m2 + if (f1 < f2) + l = m1; + else + r = m2; + } + return f(l); //return the maximum of f(x) in [l, r] +} diff --git a/Year_month_day b/Year_month_day new file mode 100644 index 0000000..90d3410 --- /dev/null +++ b/Year_month_day @@ -0,0 +1,21 @@ +package clz; +import java.util.Scanner; +public class YearMonthDay { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter Days : "); + int n = sc.nextInt(); + + int year = n/365; + System.out.print("Your result is : "+year+" years "); + int year_rem = n%365; + + int month = year_rem/30; + System.out.print(month+" months "); + int month_rem = year_rem%30; + + System.out.println(month_rem+" days."); + + sc.close(); + }