Considering this piece of Java code :
import java.util.Scanner;
class BreakWhileLoop {
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Input an integer");
n = input.nextInt();
if (n == 0) {
break;
}
System.out.println("You entered " + n);
}
}
}
Let's take this particular case : the user will always enter any integer except 0
.
1.Can i consider this code as an algorithm ?
2.If yes , how to calculate its complexity ?
Thanks
To avoid trivial answers, let us relax the problem statement by removing the except 0
condition.
Then yes, it is an algorithm, we can call it a 0 acceptor
.
Assuming that user input takes constant time, the time complexity is O(N)
where N
is the length of the nonzero sequence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With