[Solved] Command Line Argument Print String I with Java, C++

Command Line Argument Print String I: Write a program to accept a string as command line argument and print the arguments in the even positions.

Sample Input (Command Line Argument) 1:
Anu
Anandhi
Haritha
Karthiga
Madhu
Malini
Pragha
Punitha
Vidhya

Sample Output :
Names in even position :
Anandhi
Karthiga
Malini
Punitha

Solution

import java.util.Scanner;
public class Main{
    public static void main(String args[]){
        
        System.out.println("Names in even position :");
            for (int i = 1; i < args.length; i++)
            {
                if (i % 2 != 0)
                {
//sauravhathi
                    System.out.println(args[i]);
                }
            }
    }
}
#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
    cout<<"Names in even position :"<<endl;
    for (int i = 1; i < argc; i++)
    {
        if (i % 2 == 0)
        {
            cout << argv[i] << endl;
        }
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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