Skip to content

Instantly share code, notes, and snippets.

@PatchRowcester
PatchRowcester / any_number_to_binary.py
Created February 28, 2013 04:00
This python program will convert any given number to its binary form
#accept a number, and return its binary form
x = float(raw_input('Enter a number: '))
isNeg = False
if x < 0:
x = abs(x)
isNeg = True
resultInt = ''
resultFlt = ''
@PatchRowcester
PatchRowcester / decimal_fraction_to_binary.py
Created February 27, 2013 03:34
This is a way to convert a number between 0 & 1 to its binary form in Python
x = float(raw_input('Enter a number between 0 and 1: '))
p = 0
#if a number is a whole number, the reminder when divided by 1 is always 0
#if a number is not a whole number, the reminder when divided by 1 is NOT 0
#Because we are trying to convert a decimal to a whole number, this check is necessary
while (((2**p) * x)%1 !=0):
print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
p += 1
num = int(x*(2**p))
print str(p)
@PatchRowcester
PatchRowcester / while_number_check.py
Created February 27, 2013 02:40
Simplest way to check if an entered number is a whole number or not.
x = float(raw_input('Enter any number (decimal or integer)'))
if (x%1 ==0):
print('Whole number')
else:
print('Not a whole number')
@PatchRowcester
PatchRowcester / nearest_whole_number.py
Created February 27, 2013 01:27
A simple python program to accept a float and print its nearest whole number
x = float(raw_input('Enter a decimal number: '))
a = int(x)
c = (float(a + (a+1))/2)
#print ('c = ' + str(c))
if x >= c:
b = int(x) + 1
@PatchRowcester
PatchRowcester / int_to_binary.py
Last active December 14, 2015 04:09
This is python code to convert an integer to its binary form.
x = int(raw_input('enter an int: '))
isNeg = False
if x < 0:
x = abs(x)
isNeg = True
result =''
if x == 0:
result = '0'
while x>0:
result = str(x%2) + result
@PatchRowcester
PatchRowcester / FindWithinHorizon_Test.java
Created January 4, 2013 02:38
Accept a character and display it. Also, accept a string and look for two consecutive digits and display them.
import java.util.Scanner;
public class FindWithinHorizon_Test {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
char t,x;
@PatchRowcester
PatchRowcester / AssignmentOperators.java
Created January 2, 2013 10:20
Convoluted way to demonstrate assignment operators in Java
import java.util.Scanner;
public class AssignmentOperators {
public static void main(String args[]) {
int k = 5;
double d;
Scanner myScanner = new Scanner(System.in);
System.out.println("select the assignment operator you want to use: \n" + "1. addition\n" + "2. substraction\n" + "3. multiplication \n" + "4. division \n");