Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- char*change_binary(int num)
- {
- int i, k=0,temp = num, cnt = 0, *arr = NULL; char *str = NULL;
- while (num != 0)
- {
- temp = num;
- temp %= 2;
- str = (char *)realloc(str, sizeof(char)*(k + 1));
- *(str + k) = temp + '0';
- k++;
- num /= 2;
- }
- *(str + k) = NULL;
- puts(str);
- return str;
- }
- void main()
- {
- FILE *f1, *f2; int i, j = 0, k = 0, *arr = NULL,*intarr=NULL, temp = -1; char *newarr = NULL,tempr[5];
- fopen_s(&f1, "Text.txt", "rt");
- fopen_s(&f2, "res.txt", "w");
- if (f1 == NULL)
- exit(1);
- for (i = 0; temp!=0; i++)
- {
- arr = (int *)realloc(arr,sizeof(int)*(i+1));
- if (arr == NULL)
- exit(1);
- fscanf_s(f1, "%d", &temp);
- *(arr + i) = temp;
- }
- if (f1 == NULL)
- exit(1);
- for (i = 0; i <16; i++)
- {
- if (arr[i] % 2 == 0)
- fprintf_s(f2, "%-3d", arr[i]);
- else
- {
- int num2, num = arr[i];
- num2 = arr[i];
- k = 0;
- while (num != 0)
- {
- temp = num;
- temp %= 2;
- newarr = (char *)realloc(newarr, sizeof(char)*(k + 1));
- intarr = (int *)realloc(intarr, sizeof(int)*(k + 1));
- *(newarr + k) = temp + '0';
- tempr[k] = *(newarr + k);
- k++;
- num /= 2;
- }
- *(newarr + k) = NULL;
- printf("The number in dec is : %d \n and in binary is: \n", num2);
- printf("%b", num2);
- puts(newarr);
- }
- }
- for (i = 0; i < k; i++)
- {
- printf("%-3d", *(newarr + i));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement