Computer Applications
Write a program to accept 10 numbers in a Single Dimensional Array. Pass the array to a method Search(int m[], int ns) to search the given number ns in the list of array elements. If the number is present, then display the message 'Number is present' otherwise, display 'number is not present'.
Java
User Defined Methods
74 Likes
Answer
import java.util.Scanner;
public class KboatSDASearch
{
public void search(int m[], int ns) {
boolean found = false;
for (int i = 0; i < m.length; i++) {
if (m[i] == ns) {
found = true;
break;
}
}
if (found)
System.out.println("Number is present");
else
System.out.println("Number is not present");
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.print("Enter number to search: ");
int num = in.nextInt();
KboatSDASearch obj = new KboatSDASearch();
obj.search(arr, num);
}
}
Variable Description Table
Program Explanation
Output
Answered By
16 Likes
Related Questions
Write a program in Java to accept a String from the user. Pass the String to a method First(String str) which displays the first character of each word.
Sample Input : Understanding Computer Applications
Sample Output:
U
C
AWrite a class with the name Perimeter using method overloading that computes the perimeter of a square, a rectangle and a circle.
Formula:
Perimeter of a square = 4 * s
Perimeter of a rectangle = 2 * (l + b)
Perimeter of a circle = 2 * (22/7) * r
Write a program in Java to accept the name of an employee and his/her annual income. Pass the name and the annual income to a method Tax(String name, int income) which displays the name of the employee and the income tax as per the given tariff:
Annual Income Income Tax Up to ₹2,50,000 No tax ₹2,50,001 to ₹5,00,000 10% of the income exceeding ₹2,50,000 ₹5,00,001 to ₹10,00,000 ₹30,000 + 20% of the amount exceeding ₹5,00,000 ₹10,00,001 and above ₹50,000 + 30% of the amount exceeding ₹10,00,000 Write a class with the name Area using method overloading that computes the area of a parallelogram, a rhombus and a trapezium.
Formula:
Area of a parallelogram (pg) = base * ht
Area of a rhombus (rh) = (1/2) * d1 * d2
(where, d1 and d2 are the diagonals)Area of a trapezium (tr) = (1/2) * ( a + b) * h
(where a and b are the parallel sides, h is the perpendicular distance between the parallel sides)