CS Knowledge Opener - Logo
CS Knowledge
Opener
Facebook Youtube Telegram
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
CS Knowledge Opener - Logo
CS Knowledge
Opener
Facebook Youtube Telegram
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
  • Home
  • 12th CS
    • 12th CS English Medium
    • 12th CS Tamil Medium
  • 12th CA
    • 12th CA English Medium
    • 12th CA Tamil Medium
  • 11th CS
    • 11th CS English Medium
    • 11th CS Tamil Medium
  • 11th CA
    • 11th CA English Medium
    • 11th CA Tamil Medium
  • CS / CA Question Bank
  • Other Subjects
  • Student Alerts
  • Govt Exams
11th CS English Medium

Practical Program Exercise 4 Number conversion for 11th Computer Science

Baskaran J
February 22, 2023 3 Mins Read
2.5K Views
0 Comments

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)

Tags:

Practical

Share Article

Follow Me Written By

Baskaran J

Other Articles

Previous

Practical program exercise 10 PHP Converting word to digit for 12th computer applications

Next

Puducherry Police Constable and Driver Grade III PST-PET date announced 2023

Next
March 1, 2023

Puducherry Police Constable and Driver Grade III PST-PET date announced 2023

Previous
February 18, 2023

Practical program exercise 10 PHP Converting word to digit for 12th computer applications

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

5 × 4 =

Related Posts

11th CS Board Exam Answer Key Discussion 2024-2025

Baskaran J
March 14, 2025

11th Computer Science Practice One marks question 2024-2025

Baskaran J
February 3, 2025

11th Computer Science English medium Public Exam Important Important Question 2024-2025

Baskaran J
January 17, 2025

11th computer science English medium new study material 2024-2025

Baskaran J
November 1, 2024

Subscribe to our newsletter and stay updated.

CS Knowledge Opener

CS Knowledge Opener focuses on 11th &12th Computer Science & Computer Applications English Medium & Tamil Medium. Programming Skills and also we are Publishing Study Materials Based on New syllabus introduced by TNSCERT. (TNBOARD).

Quick Links

  • Contact Us
  • About Us
  • Privacy Policy
  • Disclaimer
  • Terms and Conditions
Categories
11th CA English Medium 15
11th CA Tamil Medium 9
11th CS English Medium 21
11th CS Tamil Medium 12
12th CA English Medium 32
12th CA Tamil Medium 8
12th CS English Medium 36
12th CS Tamil Medium 12
Government Exams 36
Notifications 8
Other Subjects 13

Follow Us

Youtube
Facebook
Telegram
CS Knowledge Opener © 2020 - 2025. All Rights Reserved.