Skip to content

#10 Create binary_to_Decimal_Conversion Method in Maths Function # #125

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

Open
wants to merge 3 commits into
base: master
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
22 changes: 22 additions & 0 deletions maths/binary_to_Decimal_conversion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @function binaryToDecimal
* @description Convert the binary to decimal .
* @param {number} binary - The input string
* @return {string} - decimal of binary.
* @example binaryToDecimal('1011') = 11
* @example binaryToDecimal('1110') = 14
*/

function binaryToDecimal(binary: string): number {
let decimal: number = 0;
let power: number = 0;
for (let i = binary.length - 1; i >= 0; i--) {
if (binary[i] === '1') {
decimal += Math.pow(2, power);
}
power++;
}
return decimal;
}


27 changes: 27 additions & 0 deletions maths/prime_check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* @function isPrime
* @description program to check if a number is prime or not
* @see [Prime Check](https://www.programiz.com/javascript/examples/prime-number)
* @example Prime Number -> 5,7,11,13,17
* @param {num} number
*/

function isPrime(num: number): boolean {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}


const num: number = 17;
if (isPrime(num)) {
console.log(`${num} is prime`);
} else {
console.log(`${num} is not prime`);
}