-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexo6.c
More file actions
30 lines (29 loc) · 687 Bytes
/
exo6.c
File metadata and controls
30 lines (29 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <math.h>
int main() {
int N, lastDigit, i, p, n, sumOfDigits;
printf("Enter N(Binary): ");
scanf("%d", &N);
i = 0;
while(N > 0){
if(N%10 > 1){
printf("Invalid binary number");
return 0;
}
else{
lastDigit = N%10;
p = pow(2, i);
n = lastDigit * p;
sumOfDigits += n;
N = N/10;
i++;
}
}
printf("N = %d", sumOfDigits);
printf("\n");
printf("\n");
printf("\n");
printf("---------------------------------------------------\n");
printf("Made by: Ahmed Belmehnouf");
return 0;
}