Challenge: Count the Lucky Sevens
Completed
A shop gives a prize to every ticket whose number ends in 7. Read n ticket numbers and print how many win. Look at each ticket once: that's O(n).
Input
Line 1: n. Line 2: n positive whole numbers.
Output
The number of winning tickets.
Example
Input
6 17 23 7 100 4507 70
Output
3
Editor
Java
Hints
The last digit of a number is number % 10.
Add 1 to a counter for every ticket whose last digit is 7.
Solution explanation
We don't even need an array: each ticket is read, checked and forgotten. O(n) time, O(1) memory.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int winners = 0;
for (int i = 0; i < n; i++) {
int ticket = sc.nextInt();
if (ticket % 10 == 7) winners++;
}
System.out.println(winners);
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module