Computer

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

COMPUTER ASSIGNMENT

Name - Soumyadeep Das


Class - XI Sec - A
Roll - 13
Question :-
Write a menu driven program to perform conversion
between different number system using user’s choice:
Choice 1: Decimal to Binary
Choice 2: Decimal to Octal
Choice 3: Decimal to Hexadecimal
Choice 4: Binary to Decimal
Choice 5: Octal to Decimal
Choice 6: Hexadecimal to Decimal

Program :-

import java.util.*;
class Assignment_Soumadeep
{
void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Decicaml to Binary
Conversion");
System.out.println("Enter 2 for Decicaml to Octal
Conversion");
System.out.println("Enter 3 for Decicaml to
Hexadecimal Conversion");
System.out.println("Enter 4 for Binary to Decicaml
Conversion");
System.out.println("Enter 5 for Octal to Decicaml
Conversion");
System.out.println("Enter 6 for Hexadecicaml to
Decicaml Conversion");
System.out.println("Enter your choice");
int ch = sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter Decicaml value");
int dec = sc.nextInt();
int bin = 0;
int p = 0;
while(dec>0)
{
int d = dec%2;
bin = bin + d*(int)Math.pow(10,p);
dec = dec / 2;
p++;
}
System.out.println("Binary = "+bin);
break;
case 2:
System.out.println("Enter Decimal Value");
int dec1 = sc.nextInt();
int oct = 0;
int q = 0;
while(dec1>0)
{
int d = dec1%8;
oct = oct + d*(int)Math.pow(10,q);
dec1 = dec1 / 8;
q++;
}
System.out.println("Octal = "+oct);
break;
case 3:
System.out.println("Enter Decicaml value");
int dec2 = sc.nextInt();
String hex = "";
char
hexchars[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E',
'F'};
while(dec2>0)
{
int d = dec2%16;
hex = hexchars[d]+hex;
dec2 = dec2 / 16;
}
System.out.println("Hexadecimal = "+hex);
break;
case 4:
System.out.println("Enter Binary value");
int bin1 = sc.nextInt();
int r = 0;
int di = 0;
while (bin1>0)
{
int d = (int)bin1%10;
di = di+d*(int)Math.pow(2,r);
bin1/=10;
r++;
}
System.out.println("Decimal = "+di);
break;
case 5:
System.out.println("Enter Octal value");
int oct1 = sc.nextInt();
int s = 0;
int z = 0;
while (oct1>0)
{
int d = (int)oct1%10;
s = s+d*(int)Math.pow(8,z);
oct1/=10;
z++;
}
System.out.println("Decimal = "+s);
break;
case 6:
String hexa = sc.next();
String digits = "0123456789ABCDEF";
hexa = hexa.toUpperCase();
int val = 0;
for (int i = 0; i < hexa.length(); i++)
{
char c = hexa.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
System.out.println(""+val);
break;
default:
System.out.println("Wrong Input");
}
}
}
Output :-
Choice 1:

Choice 2:

Choice 3:

Choice 4:

Choice 5:

Choice 6:

*******

You might also like