Amplify Learning
Amplify Learning
g
import java.util.Scanner;
class Array_Max_Min
n
{
ni
public static void main(String[] args)
{
int a[] = new int[5];
ar
Scanner sc = new Scanner(System.in);
import java.util.Scanner;
g
class BinarySearch
{
n
public static void main(String[] args)
{
ni
int a[] = {31, 36, 45, 50, 60, 75, 86, 90};
Scanner sc = new Scanner(System.in);
System.out.println("Enter the element to be searched");
ar
int x = sc.nextInt(); //element to be found
//Binary Search
Le
int pos = -1; //it will save the index of found element
int start = 0, end = a.length-1;
while(start<=end)
{
ify
break;
}
else if(x<a[mid])
Am
end = mid-1;
else if(x>a[mid])
start = mid+1;
}
if(pos!=-1)
System.out.println("Element is found at " + (pos+1) + " position");
else
System.out.println("Element not found");
}
}
Q.3
import java.util.Scanner;
class Bubble_Sort_String
g
{
public static void main(String[] args)
n
{
String city[] = new String[10];
ni
Scanner sc = new Scanner(System.in);
System.out.println("Enter the names of 10 Cities");
for(int i=0; i<city.length; i++)
city[i] = sc.nextLine();
int n=city.length;
ify
//Bubble Sort
for(int i=0; i<n-1; i++)
{
pl
{
String t = city[j];
city[j] = city[j+1];
city[j+1] = t;
}
}
}
import java.util.Scanner;
class Convert
{
public static void main(String[] args)
g
{
Scanner sc = new Scanner(System.in);
n
System.out.println("Enter the Sentence");
String s = sc.nextLine();
ni
String t = ""; //result string
c = Character.toUpperCase(c);
}
t = t + c;
pl
System.out.println(t);
Am
}
}
Q.5)
import java.util.Scanner;
g
class Fare
{
n
public static void main(String[] args)
{
ni
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Distance in Kilometer");
int d = sc.nextInt();
double fare=0;
if(d>=1 && d<=10)
fare=80;
ar
Le
else if(d>=11 && d<=20)
fare = 6 * d; // rate x distance = fare
else if(d>=21 && d<=30)
fare = 5 * d;
else if(d>=31)
ify
fare = 4 * d;
System.out.println("Fare = Rs."+fare);
pl
}
}
Am
Q.6) Write a program in Java to check whether it is a mystery number or not.
A mystery number is a number that can be expressed as the sum of two
numbers and those two numbers should be the reverse of each other.
For Example,
132: 93+39 Mystery number
154: 68+86 Mystery number
23: Not a Mystery number
import java.util.Scanner;
class Mystery_Number
{
g
//to calculate the reverse
static int reverse(int n)
n
{
int rev=0;
ni
while(n!=0)
{
}
rev = rev*10 + n%10;
n /= 10; ar
Le
return rev;
}
if(i+reverse(i) == n)
{
flag = 1;
break;
}
}
if(flag == 1)
System.out.println(n+ " is a Mystery No.");
else
System.out.println(n+ " is not a Mystery No.");
}
}
Q7) Write a program to print this Pattern
n g
ni
class String_Pattern
{
public static void main(String[] args)
{
for(int i=1; i<=6; i++)
{
ar
Le
char c='A';
for(int j=1; j<=i; j++)
{
c = (char) ('A'+ (j-1));
System.out.print(c);
ify
}
//for rest of the REVERSE LINE
for(int k=i-1; k>0; k--)
pl
{
c = (char) (c-1);
System.out.print(c);
Am
}
System.out.println();
}
}
}
Q.8)
g
import java.util.Scanner;
n
class Vowels_Count
ni
{
public static void main(String[] args)
{
ar
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Sentence");
String s = sc.nextLine();
Le
s = s.toUpperCase();
int count[] = {0, 0, 0, 0, 0}; //to count Vowels
for(int i=0; i<s.length(); i++)
{
char ch = s.charAt(i);
ify