Tuesday, July 12, 2011

1 part sum of three number divide by fourth number

This java program is not able to calculate result in decimal value, and if you provide that type of input it give wrong answer. 
For Ex- n1=3, n2=2, n3=3 and n4=3 then answer (Total=2) which is wrong right answer is (2.6666).
In 2nd part program we provide how to get rid from this error.


source code
  1. import java.io.*;
  2. class sum_of_three_no_divide_by_fourth_no
  3. {
  4.     public static void main(String[] args)throws IOException
  5.     {
  6.         InputStreamReader ir=new InputStreamReader(System.in);
  7.         BufferedReader br=new BufferedReader(ir);
  8.         int A,B,C,D,total;
  9.         System.out.println("Eter no1");
  10.         A=Integer.parseInt(br.readLine());
  11.         System.out.println("Eter no2");
  12.         B=Integer.parseInt(br.readLine());
  13.         System.out.println("Eter no3");
  14.         C=Integer.parseInt(br.readLine());
  15.         System.out.println("Eter no4");
  16.         D=Integer.parseInt(br.readLine());
  17.         total=(A+B+C)/D;
  18.         System.out.println("Total="+total);
  19.        
  20.                 }
  21. }

No comments:

Post a Comment