Java program of Generate number using odd digits

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:-

Main.java

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");
        }
    }
}

Recommended:-

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

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

x