TCS Coding Practice Question | Prime Numbers upto N Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a number N, the task is to find the Prime Numbers from 1 to N using Command Line Arguments. Examples: Input: N = 7Output: 2, 3, 5, 7 Input: N = 13Output: 2, 3, 5, 7, 11, 13 Approach: Since the number is entered as Command line Argument, there is no need for a dedicated input lineExtract the input number from the command line argumentThis extracted number will be in String type.Convert this number into integer type and store it in a variable, say NNow loop through the numbers from 2 to N, using a variable say i, to check if they are prime or not. This is done as follows: In each iteration, Check if any numbers from 2 to (i/2+1) divide i completely (i.e. if it is a factor of i).If yes, then i is not a prime number. Hence check for next numberIf no, then i is a prime number. Hence print the value of i and check for next numberAfter the loop has ended, the prime numbers from 1 to N are printed on the screen. Note: Please note that 1 is not checked in this scenarios because 1 is neither prime nor composite. Program: C // C program to find // the Prime Numbers from 1 to N // using command line arguments #include <stdio.h> #include <stdlib.h> /* atoi */ // Function to check if x is prime int isPrime(int x) { int i; // Loop to check if x has any factor // other than 1 and x itself for (i = 2; i < x / 2 + 1; i++) { if (x % i == 0) { // Since i is a factor of x // x is not prime return 0; } } // x is prime return 1; } // Function to find prime numbers up to n void findPrimes(int n) { int i; // Loop from 2 to n // to find all prime numbers in between for (i = 2; i <= n; i++) { // Check if i is prime // If yes then print it // else continue to next number if (isPrime(i) == 1) printf("%d, ", i); } printf("\n"); } // Driver code int main(int argc, char* argv[]) { int n; // Check if the length of args array is 1 if (argc == 1) printf("No command line arguments found.\n"); else { // Get the command line argument and // Convert it from string type to integer type // using function "atoi( argument)" n = atoi(argv[1]); // Find all prime numbers upto n findPrimes(n); } return 0; } Java // Java program to find // the Prime Numbers from 1 to N // using command line arguments class GFG { // Function to check if x is prime public static int isPrime(int x) { int i; // Loop to check if x has any factor // other than 1 and x itself for (i = 2; i < x / 2 + 1; i++) { if (x % i == 0) { // Since i is a factor of x // x is not prime return 0; } } // x is prime return 1; } // Function to find prime numbers up to n public static void findPrimes(int n) { int i; // Loop from 2 to n // to find all prime numbers in between for (i = 2; i <= n; i++) { // Check if i is prime // If yes then print it // else continue to next number if (isPrime(i) == 1) System.out.print(i + ", "); } System.out.println(); } // Driver code public static void main(String[] args) { // Check if length of args array is // greater than 0 if (args.length > 0) { // Get the command line argument and // Convert it from string type to integer type int n = Integer.parseInt(args[0]); // Find all prime numbers upto n findPrimes(n); } else System.out.println("No command line " + "arguments found."); } } Output: In C: In Java: Time Complexity: O(N*N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Kempston Interview Experience SDE-1 R RishabhPrabhu Follow Improve Article Tags : Interview Experiences TCS TCS-coding-questions Practice Tags : TCS Similar Reads Kempston Interview Experience SDE-1 Round - 01Level: Easy Time Limit: 60 minutes Problem Set: 2 Problems Problem 1: Prime Number VerificationYou are required to implement a function that checks whether a given positive integer 'N' is a prime number or not. A prime number is a natural number greater than 1 that has no positive divisors 4 min read Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range Given two integers X and Y, the task is to perform the following operations: Find all prime numbers in the range [X, Y].Generate all numbers possible by combining every pair of primes in the given range.Find the prime numbers among all the possible numbers generated above. Calculate the count of pri 13 min read Print all Prime Quadruplet of a number less than it Given a positive integer n, print every Prime Quadruplet below n .Prime quadruplet: In mathematics, Prime Quadruplet is a set of four primes of the form {p, p+2, p+6, p+8 }.Example : Input : N = 15 Output : 5 7 11 13 Input : N = 20 Output : 5 7 11 13 11 13 17 19 A Simple solution to generate all Pri 7 min read Smallest composite number not divisible by first N prime numbers Given an integer N, the task is to find the smallest composite number which is not divisible by first N prime numbers. Examples: Input: N = 3 Output: 49Explanation:The first 3 prime numbers are {2, 3, 5}. The smallest composite integer not divisible by either 2, 3, or 5 is 49. Input: N = 2 Output: 2 7 min read Prime numbers from 1 to n in SAP ABAP SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP (Systems, Applications, and Products in Data Processing). It was developed in the 1980s for building business applications in the SAP environment. SAP ABAP is primaril 2 min read Prime Number of Set Bits in Binary Representation | Set 1 Given two integers âLâ and âRâ, write a program to find the total numbers that are having prime number of set bits in their binary representation in the range [L, R]. Examples: Input : l = 6, r = 10Output : 4Explanation :6 -> 110 (2 set bits, 2 is prime)7 -> 111 (3 set bits, 3 is prime)9 -> 13 min read Like