Generate number using odd digits Program in java

Question:

Jose gets n numbers in an array. Write a Java program to take the single digit odd numbers in the array and make a number by combining those odd numbers alone.Example: If the array is {2,7,14,24,41,3} the output should be a single number 73, if not found print “Single digit odd number is not found in the array”.If the array size is zero or lesser then display the message as “Invalid array size”


Sample Input 1:

Enter the size of an array:

4

Enter the array elements:

45

3

56

7

Sample Output 1:

37


Sample Input 2:

Enter the size of an array:

0

Sample Output 2:

Invalid array size

CODE:

import java.util.Scanner;
public class Main
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of an array:");
        int n=sc.nextInt();
        if(n>0)
        {
        
        int arr[]=new int[n];
        System.out.println("Enter the array elements:");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        
        String output="";
        
        for(int j=0;j<n;j++)
        {
            if(arr[j]>=0&&arr[j]<10)
            {
                if(arr[j]%2!=0)
                {
                    output=output.concat(String.valueOf(arr[j]));
                }
            }
        }
        if(output.equals(""))
            System.out.println("Single digit odd number is not found in the array");
        
        else
            System.out.println(output);
        }
        
        else
        {
            System.out.println("Invalid array size");
        }
    }
}
Next:
  1. Alternate Number Difference
  2. Next Greatest number
  3. Mark Comparison
  4. Print the characters in descending order

Leave a Comment

Scroll to Top