mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Added basic decimal conversions (#40)
* Added basic decimal conversions * Updated basic decimal conversions
This commit is contained in:
parent
79e04baa43
commit
bb8c41b2de
@ -1,17 +0,0 @@
|
|||||||
#include<stdio.h>
|
|
||||||
#include<conio.h>
|
|
||||||
int main() {
|
|
||||||
int number, decimal_number, temp=1;
|
|
||||||
printf("Enter any binary number= ");
|
|
||||||
scanf("%d", &number);
|
|
||||||
int remainder;
|
|
||||||
while(number>0) {
|
|
||||||
|
|
||||||
remainder = number%10;
|
|
||||||
number = number/10;
|
|
||||||
decimal_number += remainder*temp;
|
|
||||||
temp = temp*2;//used as power of 2
|
|
||||||
|
|
||||||
}
|
|
||||||
printf("%d",decimal_number);
|
|
||||||
}
|
|
38
conversions/decimal2Binary.c
Normal file
38
conversions/decimal2Binary.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*********decimal number to binary number conversion*****************/
|
||||||
|
#include <stdio.h>
|
||||||
|
void decimal2Binary(long num);
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
long num;
|
||||||
|
|
||||||
|
printf("Enter a decimal integer \n");
|
||||||
|
scanf("%ld", &num);
|
||||||
|
decimal2Binary(num);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/***function for convert decimal numbers to binary numbers****************/
|
||||||
|
void decimal2Binary(long num){
|
||||||
|
|
||||||
|
long decimal_num, remainder, base, binary, no_of_1s;
|
||||||
|
|
||||||
|
base = 1;
|
||||||
|
binary = 0;
|
||||||
|
no_of_1s = 0;
|
||||||
|
|
||||||
|
while (num > 0)
|
||||||
|
{
|
||||||
|
remainder = num % 2;
|
||||||
|
|
||||||
|
if (remainder == 1)
|
||||||
|
{
|
||||||
|
no_of_1s++;
|
||||||
|
}
|
||||||
|
binary = binary + remainder * base;
|
||||||
|
num = num / 2;
|
||||||
|
base = base * 10;}
|
||||||
|
|
||||||
|
printf("Its binary equivalent is = %ld\n", binary);
|
||||||
|
}
|
46
conversions/decimal2Hexa.c
Normal file
46
conversions/decimal2Hexa.c
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*****Decimal to Hexadecimal conversion*******************/
|
||||||
|
#include <stdio.h>
|
||||||
|
void decimal2Hexadecimal(long num);
|
||||||
|
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
long decimalnum;
|
||||||
|
|
||||||
|
printf("Enter decimal number: ");
|
||||||
|
scanf("%ld", &decimalnum);
|
||||||
|
|
||||||
|
decimal2Hexadecimal(decimalnum);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/********function for convert decimal number to hexadecimal number****************/
|
||||||
|
void decimal2Hexadecimal(long num){
|
||||||
|
|
||||||
|
long decimalnum=num;
|
||||||
|
long quotient, remainder;
|
||||||
|
int i, j = 0;
|
||||||
|
char hexadecimalnum[100];
|
||||||
|
|
||||||
|
quotient = decimalnum;
|
||||||
|
|
||||||
|
while (quotient != 0){
|
||||||
|
|
||||||
|
remainder = quotient % 16;
|
||||||
|
if (remainder < 10)
|
||||||
|
hexadecimalnum[j++] = 48 + remainder;
|
||||||
|
|
||||||
|
else
|
||||||
|
hexadecimalnum[j++] = 55 + remainder;
|
||||||
|
|
||||||
|
quotient = quotient / 16;}
|
||||||
|
|
||||||
|
// print the hexadecimal number
|
||||||
|
|
||||||
|
for (i = j; i >= 0; i--){
|
||||||
|
printf("%c", hexadecimalnum[i]);}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
35
conversions/decimal2Octal.c
Normal file
35
conversions/decimal2Octal.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*****Decimal to octal conversion*******************/
|
||||||
|
#include <stdio.h>
|
||||||
|
void decimal2Octal(long decimalnum);
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
long decimalnum;
|
||||||
|
|
||||||
|
printf("Enter the decimal number: ");
|
||||||
|
scanf("%ld", &decimalnum);
|
||||||
|
|
||||||
|
decimal2Octal(decimalnum);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/********function for convert decimal numbers to octal numbers************/
|
||||||
|
void decimal2Octal(long decimalnum){
|
||||||
|
long remainder, quotient;
|
||||||
|
|
||||||
|
int octalNumber[100], i = 1, j;
|
||||||
|
quotient = decimalnum;
|
||||||
|
|
||||||
|
while (quotient != 0){
|
||||||
|
octalNumber[i++] = quotient % 8;
|
||||||
|
|
||||||
|
quotient = quotient / 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j = i - 1; j > 0; j--)
|
||||||
|
|
||||||
|
printf("%d", octalNumber[j]);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user