Practical Program Exercise 4 Number conversion for 11th Computer Science
Program Exercise 4 Number conversion
do-while loop
- The do-while loop is an exit-controlled loop.
- In do-while loop, the condition is evaluated at the bottom of the loop after executing the body of the loop.
- This means that the body of the loop is executed at least once, even when the condition evaluates false during the first iteration.
The do-while loop syntax is:
do
{
Body of the loop;
} while(condition);
Switch Statement
- The Switch Statement is a multi-way branching statement.
- It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
- The switch statement replaces multiple if-else sequence.
The syntax of the switch statement is
switch(expression)
{
case constant 1:
statement(s);
break;
case constant 2:
statement(s);
break;
.
.
.
.
default:
statement(s);
}
- In the above syntax, the expression is evaluated and if its value matches against the constant value specified in one of the case statements, that respective set of statements are executed.
- Otherwise, the statements under the default option are executed.
Rules:
1. The expression provided in the switch should result in a constant value otherwise it would not be valid.
2. Duplicate case values are not allowed.
3. The default statement is optional.
4. The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
5. The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
6. Nesting of switch statements is also allowed.
Decimal Number System
- It consists of 0,1,2,3,4,5,6,7,8,9(10 digits).
- It is the oldest and most popular number system used in our day to day life.
- In the positional number system, each decimal digit is weighed relative to its position in the number.
Binary Number System
- There are only two digits in the Binary system, namely, 0 and 1.
- The numbers in the binary system are represented to the base 2 and the positional multipliers are the powers of 2.
Decimal to Binary Conversion
- To convert Decimal to Binary “Repeated Division by 2” method can be used.
- Any Decimal number divided by 2 will leave a remainder of 0 or 1.
- Repeated division by 2 will leave a sequence of 0s and 1s that become the binary equivalent of the decimal number.
Binary to Decimal Conversion
Step 1: Write down the Binary digits and list the powers of 2 from right to left(Positional Notation)
Step 2: For each positional notation written for the digit, now write the equivalent weight.
Step 3: Multiply each digit with its corresponding weight
Step 4: Add all the values.
CS – 4 NUMBER CONVERSION
QUESTION:
Using do while loop create the following menu based C++ program
1.Convert a Decimal to binary number
2.Convert a binary number to Decimal
3. Exit
Depending on the choice accept the value and display the result. The program should continue till the user select the third option
AIM:
- To write a C++ program by Using do while loop create the menu based program for number conversion.
CODING:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int dec, d, i, temp, ch;
long int bin;
do
{
dec=bin=d=i=0;
cout<<“\n\n\t\tMENU\n1. Decimal to Binary number\n2.Binary to Decimal number\n3.Exit\n”;
cout <<“Enter your choice(1/2/3)”;
cin>>ch;
switch (ch)
{
case 1: cout << “Enter a decimal number: “;
cin >> dec;
temp=dec;
while (dec!=0)
{
d = dec%2;
bin += d * pow(10,i);
dec /= 2;
i++;
}
cout << temp << ” in decimal = ” << bin << ” in binary” << endl ;
break;
case 2: cout << “Enter a binary number: “; cin >> bin;
temp=bin;
while (bin!=0)
{
d = bin%10;
dec += d*pow(2,i);
bin /= 10;
i++;
}
cout << temp << ” in binary = ” <<dec << ” in decimal”;
break;
case 3: break;
default : cout<<“Invalid choice”;
}
} while (ch!=3);
return 0;
}
OUTPUT 1:
MENU
1. Decimal to Binary number
2.Binary to Decimal numbern3.Exit
Enter your choice(1/2/3)1
Enter a decimal number: 23
23 in decimal = 10111 in binary
MENU
1. Decimal to Binary number
2.Binary to Decimal numbern3.Exit
Enter your choice(1/2/3)2
Enter a binary number: 11001
11001 in binary = 25 in decimal
MENU
1. Decimal to Binary number
2.Binary to Decimal numbern3.Exit
Enter your choice(1/2/3)3
OUTPUT 2:
MENU
1. Decimal to Binary number
2.Binary to Decimal number
3.Exit
Enter your choice(1/2/3)4
Invalid choice
MENU
1. Decimal to Binary number
2.Binary to Decimal number
3.Exit
Enter your choice(1/2/3)