Tuesday, January 20, 2015

Java Programming: Program that adds two (2) floats

No comments :
Here is a program that adds two (2) floats. The program only accepts numbers. It keeps asking for input until a number is entered. The function add(float addend1, float addend2) returns the result in float.

Program output


code:
 import java.util.Scanner;  
   
 /**  
  *  
  * @author Engineering Problems and Answers (http://engineeringproblemsandanswers.blogspot.com)  
  */  
 public class main {  
        
      public static void main(String[] args) {  
             
           float addend1, addend2, result; //declare addends and result variables  
           Scanner sc = new Scanner(System.in); //initialize scanner  
             
           System.out.print("Enter 1st addend: "); //ask for the 1st number  
             
           //condition keep asking until a number is entered  
           while (!sc.hasNextFloat()) {  
                System.out.print("Enter 1st addend: ");  
                sc.next();  
           }  
           addend1 = sc.nextFloat(); //store the number here  
             
           System.out.print("Enter 2nd addend: "); //ask for the 2nd number  
             
           //condition to keep asking until a number is entered  
           while (!sc.hasNextFloat()) {  
                System.out.print("Enter 2nd addend: ");  
                sc.next();  
           }  
           addend2 = sc.nextFloat(); //store the number here  
             
           result = add(addend1, addend2); /*call add method and store result to variable  
                                           result(float)*/  
             
           System.out.println("\nNon-formatted print:");  
           System.out.println(addend1+" + "+addend2+" = "+result); /*here, result value is not  
                                                                   accurate*/  
           System.out.println("\nFormatted print:");  
           System.out.printf("%.2f + %.2f = %.2f", addend1, addend2, result); /*format to decimal  
                                                                              places (result is  
                                                                              rounded off) in order  
                                                                              to get a more accurate  
                                                                              result value*/  
      }  
        
      private static float add(float addend1, float addend2) {  
           return addend1 + addend2; //return the sum of the two numbers  
      }  
 }  

When operating on floats, the computer, sometimes, doesn't give the right answer. The computer isn't actually wrong. Read the explanation HERE. We can format the answer we print as a workaround for this (see code lines 38 to 43).

You may use this or any part of this program for noncommercial use, i.e. your assignments, projects, teaching programs.

If you have questions/suggestions, quickly place a comment below.

No comments :

Post a Comment