0% found this document useful (0 votes)
144 views8 pages

Amplify Learning

The document contains 8 Java programs that demonstrate various programming concepts: 1. A program that finds the maximum, minimum and sum of elements in an integer array 2. A program that implements binary search on an integer array 3. A program that sorts an array of strings using bubble sort 4. A program that converts characters in a string to uppercase/lowercase depending on case 5. A program that calculates fare based on distance traveled 6. A program that checks if a number is a "mystery number" 7. A program that prints a letter pattern 8. A program that counts the frequency of vowels in a sentence

Uploaded by

Mithilesh Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views8 pages

Amplify Learning

The document contains 8 Java programs that demonstrate various programming concepts: 1. A program that finds the maximum, minimum and sum of elements in an integer array 2. A program that implements binary search on an integer array 3. A program that sorts an array of strings using bubble sort 4. A program that converts characters in a string to uppercase/lowercase depending on case 5. A program that calculates fare based on distance traveled 6. A program that checks if a number is a "mystery number" 7. A program that prints a letter pattern 8. A program that counts the frequency of vowels in a sentence

Uploaded by

Mithilesh Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q.

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);

//Input values into the Array


Le
System.out.println("Enter the values in array");
for(int i=0; i<a.length; i++)
a[i] = sc.nextInt();

//Finding Maximum, Minimum, Sum


ify

int max = a[0];


int min = a[0];
int sum = 0;
pl

for(int i=0; i<a.length; i++)


{
sum += a[i];//calculate the sum
Am

if (a[i] > max)


max = a[i]; // we will change the max
if (a[i] < min)
min = a[i]; // we will change the min
}
System.out.println("Maximum = "+max);
System.out.println("Minimum = "+min);
System.out.println("Sum = "+sum);
}
}
Q.2

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

int mid = (start + end) / 2;


if(x==a[mid])
{
pos = mid;
pl

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();

//Print the Original Array


ar
Le
System.out.println("ORIGINAL ARRAY");
for(int i=0; i<city.length; i++)
System.out.print(city[i] + ", ");

int n=city.length;
ify

//Bubble Sort
for(int i=0; i<n-1; i++)
{
pl

for(int j=0; j<n-i-1; j++)


{
if(city[j].compareTo(city[j+1])>0)
Am

{
String t = city[j];
city[j] = city[j+1];
city[j+1] = t;
}
}
}

//Print the Sorted Array


System.out.println("SORTED ARRAY");
for(int i=0; i<city.length; i++)
System.out.print(city[i] + ", ");
}
}
Q.4) Write a java class to accept a string to convert all Capital
letter to small letters, small letters to capital letters. If it is a
digit leave as it is, and print the revised string.

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

for(int i=0; i<s.length(); i++)


{
char c = s.charAt(i); ar
//to check if character is a Alphabet
Le
if(Character.isLetter(c))
{
if(c>='A' && c<='Z')
c = Character.toLowerCase(c);
else if(c>='a' && c<='z')
ify

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;
}

public static void main(String[] args)


{
ify

Scanner sc = new Scanner(System.in);


System.out.println("Enter a number");
int n = sc.nextInt();
int flag = 0;
pl

for(int i=1; i<=n; i++)


{
Am

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

if(ch == 'A') ++count[0];


else if(ch == 'E') ++count[1];
else if(ch == 'I') count[2]++;
pl

else if(ch == 'O') count[3]++;


else if(ch == 'U') count[4]++;
}
Am

System.out.println("Frequency of 'A' = " + count[0]);


System.out.println("Frequency of 'E' = " + count[1]);
System.out.println("Frequency of 'I' = " + count[2]);
System.out.println("Frequency of 'O' = " + count[3]);
System.out.println("Frequency of 'U' = " + count[4]);
}
}

You might also like