0% found this document useful (0 votes)
17 views2 pages

Package Import Public Class Public Static Void: "Introduceti Un Numar Pozitiv: "

info chestii inutil bhdgf chdsgfyfgyvc ghbv hggechgcx bnddhbvb cv dfbhj hgj ghgfvt cgh fjhgd fk hjghg hjhj hgg j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Package Import Public Class Public Static Void: "Introduceti Un Numar Pozitiv: "

info chestii inutil bhdgf chdsgfyfgyvc ghbv hggechgcx bnddhbvb cv dfbhj hgj ghgfvt cgh fjhgd fk hjghg hjhj hgg j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Student : Toader (Stanciulescu ) Nicoleta Carmen

Anul III Informatica ID


package baza ;
import java.util.Scanner;
public class baza {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int decimalNumber;
String binaryNumber;
System.out.print("Introduceti un numar pozitiv: ");
decimalNumber = in.nextInt();
if (decimalNumber <= 0)
System.out.println("ERROR: numarul nu este pozitiv.");
else {
binaryNumber = "";
// algorithm step by step
// initial: binaryNumber = "", decimalNumber = 123
// step 1 : binaryNumber = "1 ", decimalNumber = 61
// step 2 : binaryNumber = "11 ", decimalNumber = 30
// step 3 : binaryNumber = "011 ", decimalNumber = 15
// step 4 : binaryNumber = "1011 ", decimalNumber = 7
// step 5 : binaryNumber = "1 1011 ", decimalNumber = 3
// step 6 : binaryNumber = "11 1011 ", decimalNumber = 1
// step 6 : binaryNumber = "111 1011 ", decimalNumber = 0
// stop : (decimalNumber != 0)
while (decimalNumber != 0) {
// add spaces to separate 4-digit groups
if (binaryNumber.length() % 5 == 0)
binaryNumber = " " + binaryNumber;
// extract last digit in binary representation
// and add it to binaryNumber
binaryNumber = (decimalNumber % 2) + binaryNumber;
// cut last digit in binary representation
decimalNumber /= 2;
}
System.out.println("Binar: " + binaryNumber);
}
}
}

------------------------------------------Exemplu:
Enter a positive integer: 123
Binary: 111 1011
Decimal in binar.

You might also like