logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

Java Program to Print Odd Elements at Odd Index Number

Posted on January 16, 2019 by staff10

This is the Java Program to Print Odd Elements at Odd Index Number.

Problem Description

Given an array of integers, print the odd numbers present at odd index numbers.
Example:

advertisement

Array = [2,1,4,4,5,7]

Output = 1,7

Problem Solution

Iterate through the array, and check whether the current index is even or odd. If it is odd then check the element at that index to be even or odd, print the element if it is odd.

Program/Source Code
advertisement

Here is the source code of the Java Program to Print Odd Elements at Odd Index Number. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Print Odd Elements at Odd Index Number
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class OddIndexedOddElements {
  8.     // Function to print Odd Elements present at Odd Index Number
  9.     static void printOddIndexedElements(int[] array){
  10.         int i=0;
  11.         for(i=0; i<array.length; i++){
  12.             if(i%2 != 0 && array[i]%2 !=0){
  13.                 System.out.print(array[i] + " ");
  14.             }
  15.         }
  16.     }
  17.  
  18.     // Function to read user input
  19.     public static void main(String[] args) {
  20.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  21.         int size;
  22.         System.out.println("Enter the size of the array");
  23.         try {
  24.             size = Integer.parseInt(br.readLine());
  25.         } catch (Exception e) {
  26.             System.out.println("Invalid Input");
  27.             return;
  28.         }
  29.         int[] array = new int[size];
  30.         System.out.println("Enter array elements");
  31.         int i;
  32.         for (i = 0; i < array.length; i++) {
  33.             try {
  34.                 array[i] = Integer.parseInt(br.readLine());
  35.             } catch (Exception e) {
  36.                 System.out.println("An error occurred");
  37.                 return;
  38.             }
  39.         }
  40.         System.out.println("Output");
  41.         printOddIndexedElements(array);
  42.     }
  43. }
Program Explanation

1. In function printOddIndexedElements(), the loop for(i=0; i<array.length; i++) iterates through the array.
2. Then using the condition if(i%2 != 0 && array[i]%2 != 0), we first check whether the current index is odd.
3. If the index is odd, then we check the element at that index (the condition after the && operator), if the element is odd, then display the element.

advertisement

Time Complexity: O(n) where n is the number of elements in the array.

Runtime Test Cases
 
Case 1 (Positive test Case - having odd elements at odd index):
 
Enter the size of the array
6
Enter array elements
2
1
4
4
5
7
Output
1 7 
 
Case 2 (Negative test Case - no odd element present at odd index):
 
Enter the size of the array
5
Enter array elements
1
2
3
4
5
Output

Sanfoundry Global Education & Learning Series – Java Programs.

« Prev Page - Java Program to Check if an Array is Strictly Increasing
» Next Page - Java Program to Find Local Maximas in an Array

« Java Program to Check if an Array is Strictly Increasing
Java Program to Find Local Maximas in an Array »
advertisement

Deep Dive @ Sanfoundry:

  1. Java Programming Examples on Java.Lang
  2. Simple Java Programs
  3. C Programming Examples on Arrays
  4. Java Programming Examples on Data-Structures
  5. Java Programming Examples on Combinatorial Problems & Algorithms
  6. Java Programming Examples on File Handling
  7. Java Programming Examples on String Handling
  8. Java Programming Examples on Mathematical Functions
  9. Java Programming Examples on Collections
  10. Java Programming Examples on Arrays
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.