Lab1 - Part Two

Wednesday, June 18, 2008

I am giving this code to help you all understand not to copy you can post any comment and queries I will return help as soon as possible, Copying of this code for any kind of assignment is strictly prohibited.

--------------------------
import java.util.Scanner;
public class lab1part2
{
public static void main (String args[])
{
int integer=0;
int a=0, b=0,c=0,d=0,e=0,k=0;
int rem;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Integer:");
integer = scan.nextInt();

while(k<5)
{
rem = integer%2;
integer = integer/2;
if(k==0)
{
e = rem;
}
if(k==1)
{
d = rem;
}
if(k==2)
{
c = rem;
}
if(k==3)
{
b = rem;
}
if(k==4)
{
a = rem;
}
k++;
}
System.out.println(a+""+b+""+c+""+d+""+e+"");
}

}

-------------------------------------------------------------------------------------------------

other way

-------------------------------------------------------------------------------------------------

import java.util.Scanner;

public class Lab1b
{
 public static void main(String[] args)
 {
  Scanner scan = new Scanner(System.in);
  System.out.print("Please enter a decimal number between 0 and 31: ");
  int num = scan.nextInt();

  System.out.println("Your number in decimal form: " + num);
  System.out.print("Your number in binary form: ");
  int result = num/16;
  System.out.print(result);
  num %= 16;
  result = num/8;
  System.out.print(result);
  num %= 8;
  result = num/4;
  System.out.print(result);
  num %= 4;
  result = num/2;
  System.out.print(result);
  num %= 2;
  System.out.print(num);
 }
}



0 comments: