diff --git a/README.md b/README.md index 5f720515..86e94ad4 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ C - decimal_to_hexa - decimal_to_octal - to_decimal + - hexa_to_octal + ## Data Structures - stack @@ -73,19 +75,20 @@ C ## Misc + - ArmstrongNumber - Binning - Factorial - Fibonacci + - Greatest Common Divisor - isArmstrong - LongestSubSequence - palindrome + - prime factorization - QUARTILE - rselect - strongNumber - - TowerOfHanoi - - Greatest Common Divisor - - Sudoku Solver - - prime factorization + - Sudoku Solver + - TowerOfHanoi ## Project Euler - Problem 1 diff --git a/conversions/hexal_to_octal.c b/conversions/hexal_to_octal.c new file mode 100644 index 00000000..097ba87c --- /dev/null +++ b/conversions/hexal_to_octal.c @@ -0,0 +1,132 @@ +/* C program to convert Hexadecimal to Octal number system */ + +#include + +int main() +{ + char hex[17]; + long long octal, bin, place; + int i = 0, rem, val; + + /* Input hexadecimal number from user */ + printf("Enter any hexadecimal number: "); + gets(hex); + + octal = 0ll; + bin = 0ll; + place = 0ll; + + /* Hexadecimal to binary conversion */ + for(i=0; hex[i]!='\0'; i++) + { + bin = bin * place; + + switch(hex[i]) + { + case '0': + bin += 0; + break; + case '1': + bin += 1; + break; + case '2': + bin += 10; + break; + case '3': + bin += 11; + break; + case '4': + bin += 100; + break; + case '5': + bin += 101; + break; + case '6': + bin += 110; + break; + case '7': + bin += 111; + break; + case '8': + bin += 1000; + break; + case '9': + bin += 1001; + break; + case 'a': + case 'A': + bin += 1010; + break; + case 'b': + case 'B': + bin += 1011; + break; + case 'c': + case 'C': + bin += 1100; + break; + case 'd': + case 'D': + bin += 1101; + break; + case 'e': + case 'E': + bin += 1110; + break; + case 'f': + case 'F': + bin += 1111; + break; + default: + printf("Invalid hexadecimal input."); + } + + place = 10000; + } + + place = 1; + + /* Binary to octal conversion */ + while(bin > 0) + { + rem = bin % 1000; + + switch(rem) + { + case 0: + val = 0; + break; + case 1: + val = 1; + break; + case 10: + val = 2; + break; + case 11: + val = 3; + break; + case 100: + val = 4; + break; + case 101: + val = 5; + break; + case 110: + val = 6; + break; + case 111: + val = 7; + break; + } + + octal = (val * place) + octal; + bin /= 1000; + + place *= 10; + } + + printf("Hexadecimal number = %s\n", hex); + printf("Octal number = %lld", octal); + + return 0; +} diff --git a/misc/ArmstrongNumber.c b/misc/ArmstrongNumber.c new file mode 100644 index 00000000..ced9b9f8 --- /dev/null +++ b/misc/ArmstrongNumber.c @@ -0,0 +1,64 @@ +//A number is called as Armstrong number if sum of cubes of digits of number is equal to the number itself. +// For Example 153 is an Armstrong number because 153 = 1³+5³+3³. +#include + +//Function to calculate x raised to the power y +int power(int x, unsigned int y) +{ + if (y == 0) + return 1; + if (y % 2 == 0) + return power(x, y / 2) * power(x, y / 2); + return x * power(x, y / 2) * power(x, y / 2); +} + +//Function to calculate order of the number +int order(int x) +{ + int n = 0; + while (x) { + n++; + x = x / 10; + } + return n; +} + +// Function to check whether the given number is +// Armstrong number or not +int isArmstrong(int x) +{ + // Calling order function + int n = order(x); + int temp = x, sum = 0; + while (temp) + { + int r = temp % 10; + sum += power(r, n); + temp = temp / 10; + } + + // If satisfies Armstrong condition + if (sum == x) + return 1; + else + return 0; +} + +// +int main() +{ + int x = 153; + if (isArmstrong(x) == 1) + printf("True\n"); + else + printf("False\n"); + + x = 1253; + if (isArmstrong(x) == 1) + printf("True\n"); + else + printf("False\n"); + + return 0; +} +