this problem needs to store input to some point and display it so is it possible without using arrays?
Without array is it possible
Hi @radhagovinda008,
see you can do while(1) and take the input inside,calculate sum and check that whether sum<0 then break else print n.
Scanner sc=new Scanner(System.in);
boolean input=true;
int num;
int sum=0;
while(input){
num=sc.nextInt();
if(num>0){
sum+=num;
System.out.println(num);
}
else if(Math.abs(num)>sum){
input=false;
}
else{
sum=sum+num;
System.out.println(num);
}
}
Hi ,see you dont have to check whether the number is negative or positive,you only have to check for sum that if it is positive then print the number you have given as input else come out of the loop.
may this will help you for the start:
import java.util.Scanner;
public class SimpleInput {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
boolean input=true;
int num;
int sum=0;
while(input){
num=sc.nextInt();
if(num>0){
sum+=num;
System.out.println(num);
}
else if(Math.abs(num)>sum){
input=false;
}
else{
sum=sum+num;
System.out.println(num);
}
}
// do while(1) and take the input inside calculate sum and check that whatever sum<0 then break else print n
}
}