java basic problem for those students are learning
- find a elemet in array available or not
public class HelloWorld{
public static int bsearch(int nums [] ,int k){
for (int i = 0 ; i<nums.length;i++){
if(nums[i] == k){
return i;
}
}
return -1;
}
public static void main(String []args){
int nums[]= {23,45,67,89,34,22,56,78,89};
int key = 89;
int dd = bsearch(nums , key);
if(dd == -1){
System.out.print("not found");
}
else{
System.out.print(" availble index of : "+dd);
}
}
}
output: availble index of :8
- find a element to add two element in array
class HelloWorld {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6};
int k = 6;
int r = find(arr,k);
}
public static int find(int arr[] , int k){
for(int i = 0; i<arr.length;i++){
for(int j = i;j<arr.length; j++){
if(arr[i] + arr[j] == k)
{
System.out.println(arr[i] + "+" + arr[j] + "=" + k);
}
}
}
return 0;
}
}
output:
1+5=6
2+4=6
3+3=6
Basic Patterns:
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
System.out.println("Squre star pattern---------->");
for(int i = 0; i<4 ; i++){
for(int j = 0 ; j<4; j++){
System.out.print("*");
}
System.out.println();
};
System.out.println("Accending star pattern---------->");
for(int r = 0; r<7 ; r++){
for(int c = 0 ; c<r; c++){
System.out.print("*");
}
System.out.println();
};
System.out.println("Inverted star pattern---------->");
for (int line = 0; line <=6; line++){
for(int star = 0; (star<6-line+1); star++){
System.out.print("*");
}
System.out.println();
};
System.out.println("half Pyrmid number pattern---------->");
for(int i = 1 ; i<=5; i++){
for(int x = 1 ; x<=i ; x++){
System.out.print(x);
}
System.out.println();
};
System.out.println("Alphbets pattern---------->");
char ch = 'A';
for(int i = 1 ; i<=4;i++){
for(int l = 1; l<=i; l++){
System.out.print(ch);
ch++;
}
System.out.println();
}
}}
output:
Squre star pattern---------->
****
****
****
****
Accending star pattern---------->
*
**
***
****
*****
******
Inverted star pattern---------->
*******
******
*****
****
***
**
*
numeric half Pyrmid number pattern---------->
1
12
123
1234
12345
Alphbets pattern---------->
A
BC
DEF
GHIJ
thanks for visit
No comments: