Commit 6830b2fa by karlki

praktikum 6

parent d71b1845
#include <stdio.h>
#include <math.h>
int main(void) {
int arv;
int n=1;
int max2 = 1;
int i;
printf("sisesta arv:");
scanf("%d", &arv);
do {
//printf("%d \n",n);
n++;
max2 = max2 * 2;
} while ( arv > max2);
int B[n];
i = 0;
do {
if ( arv >= max2)
{
//printf("k");
B[i] = 1;
arv = arv - max2;
} else {
//printf("s");
B[i] = 0;
}
max2 = max2 / 2;
//printf("%d \n", B[i]);
i++;
} while (i < n);
for (i = 0; i < n; i++) {
printf("%d ", B[i]);
}
printf("\n");
return 0 ;
}
#include <stdio.h>
#include <math.h>
int main(void) {
int arv;
int tul;
printf("arv: ");
scanf("%d", &arv);
if (arv == 0) {
printf("0 \n");
return 0;
}
int length = log10(arv) + 1;
int A[length];
int i = 0;
while (arv > 0) {
int digit = arv % 10;
A[i] = digit;
arv /= 10;
i++;
}
/*for (i = 0; i < length; i++) {
printf("%d ", A[i]);
}*/
i = 0;
int j = length;
do {
tul += pow(2, (j-1))*A[j-1];
i++;
j--;
} while (i < length);
printf("%d \n", tul);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment