## Try ….
char = input("Enter Your Name: ")
myDict = {}
for i in char:
myDict[i] = ord(i)
print(myDict)
» Binary To Decimal Convertor «
1.>
2.>
1.>
## using last digit of intiger...
def binary(n):
sum = 0
pow = 0
while n != 0:
n2 = n % 10
n //= 10
sum += (2 ** pow) * n2
pow += 1
return sum
print(binary(int(input("Enter a Binary Number: "))))
2.>
## using string length
def binary(n):
summ = 0
j = 0
i = len(n) - 1
while i >= 0:
summ += (2 ** j) * int(n[i])
j += 1
i -= 1
return summ
print(binary(input("Enter a Binary Digit: ")))
public static int binary(int n) {
int sum = 0, pow = 0;
while(n != 0) {
int n2 = n%10;
n = n / 10;
sum += Math.pow(2, pow)*n2;
pow++;
}
return sum;
}
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Binary Number: ");
int num = sc.nextInt();
System.out.print(binary(num));
}
Same Question using java
CᴏᴅɪɴɢNᴇʀᴅ 💸🐾
public static int binary(int n) { int sum = 0, pow = 0; while(n != 0) { int n2 = n%10; n = n / 10; sum += Math.pow(2, pow)*n2; pow++; } return sum; } public static void…
public static int binaryToDec(int n) {
int sum = 0, j = 0;
int i = String.valueOf(n).length() - 1;
while (i >= 0) {
sum += Math.pow(2, j)*Character.getNumericValue(String.valueOf(n).charAt(i));
j++;
i--;
}
return sum;
}
public static void main(String[] arg) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter binary Number: ");
int bin = sc.nextInt();
System.out.print(binaryToDec(bin));
}
👍1
Print Hollow Rectangle.>>
Code >>
* * * * *
* *
* *
* * * * *
Code >>
def hollow_pattern(row, col):
for i in range(1, row+1):
for j in range(1, col+1):
if i == 1 or i == row or j == 1 or j == col:
print('* ', end='')
else:
print(' ', end='')
print('')
hollow_pattern(
int(input("Enter the number of row: ")),
int(input("Enter the number of column: "))
)
» Decimal to binary and Binary to decimal convertor... «
import java.util.*;
public class decimaltoBinary {
public static int tobinary(int num) {
int binary = 0, j = 0;
while (num != 0) {
binary += (num % 2) * Math.pow(10, j);
num /= 2;
j++;
}
return binary;
}
public static int todecimal(int num) {
int decimal = 0, j = 0;
while (num !=0) {
int rem = num % 10;
decimal += rem * (int)(Math.pow(2, j));
num /= 10;
j++;
}
return decimal;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Type `0` for Decimal to binary.\nType `1` for Binary to decimal\n\nEnter your choice: ");
int choice = sc.nextInt();
if(choice == 0) {
System.out.print("Enter a decimal number: ");
int num = sc.nextInt();
System.out.println("The binary of " + num + " is " + tobinary(num));
} else if(choice == 1) {
System.out.print("Enter a binary number: ");
int num = sc.nextInt();
System.out.println("The decimal of " + num + " is " + todecimal(num));
} else {
System.out.println("Invalid choice!");
}
}
}
Output of both codes is »
Find Out the Difference between both codes
1234
123
12
1
Find Out the Difference between both codes
🔥1
You have given an array
Find the total pairs can be formed using this array and also print that all.
Print numbers of total pair exist in this array too.
Question 3: Java / C / C++ / Python / JavaScript.
**Note : For Python Treat this array as list then solve it.
{2, 3, 9, 6, 4, 5}
Find the total pairs can be formed using this array and also print that all.
Print numbers of total pair exist in this array too.
Question 3: Java / C / C++ / Python / JavaScript.
**Note : For Python Treat this array as list then solve it.
>> Write a program to print only next prime number. Example - input is 5 output is 7.
import math
def isPrime(n):
if n == 2:
return True
for i in range(2, int(math.sqrt(n))+1):
if n % i == 0:
return False
return True
def main():
num = int(input("Enter a number: "))
while True:
num += 1
if isPrime(num) == True:
print(num)
break
main()
Question 3 >> Solution!
import java.math.*;
public class TrappingRainwater {
public static int trappedRainwater(int arr[]) {
int trappedWater = 0;
int[] leftMax = new int[arr.length];
int[] rightMax = new int[arr.length];
// Auxiliary array
// left max
leftMax[0] = arr[0];
for(int i=1; i<arr.length; i++) {
leftMax[i] = Math.max(arr[i], leftMax[i-1]);
}
// right max
rightMax[arr.length-1] = arr[arr.length-1];
for(int i=arr.length-2; i>=0; i--) {
rightMax[i] = Math.max(rightMax[i+1], arr[i]);
}
// calculating rainwater
for(int i=0; i<arr.length; i++) {
int waterLevel = Math.min(leftMax[i], rightMax[i]);
trappedWater += Math.max(0, waterLevel-arr[i]);
}
return trappedWater;
}
public static void main(String[] arg) {
int[] array = {4, 2, 0, 6, 3, 2, 5};
int res = trappedRainwater(array);
System.out.print("Total trapped rainwater for\ngiven array is " + res);
}
}
Question 3 >> Solution!
# Calculate the rain water trapped by the gaps between the buildings
height = [4, 2, 0, 6, 3, 2, 5]
def trapping_rainwater(arr):
trapped_water = 0
right_max = [0] * len(arr)
left_max = [0] * len(arr)
# Auxiliary array
# left max boundary calculation.
left_max[0] = arr[0]
for i in range(1, len(arr)):
left_max[i] = max(arr[i], left_max[i-1])
# right max boundary calculation.
right_max[len(arr)-1] = arr[len(arr)-1]
for i in range(len(arr)-2, -1, -1):
right_max[i] = max(right_max[i+1], arr[i])
# Calculate the trapped water.
for i in range(len(arr)-1):
water_level = min(left_max[i], right_max[i])
trapped_water += max(0, water_level - arr[i])
print(trapped_water)
trapping_rainwater(height)
Question 4 : Buy & Sell Stocks
You are given an array prices where prices[i] is the price of a given stock
on the i'th day. You want to maximize your profit by choosing a single day to
buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you
cannot achieve any profit, return 0.
Using : Java / C / C++ / Python / JavaScript
You are given an array prices where prices[i] is the price of a given stock
on the i'th day. You want to maximize your profit by choosing a single day to
buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you
cannot achieve any profit, return 0.
price = {7, 1, 5, 3, 6, 4}
Using : Java / C / C++ / Python / JavaScript