SlideShare a Scribd company logo
1 of 2
Download to read offline
I need the code for a Java programming project. Allow the user to type in a integer or floating
point decimal number. calculate and display the binary equivalent. Use 2’s complement for
negative numbers. For floating point numbers use simplified format
Solution
Answer:
import java.io.*;
class ComplementProgram
{
public static void main(String args[]) throws IOException
{
int read[]=new int[32];
int input1,input2,i=0,carryflag=0,j=0,k=0,m,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
input1=Integer.parseInt(br.readLine());
input2=input1;
i=31;
while(input1!=0)
{
read[i]=Math.abs(input1%2);
input1=input1/2;
i=i-1;
}
while(i>=0)
{
read[i]=0;
i=i-1;
}
System.out.println("");
for(k=0;k<32;k++)
System.out.print(read[k]);
if(input2<0)
{
for(i=0;i<32;i++)
{
if(read[i]==1)
read[i]=0;
else
read[i]=1;
}
carryflag=1;
for(i=31;i>=0;i--)
{
if(read[i]==1 && carryflag==1)
{
carryflag=1;
read[i]=0;
}
else if(carryflag==1)
{
read[i]=1;
carryflag=0;
break;
}
}
}
System.out.println("");
if(input2<0)
{
System.out.println("The 2's Complement of a positive number is:");
for(k=0;k<32;k++)
System.out.print(read[k]);
}
else
System.out.println("For this number there is no 2's complement");
}
}

More Related Content

Similar to I need the code for a Java programming project. Allow the user to ty.pdf

Create a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfCreate a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdf
devangmittal4
 

Similar to I need the code for a Java programming project. Allow the user to ty.pdf (20)

python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Presentation1 computer shaan
Presentation1 computer shaanPresentation1 computer shaan
Presentation1 computer shaan
 
Programs of java
Programs of javaPrograms of java
Programs of java
 
Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1Chapter 1 Class 12 Computer Science Unit 1
Chapter 1 Class 12 Computer Science Unit 1
 
Anjalisoorej imca133 assignment
Anjalisoorej imca133 assignmentAnjalisoorej imca133 assignment
Anjalisoorej imca133 assignment
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2
 
OXUS20 JAVA Programming Questions and Answers PART I
OXUS20 JAVA Programming Questions and Answers PART IOXUS20 JAVA Programming Questions and Answers PART I
OXUS20 JAVA Programming Questions and Answers PART I
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdf
 
ASP.NET
ASP.NETASP.NET
ASP.NET
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 
Iron python
Iron pythonIron python
Iron python
 
