Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Program to Print Roll No of Students Seated at Even Position

Last Updated on April 27, 2023 by Prepbytes

Arrays are the data structures that enables us to store data of similar type under the same variable name. It can be used to store the details of the student in a compact an an efficient. Here we will learn how to write a program that prints the roll no. of students that are seated at the even position.

How to Print Roll No. of Students at Even Places

We have given N seats of students taking an English paper from an array of seats, where English paper students occupy even-numbered seats (0, 2, 4, …) and Hindi paper students occupy odd-numbered seats (1, 3, 5, …). We have to print the Students that are taking the English paper.

See original problem statement here

Sample Input:

N = 5 
Arr[] = [1, 2, 3, 4, 5]

Sample Output:

1 3 5

Algorithm

Here is the step-wise algorithm for implementing the above program.

  • Step 1: Declare a variable n, denoting the size of the array.
  • Step 2: Input the value of n from the user.
  • Step 3: Now declare an array of size n.
  • Step 4: Take the input values of the array by using a for loop.
  • Step 5: Now, traverse the entire array. If the current index is a multiple of 2, then print it else move ahead.

Code Implementation

Here is the code implementation of the above algorithm in C, C++, Java, and Python.

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int n;
    cin>>n;
    int arr[n];
    for(int i=0; i<n; i++) cin>>arr[i];
    for(int i=0; i<n; i++){
        if(i%2==0){
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int n;
    cin>>n;
    int arr[n];
    for(int i=0; i<n; i++) cin>>arr[i];
    for(int i=0; i<n; i++){
        if(i%2==0){
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}
import java.util.*;

class PrepBytes{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arr[] = new int[n];
        for(int i=0; i<n; i++){
            arr[i] = sc.nextInt();
        }
        for(int i=0; i<n; i++){
            if(i%2==0){
                System.out.print(arr[i]+" ");
            }
        }
    }
}
n = int(input())
arr = list(map(int,input().split()))
for i in range(n):
    if i%2==0:
        print(arr[i], end = " ")

Explanation:
The above program takes an integer input ‘t’ as the number of test cases. For each test case, it reads an integer ‘n’ and an array of size ‘n’. Then it prints every alternate element of the array starting from the first element using a for loop and an if condition. Students with roll numbers 1 3 5 are placed on even indexes i.e. 0 2 4. Keep traversing the array and print the elements that are placed on even indices as the English paper students are sitting on roll numbers like 0, 2, 4 etc. So, if the current index is divisible by 2, we print it on the screen, else we move ahead.

Time Complexity: O(n), as we are traversing the entire array with a for loop.

Space Complexity: O(1)

Conclusion
In this article, we learned how to print the roll no. of students that are seated at the even positions. This is a basic problem that is used for checking the proficiency of candidate in conditional statements and loops. Hope this blog helps you understand the concept.

Frequently Asked Questions(FAQs)

Q1. What is an array in C?
Ans: An array in C is a collection of elements of the same data type that are stored in contiguous memory locations. It allows us to store multiple values of the same type in a single variable.

Q2. What is a structure in C?
Ans: A structure in C is a user-defined data type that groups together variables of different data types under a single name. It allows us to store related data items in a structured format.

Q3. What is the use of loops in C?
Ans: Loops in C are used to execute a set of statements repeatedly until a certain condition is met. They provide a way to perform repetitive tasks efficiently and save time and effort.

Q4. Can we modify the elements of an array in C?
Ans: Yes, we can modify the elements of an array in C by accessing them using their index and assigning a new value to them.

Q5. What is the advantage of using arrays in C?
Ans: Arrays in C provide a convenient way to store and manipulate large amounts of data efficiently. They allow us to access elements by their index and perform operations on them using loops, which saves time and effort.

Leave a Reply

Your email address will not be published. Required fields are marked *