Welcome to Dream.In.Code
Become a Java Expert!

Join 150,071 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,808 people online right now. Registration is fast and FREE... Join Now!




Arithmetic operations in java

 
Reply to this topicStart new topic

Arithmetic operations in java, Consider a two digit integer. Extract the digits. Write a java program

gitartha
14 Jun, 2008 - 08:11 PM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2008
Posts: 1

1. Consider a two-digit integer. Extract the digits. Write a Java program to form the reverse of the number and print it.
2. Consider a two-digit integer number. Find the difference and product of the digits and print them.
3. Consider two two-digit integers. Find the sum of their squares.
4. Consider the length and breadth of a rectangle. Find the area and the perimeter of the rectangle.
5. Consider the side of a square. Find the area and the perimeter of the square.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Arithmetic Operations In Java
14 Jun, 2008 - 08:14 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(gitartha @ 14 Jun, 2008 - 09:11 PM) *

1. Consider a two-digit integer. Extract the digits. Write a Java program to form the reverse of the number and print it.
2. Consider a two-digit integer number. Find the difference and product of the digits and print them.
3. Consider two two-digit integers. Find the sum of their squares.
4. Consider the length and breadth of a rectangle. Find the area and the perimeter of the rectangle.
5. Consider the side of a square. Find the area and the perimeter of the square.

And your question(s) is/are ?
User is offlineProfile CardPM
+Quote Post

cutegrrl
RE: Arithmetic Operations In Java
14 Jun, 2008 - 09:13 PM
Post #3

D.I.C Head
**

Joined: 12 May, 2008
Posts: 72



Thanked: 7 times
My Contributions
#1, #2, and #3
java
import java.io.*;

public class Foo {

//Create a single shared BufferedReader for keyboard input
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

// Program exectuon starts here
public static void main(String args[]){

// Get a two-digit number from the user
int number = getTwoDigitNumber();

// Extract digits
int firstDigit = Math.abs(number / 10);
int secondDigit = Math.abs(number % 10);

// Print reverse
System.out.println("Reverse: " + (new StringBuffer(Math.abs(number)+"").reverse()));

// Print difference of digits
System.out.println("Difference: " + firstDigit + " - " + secondDigit + " = " + (firstDigit - secondDigit));

// Print product of digits
System.out.println("Product: " + firstDigit + " * " + secondDigit + " = " + (firstDigit * secondDigit));

// Get two two-digit number from the user
number = getTwoDigitNumber();
int number2 = getTwoDigitNumber();

// Print sum of squares
System.out.println(number + "^2 + " + number2 + "^2 = " + (number*number + number2*number2));

}

// Returns a two-digit number from the user
public static int getTwoDigitNumber(){
// Loop until valid input is entered
while(true){
try{
// Prompt user for input
System.out.print("Enter a two digit number: ");
String input = in.readLine();
int number = Integer.parseInt(input);
if(number > -100 && number < -9 || number < 100 && number > 9){
return number;
}else{
System.out.println("Invalid input. Please enter a valid two digit integer.");
}
}catch(Exception e){
System.out.println("Invalid input. Please enter a valid two digit integer.");
}
}
}
}


#4 and #5
java

import java.io.*;

public class Foo2 {

// Create a single shared BufferedReader for keyboard input
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

// Program exectuon starts here
public static void main(String args[]){

// Prompt user for dimensions of the rectangle
double length = getValue("length", "rectangle");
double breadth = getValue("breadth", "rectangle");

// Truncate values to two decimal places
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");

// Print area and perimeter of the rectangle
System.out.println("Area: " + df.format(length*breadth));
System.out.println("Perimeter: " + df.format(length*2 + breadth*2));

// Prompt user for dimensions of the square
double sideLength = getValue("side length", "square");

// Print area and perimeter of the square
System.out.println("Area: " + df.format(sideLength*sideLength));
System.out.println("Perimeter: " + df.format(sideLength*4));

}

// Returns a number from the user
public static double getValue(String measurement, String shape){
// Loop until valid input is entered
while(true){
try{
// Prompt user for input
System.out.print("Enter the " + measurement + " of the " + shape + ": ");
double number = Double.parseDouble(in.readLine());
if(number > 0){
return number;
}else{
System.out.println("Invalid input. Please enter a positive value.");
}
}catch(Exception e){
System.out.println("Invalid input. Please enter a valid number.");
}
}
}
}


This post has been edited by cutegrrl: 14 Jun, 2008 - 09:35 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Arithmetic Operations In Java
14 Jun, 2008 - 09:24 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Cutegrrl you are spoiling the newbie again....
don't forget the rules

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month