Hello. This program has to be done in Eclipse(Program used to write .pdf
Hello. This program has to be done in Eclipse(Program used to write .pdfHello. This program has to be done in Eclipse(Program used to write .pdf
Hello. This program has to be done in Eclipse(Program used to write .pdf
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Create a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdfCreate a C program which performs the addition of two comple.pdf
Create a C program which performs the addition of two comple.pdf
 

More from venkatesh24685

java programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdfjava programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdf
venkatesh24685
 
Hi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdfHi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdf
venkatesh24685
 
Explain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdfExplain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdf
venkatesh24685
 
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdfC# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
venkatesh24685
 
“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf
venkatesh24685
 
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdfYou have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
venkatesh24685
 
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdfWhy are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
venkatesh24685
 
When we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdfWhen we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdf
venkatesh24685
 

More from venkatesh24685 (20)

java programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdfjava programming 5th edition chapter 8 user defined classes and ADTs.pdf
java programming 5th edition chapter 8 user defined classes and ADTs.pdf
 
How each of the main themes of The Fifth Discipline are related to e.pdf
How each of the main themes of The Fifth Discipline are related to e.pdfHow each of the main themes of The Fifth Discipline are related to e.pdf
How each of the main themes of The Fifth Discipline are related to e.pdf
 
Hi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdfHi,BST question I was wondering why my insert function works p.pdf
Hi,BST question I was wondering why my insert function works p.pdf
 
Favor Composition does what and howSolutionWe know what is in.pdf
Favor Composition does what and howSolutionWe know what is in.pdfFavor Composition does what and howSolutionWe know what is in.pdf
Favor Composition does what and howSolutionWe know what is in.pdf
 
Explain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdfExplain how character displacement can influence the speciation p.pdf
Explain how character displacement can influence the speciation p.pdf
 
astronomy One Nineteenth Century observation which convinced many.pdf
astronomy One Nineteenth Century observation which convinced many.pdfastronomy One Nineteenth Century observation which convinced many.pdf
astronomy One Nineteenth Century observation which convinced many.pdf
 
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdfA) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
A) For bipyridine (bipy) determine the maximum number of coordinatio.pdf
 
Describe two (2) attributes of a globally aware EngineerEngineering.pdf
Describe two (2) attributes of a globally aware EngineerEngineering.pdfDescribe two (2) attributes of a globally aware EngineerEngineering.pdf
Describe two (2) attributes of a globally aware EngineerEngineering.pdf
 
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdfC# This program lets you pick from a choice of 8 items Circle, Squ.pdf
C# This program lets you pick from a choice of 8 items Circle, Squ.pdf
 
“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf“Our communication capabilities have raced ahead of our communicatio.pdf
“Our communication capabilities have raced ahead of our communicatio.pdf
 
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
18. The density of a noble gas at STP is 1.784 gL. Identify the nobl.pdf
 
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdfYou have identified a mutation in E. coli K-12 that causes it to bec.pdf
You have identified a mutation in E. coli K-12 that causes it to bec.pdf
 
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdfWhy are the EROI values for U.S. ethanol fuel from corn different in.pdf
Why are the EROI values for U.S. ethanol fuel from corn different in.pdf
 
Which of the following are parts of a bond Choose one or more A. t.pdf
Which of the following are parts of a bond Choose one or more A. t.pdfWhich of the following are parts of a bond Choose one or more A. t.pdf
Which of the following are parts of a bond Choose one or more A. t.pdf
 
When we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdfWhen we decide to make something compensable we assert its value.pdf
When we decide to make something compensable we assert its value.pdf
 
What is the role of ports for global commerce and why is that roe im.pdf
What is the role of ports for global commerce and why is that roe im.pdfWhat is the role of ports for global commerce and why is that roe im.pdf
What is the role of ports for global commerce and why is that roe im.pdf
 
What are the stages of team development Why is it important for a s.pdf
What are the stages of team development Why is it important for a s.pdfWhat are the stages of team development Why is it important for a s.pdf
What are the stages of team development Why is it important for a s.pdf
 
Two numbers together add to 300. One number is twice the size of the.pdf
Two numbers together add to 300. One number is twice the size of the.pdfTwo numbers together add to 300. One number is twice the size of the.pdf
Two numbers together add to 300. One number is twice the size of the.pdf
 
This Question 2 Pts The primary goal of financial accounting is to p.pdf
This Question 2 Pts The primary goal of financial accounting is to p.pdfThis Question 2 Pts The primary goal of financial accounting is to p.pdf
This Question 2 Pts The primary goal of financial accounting is to p.pdf
 
The irregularity of innovations and the variability of business prof.pdf
The irregularity of innovations and the variability of business prof.pdfThe irregularity of innovations and the variability of business prof.pdf
The irregularity of innovations and the variability of business prof.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptxMichaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

I need the code for a Java programming project. Allow the user to ty.pdf

  • 1. I need the code for a Java programming project. Allow the user to type in a integer or floating point decimal number. calculate and display the binary equivalent. Use 2’s complement for negative numbers. For floating point numbers use simplified format Solution Answer: import java.io.*; class ComplementProgram { public static void main(String args[]) throws IOException { int read[]=new int[32]; int input1,input2,i=0,carryflag=0,j=0,k=0,m,n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); input1=Integer.parseInt(br.readLine()); input2=input1; i=31; while(input1!=0) { read[i]=Math.abs(input1%2); input1=input1/2; i=i-1; } while(i>=0) { read[i]=0; i=i-1; } System.out.println(""); for(k=0;k<32;k++) System.out.print(read[k]); if(input2<0) { for(i=0;i<32;i++)
  • 2. { if(read[i]==1) read[i]=0; else read[i]=1; } carryflag=1; for(i=31;i>=0;i--) { if(read[i]==1 && carryflag==1) { carryflag=1; read[i]=0; } else if(carryflag==1) { read[i]=1; carryflag=0; break; } } } System.out.println(""); if(input2<0) { System.out.println("The 2's Complement of a positive number is:"); for(k=0;k<32;k++) System.out.print(read[k]); } else System.out.println("For this number there is no 2's complement"); } }