Write a program that given a list of 30 integers and an inte

Write a program that, given a list of 30 integers and an integer n, determines the position of the first integer in the list that is smaller than n.

Java please and a pseudocode program

Solution


public class FindSmallerNo{

     public static void main(String []args){
         //declare array of elemnts
         int numbers[]={32,54,9,21,98,48};
         //no to find
         int n=21;
         int index=-1;
       
         //loop through the array and find the no lesser than n
         for(int i=0;i<numbers.length;i++)
         {
             //check index of elemnt smaller than n
             if(numbers[i]<n)
             {
                 index=i;
                 i=numbers.length;
             }
         }
        System.out.println(\"Element smaller than \"+n+\" is found at \"+index );
     }
}

Sample Output:

Element smaller than 21 is found at 2  

Psewudo Code:


declare numbers array
declare n
initialize index to 0
For each i in [0,numbers.length-1]
if numbers[i] > n
     set index to i
exit loop
print \"Element found at \"index
  

Write a program that, given a list of 30 integers and an integer n, determines the position of the first integer in the list that is smaller than n. Java please

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site