0

how to get the correct difference and product of dis?!

import java.io.*;

public class exer05{
	public static void main(String args[])throws Exception{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int num = 0;
		int number = 0;
		int diff = 0,pro = 0,sum=0;
		
	
	
		System.out.print("Enter number of times:");
		number = Integer.parseInt(br.readLine());
		
			
		for(int i=1;i<=number;i++){
		
		 System.out.print("Number ("+ number +")");
		 num = Integer.parseInt(br.readLine());
		 
		 diff = diff - num;
		 pro =  pro * num;}
		 {
		 
		 System.out.println("\nDiff: "+diff);
		 System.out.println("Pro: "+pro);}
		 
		 
		
			
			
		
	}
}
4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by mvmalderen
0

fixed couple of things:
cycle indention was off, diff and pro was printed on only last number because of the way you organized the brackets. heres the reworked version

import java.io.*;

public class exer05{
	public static void main(String args[])throws Exception{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int num = 0;
		int number = 0;
		int diff = 0,pro = 1,sum=0; 

		System.out.print("Enter number of times:");
		number = Integer.parseInt(br.readLine());

		for(int i=0;i<number;i++){ //has to be total -1		
			 System.out.println("Number ("+ (i+1) +")"); //current index
			 num = Integer.parseInt(br.readLine());
			 
			 diff = diff - num;
			 pro =  pro * num;
	
			 System.out.println("Diff: "+diff);
			 System.out.println("Pro: "+pro);
		}
	}
}
0

I am not very much clear what you're trying to ask. Whose difference and product you want to find out. It would be good if you explain what your requirement is in more detail.

0

To the OP:
It would be better that you chose a better thread title in future, a title like "need help.." is useless, because pretty much everyone which opens a thread here needs help.
Try to be more descriptive the next time.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.