All Prime Numbers Less Than or Equal to N

Given a positive integer N, your task is to return all the prime numbers less than or equal to N.

Note:

1) A prime number is a number that has only two factors: 1 and the number itself.
2) 1 is not a prime number.

Input:

The first line contains an integer 'T', which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow.
The first line of each test case contains only one integer 'N', as described in the problem statement.

Output:

For each test case, return a sequence of integers denoting the prime numbers less than or equal to 'N' in increasing order.

Example:

Input:
T = 2
N = 10
N = 20
Output:
2, 3, 5, 7
2, 3, 5, 7, 11, 13, 17, 19

Constraints:

  • 1 <= T <= 100
  • 2 <= N <= 5000
  • Time Limit: 1 sec
AnswerBot
4mo

Return all prime numbers less than or equal to a given positive integer N.

  • Iterate from 2 to N and check if each number is prime using a helper function.

  • A number is prime if it is not divisible by any ...read more

Help your peers!
Select
Add answer anonymously...

SAP Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked Q. Longest common subsequence
An Associate Software Engineer was asked Q. All Prime Numbers Less Than or Equal to N Given a positive integer N, your task ...read more
An Associate Software Engineer was asked Q. Problem: Total Area of Overlapping Rectangles You are given two rectangles situa...read more

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked Q1. Longest common subsequence
An Associate Software Engineer was asked Q2. All Prime Numbers Less Than or Equal to N Given a positive integer N, your task ...read more
An Associate Software Engineer was asked Q3. Problem: Total Area of Overlapping Rectangles You are given two rectangles situa...read more
SAP Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits