SlideShare a Scribd company logo
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

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 

Recently uploaded (20)

2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 

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"); } }