### Question

I really don't have a clue how to carry out this.. could some1 please write the program that I could see to understand what mut be written. thankyou.

question-write a C program to accept a decimal number and to out put the answer into binary. Divide by two and write down the remainder.(will this include the 'For while'?)

question2-write a program that would convert feet to inches, inches back to feet. cm to m and m back to cm.

And could some one explain the 'For while' and 'Do while' to me please. I understand it not.
thankyou.
I wont write the program for you but I can give you some direction....

Question 1. Im not sure I understand it completely. "Divide by two and write down the remainder.(will this include the 'For while'?)

Lets get to what I can tell you about it first.... It wants you to be able to take in a decimal number. So you would have to declare your variable as either a double or float. Then it wants you to output your answer in binary. To achieve that you can use static_cast<int> in your calculation or output. What static_cast<int> does is force a decimal number to display without the decimal. For more information you can look up "Type Casting"

The second part of it, Im not sure I understand. What does he mean write down the remainder? Does he mean output it or just determine what it is and write it down on paper? To get the remainder just use the mod operator in your calculation which is %. Example 7 % 2 would divide 7 by 2 but only output the remainder which is 1.

I also don't understand (will this include the 'For while'?) Is that a question you are asking or is it a question in your assignment? 'For while' is a loop. You don't need to use a loop to make your program but you could if you wanted to.

Question 2. So here you basically need to take two types of input. 1 input is a number. The second input would be to ask the user what type of conversion he wants. You could use if else statements to achieve this. You will need to make math calculations based on the users input. There are 12 inches in a foot. There are 100 centimeters in a meter.

For, while and Do while are all loops. They all do pretty much the same thing. They just have slightly different syntax. Loops check a Boolean expression for a true condition. If the condition is true it will execute the code in the loop. It will continue to execute that code until the condition is false. Once false it breaks out of the statement and continues on with the rest of your program.

Syntax of For is : for(control variable; boolean expression; counter)
{ Code to excute goes here }

Example: for(i = 1; i <= 10; i++)

cout << i << " ";

Syntax of while: while(Boolean expression)

{ code to execute goes here}

Example: while( i <=10)

{ cout << i << " ";
i++;}

Syntax of do while: do

{body of code goes here
}while(Boolean expression);

Example: do
{ cout << i << " ";
i++:
}while( i <= 10);

The i++ is your counter. It increments i by 1 everytime it passes through the loop. That way i will eventually be greater then 10 and stop the loop. What all of the about code does (its all the same just written different) is set the i variable to 1. Then it checks the Boolean statement to see if i is less then or equal to 10. 1 is less then or equal to 10 so the condition is true. Because the condition is true it executes the code in the loop which is to output i. Then the counter increments i by one. i is now 2. Check the Boolean again and the condition is still true. It will keep doing this until i becomes 11. 11 turns the condition false and stops the loop. You can make any variable, Boolean expression, and counter you want to make your program do what you want it to do.

If you follow the code it would output: 12345678910

EDIT: I forgot to mention that the for loop sets i=1. In the while and do while loops you would have to set that variable above the loop.

Take an important note on the semi colon. You do not use them after the for or while statements. Doing so would execute the semi colon instead of your code. The exception is in the do while. You would use the semi colon after the while statement in that case.

Hope that helps.

Last edited on
@abriella1

Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that a = bq + r and 0 ≤ r < |b|, where |b| denotes the absolute value of b.

Example: if a = 7 and b = 2, then q = 3 and r = 1, since 7 = 2 × 3 + 1.

The four integers that appear in this theorem have been given a name: a is called the dividend, b is called the divisor, q is called the quotient and r is called the remainder.

And now try to implement this possible way to convert an ordinary number to its binary form:

 ``1234567`` `````` int i = 1; while(number) // number is the dividend. { // now bin[] will bin[i++] = number % 2; // store the remainder. number /= 2; // this is the quotient } // which becomes dividend // and resumes the loop ``````

where bin[] has been declared as an array of integers.
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include int main() { long long int number;// type for huge integers int bin[50] = {0}; // stores up to 50 bits(0/1). // bin[50] can store the binary form of a decimal integer // up to the huge number 2^50 - 1 = 1125899906842623. // Of course, 50 is a value which can be modified. printf("Enter a positive integer: "); scanf("%lli", &number);// format for long long integer if(number < 0 || number > 1125899906842623)return 1; printf("\nThe binary form of %lli is:\n\n", number); int i = 0; while(number) // number is the dividend. { // now bin[] will bin[i++] = number % 2; // store the remainder. number /= 2; // this is the quotient } // which becomes dividend // and resumes the loop int j; for(j = i - 1 ;j >= 0; j--)// prints the binary printf("%i", bin[j]); // positions printf("\n\nhaving %i bits.", i); return 0; }`````` ```Example 1: ---------- Enter a positive integer: 10 The binary form of 10 is: 1010 having 4 bits. Example 2: ---------- Enter a positive integer: 123456 The binary form of 123456 is: 11110001001000000 having 17 bits. Example 3: ---------- Enter a positive integer: 1125899906842623 The binary form of 1125899906842623 is: 11111111111111111111111111111111111111111111111111 having 50 bits.```
Last edited on
thank you all.. what you guys said was extremely helpful!*
Topic archived. No new replies allowed.