diff --git a/C++/Program 29/DecimaltoBinary.cpp b/C++/Program 29/DecimaltoBinary.cpp new file mode 100644 index 00000000..be854a50 --- /dev/null +++ b/C++/Program 29/DecimaltoBinary.cpp @@ -0,0 +1,33 @@ +// number to binary number + +#include +using namespace std; + +// function to convert decimal to binary +void decToBinary(int n) +{ + // array to store binary number + int binaryNum[32]; + + // counter for binary array + int i = 0; + while (n > 0) { + + // storing remainder in binary array + binaryNum[i] = n % 2; + n = n / 2; + i++; + } + + // printing binary array in reverse order + for (int j = i - 1; j >= 0; j--) + cout << binaryNum[j]; +} + +// Driver program to test above function +int main() +{ + int n = 17; + decToBinary(n); + return 0; +} \ No newline at end of file diff --git a/C++/Program 29/Readme.md b/C++/Program 29/Readme.md new file mode 100644 index 00000000..13485264 --- /dev/null +++ b/C++/Program 29/Readme.md @@ -0,0 +1 @@ +C++ program to convert a decimal. \ No newline at end of file diff --git a/C/program-83/README.md b/C/program-83/README.md new file mode 100644 index 00000000..c5f883bb --- /dev/null +++ b/C/program-83/README.md @@ -0,0 +1,2 @@ +## Program 83 +Program in C to represent polynomial expression in one variable using linked list. \ No newline at end of file diff --git a/C/program-83/polynomial.c b/C/program-83/polynomial.c new file mode 100644 index 00000000..2d1c0049 --- /dev/null +++ b/C/program-83/polynomial.c @@ -0,0 +1,67 @@ +#include +#include + +struct poly +{ + int coeff; + int exp; + struct poly *next; +}; + +void add(struct poly **p,int c,int e) // adding nodes for each order +{ + struct poly *new=*p,*temp; + new=(struct poly *)malloc(sizeof(struct poly)); + new->coeff=c; + new->exp=e; + new->next=NULL; + + if(*p==NULL) // if start is null + *p=new; + else + { + struct poly *temp=*p; + while(temp->next!=NULL) + temp=temp->next; + temp->next=new; + } + +} + +void display(struct poly **p) // for displaying each node that contains different order of X +{ +struct poly*temp=*p; + while(temp!=NULL) + { + printf("+%d",temp->coeff); + if(temp->exp!=0) + printf("x^%d",temp->exp); + temp=temp->next; + } +} + +int main() +{ + int co,ex; + char ch='y'; + struct poly *start=NULL; + printf("Enter the polynomial from highest order to lowest\n"); + while(ch=='y') + { + printf("Enter the coefficient of x:\n"); + scanf("%d",&co); + printf("enter the exponent of x:\n"); + scanf("%d",&ex); + add(&start,co,ex); //calling add function + printf("\n\tDo u want to add more (y/n)"); + getchar(); + scanf("%c",&ch); + + } + + printf("\n\tThe polynomial expression is:\t"); + display(&start); //final display + printf("\n"); + return 0; +} + diff --git a/C/program-83/program.c b/C/program-83/program.c new file mode 100644 index 00000000..a8c5759b --- /dev/null +++ b/C/program-83/program.c @@ -0,0 +1,29 @@ +/*program to calculate profit & loss in BITCOIN mining*/ + +#include + +int main(){ + + float current_btc_price, avg_cost; + float total_btc, profitloss; + + printf("Enter Your Total Bitcoin: "); + scanf("%f",&total_btc); + + printf("Enter Current Bitcoin Price: "); + scanf("%f",¤t_btc_price); + + printf("Your Average Bitcoin Purchase Price: "); + scanf("%f",&avg_cost); + + //profitloss = (current_btc_price - averagecost) x total_btc + + profitloss = (current_btc_price - avg_cost) * total_btc; + + if(profitloss > 0) + printf("Bitcoin Trading Profit is: %0.2f",profitloss); + else + printf("Bitcoin Trading Loss is: %0.2f",profitloss); + + return 0; +} \ No newline at end of file diff --git a/Interview Questions/Trapping Rain Water/README.md b/Interview Questions/Trapping Rain Water/README.md new file mode 100644 index 00000000..7af8f70d --- /dev/null +++ b/Interview Questions/Trapping Rain Water/README.md @@ -0,0 +1,12 @@ +Trapping Rain Water + + +Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. + +Input: arr[] = {3, 0, 2, 0, 4} +Output: 7 + +Explanation: +We can trap "3 units" of water between 3 and 2, +"1 unit" on top of bar 2 and "3 units" between 2 +and 4. \ No newline at end of file diff --git a/Interview Questions/Trapping Rain Water/Trapping Rain Water.cpp b/Interview Questions/Trapping Rain Water/Trapping Rain Water.cpp new file mode 100644 index 00000000..9376a0f8 --- /dev/null +++ b/Interview Questions/Trapping Rain Water/Trapping Rain Water.cpp @@ -0,0 +1,63 @@ +// C++ implementation of the approach +#include +using namespace std; + +int maxWater(int arr[], int n) +{ + + // indices to traverse the array + int left = 0; + int right = n-1; + + // To store Left max and right max + // for two pointers left and right + int l_max = 0; + int r_max = 0; + + // To store the total amount + // of rain water trapped + int result = 0; + while (left <= right) + { + + // We need check for minimum of left + // and right max for each element + if(r_max <= l_max) + { + + // Add the difference between + // current value and right max at index r + result += max(0, r_max-arr[right]); + + // Update right max + r_max = max(r_max, arr[right]); + + // Update right pointer + right -= 1; + } + else + { + + // Add the difference between + // current value and left max at index l + result += max(0, l_max-arr[left]); + + // Update left max + l_max = max(l_max, arr[left]); + + // Update left pointer + left += 1; + } + } + return result; +} + +// Driver code +int main() { + int arr[] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; + int n = sizeof(arr)/sizeof(arr[0]); + cout << maxWater(arr, n) << endl; + return 0; +} + + diff --git a/Java/Program-16/CSAOfCylinder.class b/Java/Program-16/CSAOfCylinder.class new file mode 100644 index 00000000..faaea7ba Binary files /dev/null and b/Java/Program-16/CSAOfCylinder.class differ diff --git a/Java/Program-16/CSAOfCylinder.java b/Java/Program-16/CSAOfCylinder.java new file mode 100644 index 00000000..3c2d563c --- /dev/null +++ b/Java/Program-16/CSAOfCylinder.java @@ -0,0 +1,14 @@ +package com.dsa; + +import java.util.Scanner; + +public class CSAOfCylinder { + public static void main(String[] args) { + Scanner input=new Scanner(System.in); + System.out.println("Enter radius and height of cylinder"); + float radius=input.nextFloat(); + float height=input.nextFloat(); + float CSA=2*3.14f*radius*height; + System.out.println("Curved surface area of cylinder is "+CSA+" square units"); + } +} \ No newline at end of file diff --git a/Java/Program-16/README.md b/Java/Program-16/README.md new file mode 100644 index 00000000..42d29ac9 --- /dev/null +++ b/Java/Program-16/README.md @@ -0,0 +1,3 @@ +Program 16 + +Program to find CSA of Cylinder \ No newline at end of file diff --git a/Java/Program-17/ReverseString.java b/Java/Program-17/ReverseString.java new file mode 100644 index 00000000..a9cabfbe --- /dev/null +++ b/Java/Program-17/ReverseString.java @@ -0,0 +1,17 @@ +package com.dsa; + +import java.util.Scanner; + +public class ReverseString { + public static void main(String[] args) { + Scanner input=new Scanner(System.in); + String reversed_string=""; + System.out.println("Enter the string"); + String string= input.next(); + for(int i=string.length()-1;i>=0;i--){ + reversed_string = reversed_string + string.charAt(i); + } + System.out.println("The original string is "+string); + System.out.println("The reversed string is "+reversed_string); + } +} \ No newline at end of file diff --git a/Java/Program-17/readme.md b/Java/Program-17/readme.md new file mode 100644 index 00000000..fa231970 --- /dev/null +++ b/Java/Program-17/readme.md @@ -0,0 +1,3 @@ +Program 17 + +Program to reverse a given string diff --git a/Java/Program-18/ElectricityBill.java b/Java/Program-18/ElectricityBill.java new file mode 100644 index 00000000..64823e3f --- /dev/null +++ b/Java/Program-18/ElectricityBill.java @@ -0,0 +1,18 @@ +//A consumer consumes n watts per hour daily for one month. +// Calculate the total energy bill of that consumer if per-unit rate is 7? +// ( In $, £, €, INR, DHR, Riyal etc.) [Take 1 month = 30 Days]. +package com.dsa; + +import java.util.Scanner; + +public class ElectricityBill { + public static void main(String[] args) { + Scanner input=new Scanner(System.in); + System.out.println("Enter the watts consumed per hour"); + int watts=input.nextInt(); + int electricity=watts*24*30;//Total watts of electricity consumed in 30 days + int cost=(electricity/1000)*7;//We divide by 1000 to convert watts to kilo-watts + //One unit=One kilo-watt and One unit costs 7 + System.out.println("The electricity bill is "+cost); + } +} diff --git a/Java/Program-18/readme.md b/Java/Program-18/readme.md new file mode 100644 index 00000000..250559c3 --- /dev/null +++ b/Java/Program-18/readme.md @@ -0,0 +1,3 @@ +Program-18 + +Program to calculate electricity bill \ No newline at end of file diff --git a/Java/Program-19/Discount.java b/Java/Program-19/Discount.java new file mode 100644 index 00000000..a8683a8b --- /dev/null +++ b/Java/Program-19/Discount.java @@ -0,0 +1,17 @@ +package com.dsa; + +import java.util.Scanner; + +public class Discount { + public static void main(String[] args) { + Scanner input=new Scanner(System.in); + System.out.println("Enter the original price of product"); + int original_price=input.nextInt(); + System.out.println("Enter the discount percentage"); + float discount=input.nextInt(); + float discount_price=original_price*(discount/100); + float final_price=original_price-discount_price; + System.out.println("The discount price is: "+discount_price); + System.out.println("The final price after discount is: "+final_price); + } +} diff --git a/Java/Program-19/readme.md b/Java/Program-19/readme.md new file mode 100644 index 00000000..a6d73f91 --- /dev/null +++ b/Java/Program-19/readme.md @@ -0,0 +1,3 @@ +Program 19 + +Program to calculate discount \ No newline at end of file diff --git a/Java/Program-20/BattingAverage.java b/Java/Program-20/BattingAverage.java new file mode 100644 index 00000000..bab41c5a --- /dev/null +++ b/Java/Program-20/BattingAverage.java @@ -0,0 +1,15 @@ +package com.dsa; + +import java.util.Scanner; + +public class BattingAverage { + public static void main(String[] args) { + Scanner input=new Scanner(System.in); + System.out.println("Enter the number of runs scored by the batter"); + int runs=input.nextInt(); + System.out.println("Enter the number of times the batter got out "); + int out= input.nextInt(); + int batting_avg=runs/out; + System.out.println("The batting average of the batter is "+batting_avg); + } +} diff --git a/Java/Program-20/readme.md b/Java/Program-20/readme.md new file mode 100644 index 00000000..f5185f0a --- /dev/null +++ b/Java/Program-20/readme.md @@ -0,0 +1,3 @@ +Program 20 + +Program to calculate batting average diff --git a/Java/Readme.md b/Java/Readme.md index 210a50d1..a3a554ab 100644 --- a/Java/Readme.md +++ b/Java/Readme.md @@ -17,6 +17,13 @@ | [Program-11](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-11/Program.java) | Program for compound interest | [Program-12](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-12/Program-12.java) | Print number of vowels and consonants in a string | [Program-13](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-13/Program%20Number%20to%20Word.java) | Program to convert number into words +| [Program-14](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-14/Program%20Number%20to%20Word.java) | Program to convert decimal to binary +| [Program-15](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-15/Program%20Number%20to%20Word.java) | Program to convert decimal number to equivalenr binary number +| [Program-16](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-16/Program%20Number%20to%20Word.java) | Program to find CSA of cylinder +| [Program-17](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-17/Program%20Number%20to%20Word.java) | Program to reverse a given string +| [Program-18](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-18/Program%20Number%20to%20Word.java) | Program to calculate electricity bill +| [Program-19](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-19/Program%20Number%20to%20Word.java) | Program to calculate discount +| [Program-20](https://github.com/swaaz/basicprograms/blob/814a1e60ae23d81158d8174666f23c9b7419e15e/Java/Program-20/Program%20Number%20to%20Word.java) | Program to calculate batting average