Hacking with conversions of decimal and binary

$./app Sign detection [1] Power of two [2] Counting No. of bits set [3] Set or clear bits without branching[4] Find maximum value[5] Finding least common multiple[6] Finding the greatest commom divisor[7] Finding if a number is an Armstrong number or not[8] Finding if a given number is prime number[9] Finding the number of twin …

How to write an algorithm to reverse an integer ?

/* Part of this work is copied from http://www.sanfoundry.com/c-program-to-reverse-a-given-number/ */ #include <stdio.h> revint() { long num, reverse = 0, temp, remainder; printf(“Enter the number \n”); scanf(“%ld”,&num); temp = num; while ( num > 0 ) { remainder = num % 10; reverse = reverse * 10 + remainder; num /= 10; } printf(“Given number = …

Hacking with an algorithm to reverse an integer

$./app Sign detection [1] Power of two [2] Counting No. of bits set [3] Set or clear bits without branching[4] Find maximum value[5] Finding least common multiple[6] Finding the greatest commom divisor[7] Finding if a number is an Armstrong number or not[8] Finding if a given number is prime number[9] Finding the number of twin …

How to write an algorithm to do a binary to decimal conversion ?

/* part of work copied from http://www.sanfoundry.com/c-program-binary-number-into-decimal/ */ #include <stdio.h> btd() { int num, binary_val, decimal_val = 0, base = 1, rem; printf(“Enter a binary number : “); scanf(“%d”,&num); binary_val = num; while ( num > 0 ) { rem = num % 10; decimal_val = decimal_val + rem * base; num = num / …

How to write an algorithm to swap values ?

/* part of work copied from http://www.sanfoundry.com/c-program-swap-values/ */ #include <stdio.h> swap_values() { int temp; int *ptr1, *ptr2; int a, b; printf(“Enter integer values to be swapped: “); scanf(“%d %d”,&a,&b); ptr1 = &a; ptr2 = &b; temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; printf(“Swapped values are: %d %d \n”,a,b); } /* http://www.programmingsimplified.com/c-program-swap-two-numbers */ /* …

Hacking with integer divisibility

/* work copied from http://www.sanfoundry.com/c-program-number-divisible-by-5/ */ #include <stdio.h> numberdivi() { int i, num1, num2, count = 0, sum = 0; printf(“Enter the value of num1 and num2 : “); scanf(“%d %d”,&num1,&num2); for (i = num1; i < num2 ; i++) { if ((i % 5) == 0) { printf(“%3d,”,i); count++; sum = sum + i; …