Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int DecToBin(void){
- int n, o, i = 0;
- int *data;
- printf("Enter number: ");
- scanf("%d" , &n);
- printf("Dec to bin: ");
- while(n){
- o = n%2;
- if(o == 1){
- data[i] = 1;
- }
- else{
- data[i] = 0;
- }
- i++;
- n /= 2;
- }
- for(i - 1; i >= 0; i--){
- if(data[i] == 1){
- printf("1");
- }
- else if(data[i] == 0){
- printf("0");
- }
- }
- printf("\n\n");
- }
- int BinToDec(void){
- int i, sum = 0, d = 1;
- char *data;
- printf("Enter number: ");
- scanf("%s", data);
- for(i = strlen(data)-1; i >= 0; i--){
- if(data[i] == '1'){
- sum += d;
- }
- d = d * 2;
- }
- printf("Bin to dec: %d\n\n", sum);
- }
- int main(void)
- {
- int s = 0;
- while(1){
- printf("Enter 1 to convent 10 in 2\nEnter 2 to convent 2 in 10\nEnter 3 to exit\n>");
- scanf("%d", &s);
- printf("\n");
- if(s == 1){
- DecToBin();
- }
- else if(s == 2){
- BinToDec();
- }
- else if(s == 3){
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment