In: Computer Science
Write C program : Enter N and N pairs of positive integer pairs, convert each pair of positive integers into binary and add, and output the formula. Decimal to binary function prototype: int DecToBin(int integer);
The range of input N is 1 <= N <= 100, and the range of positive integers input is 0 < n <= 255. The output format is %08d. If the entered N or positive integer is out of range, "Invalid input" is output. If a positive integer pair is entered more than N pairs, the multi-input portion is not processed.
#include
void decToBinary(int n)
{
// array to store binary number
int binaryNum[32];
// counter for binary array
int i = 0;
while (n > 0) {
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%d",binaryNum[j]);
}
int main(){
int n1,n2;
scanf("%d",&n1);
scanf("%d",&n2);
if( n1<1 || n1 >100 || n2<1 || n2 > 100){
printf("Invalid Input");
}
else {
printf("The Binary equivalent is : ");
decToBinary(n1);
printf("The Binary equivalent is : ");
decToBinary(n2);
printf(" Sum is : %d",n1+n2);
}
return 0;
}