• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

find number

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]Hi!
How can I find the smallest number(nbr).
please help me.


class smallestNmr{
public static void main(String[]args){
int []v={7,2,9,3,5,4,9,2,2};

int nbr=0;
int first;
int secnd;
int k=0;
while(k<v.length-1){
second= v[k+1];
first= v[ k ];

if(second>first){
nbr=first;
}else{
nbr=second;
}
k++;
}
System.out.println(" "+nbr);
}
}
[code]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter:

The ending code tag should be [/CODE] not [CODE]. It is like html.

Your code looks nice but you did too much.

Just set nbr to v[0] and then iterate over the array, setting nbr to the smaller of nbr and the current element. When you are done, nbr will be the smallest value.

Hold onto your original code for when your class covers the bubble sort (my favorite).

Mike
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To figure this one out think about the actual process you would use in your head to determine which one is the lowest. You start by taking the first number and storing it in your memory. You then compare each of the other numbers in the array to the one stored in memory. If it's lower you update your memory with the lower one and continue on until you find another lower one. If you don't find another lower one by the time you are done - you have found the lowest number. Hope that helps.
 
peterx peter
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic