Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help in Arrays

zahooralizaizahooralizai Posts: 7Member

i want to show the element found and also it should show the position of the element, e.g. from the array having elements 1,2,3,4,5,6,7,8,9,0 and if element 6 is found then it show output as "Element Found = 6 at position 7 in array"

import javax.swing.*;
1.public class SearchingFromArray
3. public static void main(String[] args)
4. {
5. int a[]=new int[10];
6. int i,x,flag;
7. String st;
8. for(i=0;i<10;i++)
9. {
10. st=JOptionPane.showInputDialog("Enter Elements");
11. a[i]=Integer.parseInt(st);
12. }
13. st=JOptionPane.showInputDialog("Enter The Elements To Be Searched");
14. x=Integer.parseInt(st);
15. flag=0;
16. for(i=0;i<10;i++)
17. {
18. if (a[i]==x)
19. flag=1;
20. }
21. if (flag==1)
22. {
23. JOptionPane.showMessageDialog(null,"Elements Found in an Array = " +x+ " at Position " + a[i]);
24. }
25. else
26. JOptionPane.showMessageDialog(null,"Not Found");
27. }
Sign In or Register to comment.