Question

In: Computer Science

Write C program : Enter N and N pairs of positive integerpairs, convert each pair...




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.

Solutions

Expert Solution

#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;

}


Related Solutions

Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
ASAP (Convert decimals to fractions) Write a program that prompts the user to enter a decimal...
ASAP (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in Listing 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19Test.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is 13/4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT