Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
3 integer variables from the user ( a, b, c ) and find how many of them are negative numbers.

After finding out the total count of negative numbers, return the count.

for no negative numbers, count will be 0.


This is time and spaces complexity.

Constraints
-10^6<=a<=10^6
-10^6<=b<=10^6
-10^6<=c<=10^6


What I have tried:

public class Main {
    static int printCount(int a, int b, int c){

		int count =0;
		for(int i=0; i<a || i<b || i<c; i++){
			if(a<0 || b<0 || c<0){
				count++;
			}
		
		}
		return count;
		}
    }

	public static void main(String[] args) throws Throwable {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        System.out.println(printCount(a, b, c));
    }
}
Posted
Updated 26-Dec-22 23:38pm

(As Griff suggested...) your code is completely wrong.
You are required to check the entered numbers individually and increment the counter if the number currently examinated is negative.
So first check a. Is it negative? If that is the case then increment the counter.
The same for b and c.

Only after writing such a (admittedly simple) piece of code then you might think, as improvement, to implement it using an array of inputs (iterating on them).
 
Share this answer
 
Think about how you would do it manually: You would start with a count of zero, then look at the first number. If it was negative, you would add one to the count. Then you'd do the same for the second, then for the third.
After that, you'd have the count.

So why do you need a loop at all? What does it bring to the exercise?

Implement what you would do manually - the code is simple, and you always get the correct result!
 
Share this answer
 
Comments
CPallini 27-Dec-22 5:38am    
5.
Richard MacCutchan 27-Dec-22 5:43am    
I wonder whether the actual question is a little more complicated ...
OriginalGriff 27-Dec-22 6:21am    
I suspect it is - but we can only work with what he types. :sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900