[go: up one dir, main page]

0% found this document useful (0 votes)
246 views148 pages

Java Tfa

The document contains 5 Java programs that perform various array and string operations: 1) Adds an index value to array elements, 2) Calculates the product of uppercase and lowercase letters in a string, 3) Sums even elements in two arrays, 4) Checks if an array is in ascending order, 5) Removes characters that are not unique within strings. The programs demonstrate basic programming concepts like loops, conditionals, arrays, and string manipulation.
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)
246 views148 pages

Java Tfa

The document contains 5 Java programs that perform various array and string operations: 1) Adds an index value to array elements, 2) Calculates the product of uppercase and lowercase letters in a string, 3) Sums even elements in two arrays, 4) Checks if an array is in ascending order, 5) Removes characters that are not unique within strings. The programs demonstrate basic programming concepts like loops, conditionals, arrays, and string manipulation.
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/ 148

1.

Add With Index

2
import java.util.*;
public class AddWithIndex {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size");
int n=sc.nextInt();
if(n<5)
{
System.out.print("Array size "+n+" is too low");
return;
}
else if(n>10)
{
System.out.print("Array size "+n+" is too high");
return;
}
else
{
System.out.println("Enter the elements");
int array[]=new int[n];
for(int i=0;i<n;i++)
array[i]=sc.nextInt();
int max=array[0],index=0,count=0;
for(int i=0;i<n;i++)
{
if(array[i]>max)
{
max=array[i];
index=i;
}
else if(max==array[i])
count++;
}
if(count==n) {
System.out.println("These "+n+" values contain the same
elements");
return;
}

for(int i=0;i<array.length;i++)
System.out.println(array[i]+index);
}
}
}
2. Product of Case Count

import java.util.*;
import java.util.regex.Pattern;
public class ProductOfUppercaseAndLowercase {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String input=sc.nextLine();
if(!(Pattern.matches("[a-zA-Z]+", input)))
{
System.out.println(input + " is an invalid string");
return;
}
else if(input.length()>10)
{
System.out.println(input.length()+" exceeds the limit");
return;
}
else
{
int upper=0,lower=0;
for(int i=0;i<input.length();i++)
{
char c=input.charAt(i);
if(c>='A' && c<='Z')
upper+=1;
else if(c>='a' && c<='z')
lower+=1;
}
System.out.println("Product value is "+ upper*lower);
}
}
}

3. Summation
import java.util.*;
public class Summation {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size");
int n=sc.nextInt();
if(n>=1 && n<=10)
{
int array1[]=new int[n];
int array2[]=new int[n];
System.out.println("Enter the elements in first array");
for(int i=0;i<array1.length;i++)
array1[i]=sc.nextInt();
System.out.println("Enter the elements in second array");
for(int i=0;i<array2.length;i++)
array2[i]=sc.nextInt();
int c=0;
for(int i=0;i<n;i++)
{
if((array1[i]%2)==0)
c=1;
if((array2[i]%2)==0)
c=1;
}
if(c==0)
{
System.out.println("There are no even elements in the arrays");
return;
}
int sum[] =new int[n];
for(int i=0;i<sum.length;i++)
{
if(array1[i]%2==0 && array2[i]%2==0)
sum[i]=array1[i]+array2[i];
else
sum[i]=0;
System.out.println(sum[i]);
}
}
else
{
System.out.println(n+" is an invalid array size");
return;
}
}

}
Alternative Method
import java.util.*;

public class Summation {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size");
int n = sc.nextInt();
if (n >= 10 || n < 1) {
System.out.println(n + " is an invalid input");
return;
}
int arr1[] = new int[n];
int arr2[] = new int[n];
int result[] = new int[n];
System.out.println("Enter array elements in 1st array");
for (int i = 0; i < n; i++) {
arr1[i] = sc.nextInt();
}
System.out.println("Enter array elements in 2nd array");
for (int i = 0; i < n; i++) {
arr2[i] = sc.nextInt();
}
int count = 0;
for (int i = 0; i < n; i++) {
if ((arr1[i] % 2 == 0 || arr1[i] == 0) && (arr2[i] % 2 == 0 || arr2[i] == 0)) {
result[i] = arr1[i] + arr2[i];
count++;
} else {
result[i] = 0;
}
}
if (count == 0) {
System.out.println("There are no even elements in the Array");
}
for (int i = 0; i < n; i++) {
System.out.println(result[i]);
}
}
}
4. Order Identification
import java.util.*;
public class AscendingOrder {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size");
int n=sc.nextInt();
if(n>=2 && n<=10)
{
int array[]=new int[n];
System.out.println("Enter the elements");
for(int i=0;i<array.length;i++)
array[i]=sc.nextInt();
boolean flag=false;
for(int i=0;i<array.length-1;i++)
{
if(array[i]<array[i+1])
continue;
else
flag=true;
break;
}
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
if(flag==true)
System.out.println("are not in ascending order");
else
System.out.println("are in ascending order");
}
else
{
System.out.println(n+" is not a valid array size");
return;
}
}
}
5. Non unique elimination

import java.util.*;
import java.util.regex.Pattern;
public class NonUniqueElimination {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
if(Pattern.matches("[a-zA-Z ]+", s))
{
String array[]=s.split(" ");
String result="";
for(int k=0;k<array.length;k++)
{
String sub="";
int count=0;
for(int i=0;i<array[k].length();i++)
{
count=0;
char ch= array[k].charAt(i);
for(int j=0;j<array[k].length();j++)
{
if(i==j)
continue;
else if(ch==array[k].charAt(j))
count++;
}
if(count==0)
sub+=ch;
else
continue;
}
result+=sub+" ";
}
System.out.println(result);
}
else
{
System.out.println(s+" is an invalid sentence");
return;
}
}
}

6. Rating score
import java.util.*;
public class RatingScore {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int jessArray[]=new int[5];
int jamesArray[]=new int[5];
System.out.println("Enter Jesson Score");
for(int i=0;i<5;i++)
{
jessArray[i]=sc.nextInt();
if(jessArray[i]<0)
{
System.out.println(jessArray[i]+" is invalid");
return;
}
}
System.out.println("Enter James Score");
for(int j=0;j<5;j++)
{
jamesArray[j]=sc.nextInt();
if(jamesArray[j]<0)
{
System.out.println(jamesArray[j]+" is invalid");
return;
}
}
int jess=0,james=0;
for(int k=0;k<5;k++)
{
if(jessArray[k]>jamesArray[k])
jess++;
else if(jessArray[k]<jamesArray[k])
james++;
else
continue;//to skip if both gets same score
}
System.out.println("Jesson Score\n"+jess);
System.out.println("James Score\n"+james);
}
}
7. Anagram sentence
import java.util.*;
public class AnagramSentence {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter sentence 1");
String s=sc.nextLine();
System.out.println("Enter sentence 2");
String p=sc.nextLine();
String input1[]=s.split("");
String input2[]=p.split("");
Arrays.sort(input1);
Arrays.sort(input2);
boolean res=Arrays.equals(input1, input2);
if(res==true)
System.out.println(s+" and "+p+" contain the same characters");
else
System.out.println(s+" and "+p+" contain the different characters");

}
}
8. Operator Found
import java.util.*;
public class OperatorFound {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter the n1 and n2");


int n1 = sc.nextInt();
if (n1 <= 0) {
System.out.println(n1 + " is an invalid number");
return;
}
int n2 = sc.nextInt();
if (n2 < 0) {
System.out.println(n2 + " is an invalid number");
return;
}
boolean b = false;
System.out.println("Function answer n3");
int n3 = sc.nextInt();

if ((n1 + n2) == n3) {


System.out.println(n1 + "+" + n2 + "=" + n3);
b = true;
}
if ((n1 - n2) == n3) {
System.out.println(n1 + "-" + n2 + "=" + n3);
b = true;
}
if ((n1 * n2) == n3) {
System.out.println(n1 + "*" + n2 + "=" + n3);
b = true;
}
if ((n1 / n2) == n3) {
System.out.println(n1 + "/" + n2 + "=" + n3);
b = true;
}
if (b == false) {
System.out.println(n3 + " is an invalid number");
}
}
}

ALTERNATIVE METHOD

import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the n1 and n2");
int n1 = sc.nextInt();
if (n1 <= 0) {
System.out.println(n1 + " is an invalid number");
return;
}
int n2 = sc.nextInt();
if (n2 <= 0) {
System.out.println(n2 + " is an invalid number");
return;
}
System.out.println("Function answer n3");
int n3 = sc.nextInt();
boolean flag = false;
if (n1 + n2 == n3) {
System.out.println(n1 + "+" + n2 + "=" + n3);
flag=true;
}
if (n1 - n2 == n3)
{
System.out.println(n1 + "-" + n2 + "=" + n3);
flag=true;
}
else if (n1 * n2 == n3)
{
System.out.println(n1 + "*" + n2 + "=" + n3);
flag=true;
}
else if (n1 / n2 == n3)
{
System.out.println(n1 + "/" + n2 + "=" + n3);
flag=true;
}
else if (n1 % n2 == n3)
{
System.out.println(n1 + "%" + n2 + "=" + n3);
flag=true;
}
if (flag == false)
{
System.out.println(n3 + " is an invalid number");
}
}
}
9. Absolute Difference
import java.util.*;

public class AbsoluteDifference {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of values");
int value = sc.nextInt();
if (value > 0 && value % 2 == 0) {
int[] arr = new int[value];
System.out.println("Enter the numbers");
for (int i = 0; i < value; i++) {
arr[i] = sc.nextInt();
if (arr[i] <= 0) {
System.out.println(arr[i] + " is an invalid number");
return;
}
}
int sum = 0;
for (int i = 0; i < value / 2; i++) {
sum = sum + (Math.abs(arr[i] - arr[value - i - 1]));
}
System.out.println(sum);

} else {
System.out.println(value + " is an invalid size");
}
}
}

10. Block Score


import java.util.*;
import java.util.regex.Pattern;

public class BlocksScore {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s = sc.nextLine();
if (Pattern.matches("[A-Za-z0-9 ]+", s)) {
String[] array = s.split("");
System.out.println("Enter the Letter");
String letter = sc.nextLine();
int with_letter_sum = 0, total_sum = 0;
boolean flag = false;
for (int i = 0; i < array.length; i++) {
if (array[i].equalsIgnoreCase(letter)) {
with_letter_sum += i;
flag = true;
}
total_sum += i;
}
if (flag == false)
System.out.println("Score is 0");
else
System.out.println("Score is " + (with_letter_sum) * (total_sum -
with_letter_sum));
} else {
System.out.println(s + " is not a valid String");
return;
}
}
}

11. Lunch Together


import java.util.*;

public class LunchTogether {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the day interval of Tony");
int tony = sc.nextInt();
if (tony <= 0) {
System.out.print(tony + " is not a valid day");
return;
} else {
System.out.println("Enter the day interval of Potts");
int potts = sc.nextInt();
if (potts <= 0) {
System.out.println(potts + " is not a valid day");
return;
} else {
System.out.println("Tony and Potts will have lunch together on
day " + findLCM(tony, potts));
}
}
}

public static int findLCM(int tony, int potts) {


return (tony * potts) / GCD(tony, potts);
}

public static int GCD(int tony, int potts) {


if (tony == 0)
return potts;
else if (potts == 0 || tony == potts)
return tony;
if (tony > potts)
return GCD(tony - potts, potts);
return GCD(tony, potts - tony);
}
}

12. Next mixing letters


import java.util.*;

public class Nextmixingletters {


public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the String");


String str = sc.next();

int diff = str.charAt(0) - str.charAt(1);


for (int i = 0; i < str.length() - 1; i++) {
int val1 = str.charAt(i);
int val2 = str.charAt(i + 1);
if (diff != (val1 - val2)) {
System.out.println(str + " is not in Sequence");
return;
}
}
System.out.println("Enter the sequence times");
int seq = sc.nextInt();
if (seq <= 0) {
System.out.println(seq + " is invalid");
return;
}
int output = str.charAt(str.length() - 1);
for (int i = 0; i < seq; i++) {
output = output - diff;
if (output > 122) {
output = output - 26;
char last = (char) output;
System.out.print(last);
} else if (output < 97) {
output = output + 26;
char last = (char) output;
System.out.print(last);
} else {
char last = (char) output;
System.out.print(last);
}
}
}
}

13. Reverse a word


import java.util.regex.Pattern;

public class ReverseAWord {

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
String s = sc.nextLine();
String ssplit[] = s.split(" ");
if (ssplit.length < 3) {
System.out.println("Invalid Sentence");
return;
} else {
for (int i = 0; i < ssplit.length; i++) {
if (Pattern.matches("[A-Za-z ]+", ssplit[i]))
continue;
else {
System.out.println("Invalid Word");
return;
}
}
String[] firstArray = new String[ssplit.length];
for (int j = 0; j < ssplit.length; j++) {
firstArray[j] = Character.toString(ssplit[j].charAt(0));
}
int c = 0;
for (int l = 0; l < firstArray.length - 1; l++) {
if ((firstArray[l].equalsIgnoreCase(firstArray[l + 1])))
c++;
}
if (c != firstArray.length - 1)
System.out.println(reverse(ssplit[0]) + ssplit[ssplit.length - 1]);
else
System.out.println(reverse(ssplit[ssplit.length - 1]) + ssplit[0]);
}

public static String reverse(String string) {


// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
sb = sb.append(string).reverse();
return sb.toString();
}

ALTERNATIVE METHOD

import java.util.*;
import java.util.regex.Pattern;

public class ReverseAWord {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String ssplit[] = s.split(" ");
if (ssplit.length < 3) {
System.out.println("Invalid Sentence");
return;
}
for (int i = 0; i < ssplit.length; i++) {
if (Pattern.matches("[A-Za-z]+", ssplit[i]))
continue;
else {
System.out.println("Invalid Word");
return;
}
}
char first_letter = ssplit[0].charAt(0);
int flag = 0;
for (int i = 1; i < ssplit.length; i++) {
if (Character.toLowerCase(ssplit[i].charAt(0)) ==
Character.toLowerCase(first_letter)) {
flag = 1;
} else
flag = 0;
break;
}
if (flag == 1) {
StringBuffer sb = new StringBuffer(ssplit[ssplit.length - 1]);
String rev = sb.reverse().toString();
System.out.println(rev + ssplit[0]);
} else {
StringBuffer sb = new StringBuffer(ssplit[0]);
String rev = sb.reverse().toString();
System.out.println(rev + ssplit[ssplit.length - 1]);
}
}
}

14. Toy Shop


import java.util.*;
public class ToyShops {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of months");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is an invalid number of month");
return;
} else {
System.out.println("Enter the number of toys purchased monthly");
int month_array[] = new int[n];
for (int i = 0; i < month_array.length; i++) {
month_array[i] = sc.nextInt();
if (month_array[i] < 10) {
System.out.println(month_array[i] + " is an invalid number
of toys");
return;
}
}
int max = month_array[0], index = 0;
for (int i = 0; i < n; i++)
{
if (max < month_array[i])
{
max = month_array[i];

}
}
for(int i=0;i<n;i++)
{
if(max==month_array[i])
{
System.out.println("Month " + (i+1));
}
}
}

}
}

15. Bike Rent


import java.util.Scanner;
import java.util.regex.Pattern;

public class BikeRent {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
String name = sc.next();
if (Pattern.matches("[A-Za-z]+", name)) {
System.out.println("Enter the time duration");
int time = sc.nextInt();
if (time >= 1 && time <= 24) {
System.out.println("List of payment options");
System.out.println("1)Visa card\n2)Rupay card\n3)Master card");
System.out.println("Choose an option");
int option = 0;
while (true) {
option = sc.nextInt();
if (option <= 0 || option > 3)
System.out.println("Try again");
else
break;
}
double sal = 0, dis = 0;
if (option == 1) {
if (time >= 5) {
sal = 120 * time;
dis = (sal * 0.25);
sal = sal - dis;
} else {
sal = 120 * time;
}
} else if (option == 2) {
if (time >= 5) {
sal = time * 120;
dis = (sal * 0.17);
sal = sal - dis;
} else {
sal = time * 120;
}
} else if (option == 3) {
sal = time * 120;
}
System.out.print("Dear " + name + " your bill amount is ");
System.out.printf("%.2f", sal);
} else {
System.out.println(time + " is an invalid time duration");
return;
}
} else {
System.out.println(name + " is an invalid name");
return;
}
}
}
16. Ticket Code Decryption/Movie Ticket
import java.util.*;

public class TicketCodeDecryption {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
if (s.length() != 6) {
System.out.println("Invalid Input");
return;
} else {
char gate = s.charAt(0);
if (gate == 'N' || gate == 'E' || gate == 'W' || gate == 'S') {
char block = s.charAt(1);
if (block == 'A' || block == 'B' || block == 'C' || block == 'D' || block
== 'E' || block == 'F') {
char floor = s.charAt(2);
if (floor == '1' || floor == '2' || floor == '3' || floor == '4') {
int number = Integer.parseInt(s.substring(3, 6));
if (number < 0 || number > 500) {
System.out.println("Invalid Seat Number");
return;
} else {
System.out.println("Gate " + gate);
System.out.println("Block " + block);
System.out.println("Floor " + floor);
System.out.println("Seat" + number);
}
} else {
System.out.println("Invalid Floor Number");
return;
}
} else {
System.out.println("Invalid Block Name");
return;
}
} else {
System.out.println("Invalid Gate Name");
return;
}
}
}
}

17.Calculate The Offer Price


import java.util.*;

public class test {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String n = sc.nextLine();
int offerPrice = 0;
if (5 < n.length() && n.length() < 9) {
String s = n.substring(n.length() - 4);
int price = Integer.parseInt(s);
if (0 < price && price < 10) {
offerPrice = price;
} else if (10 < price && price < 51) {
offerPrice = price - 5;
} else if (50 < price && price < 501) {
offerPrice = price - 15;
} else if (500 < price && price < 5001) {
offerPrice = price - 105;
} else if (5000 < price) {
offerPrice = price - 1005;
}
System.out.println("MRP: Rs " + price);
System.out.println("Offer Price: Rs " + offerPrice);
} else {
System.out.println("Invalid Input");
}
}
}

18. Sum of Max Element


import java.util.*;
class SumOfMaxElement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
if (n < 5) {
System.out.println("Array size " + n + " is too low");
return;
} else if (n > 10) {
System.out.println("Array size " + n + " is too high");
return;
} else {
System.out.println("Enter the elements");
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
arr.add(sc.nextInt());
}
int max_element = Collections.max(arr);
int max_element_index = arr.indexOf(max_element);
if (Collections.frequency(arr, max_element) == arr.size()) {
System.out.println("These " + n + " values contain the same
elements");
} else {
for (int i : arr) {
System.out.println(i + max_element_index);
}
}
}
}
}

ALTERNATIVE METHOD

import java.util.*;

public class SumOfMaxElement {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
System.out.println("Enter the elements");
int[] arr = new int[n];
if (n < 5) {
System.out.println("Array size " + n + " is too low");
return;
} else if (n > 10) {
System.out.println("Array size " + n + " is too high");
return;
}
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
boolean flag = true;
for (int i = 1; i < n; i++) {
if (arr[0] != arr[i])
flag = false;
}
if (flag) {
System.out.println("These " + n + " values contain the same elements");
return;
}
int max = arr[0];
int index = 0;
for (int i = 0; i < n; i++) {
if (max < arr[i]) {
max = arr[i];
index = i;
}
}
for (int i = 0; i < n; i++) {
System.out.println(arr[i] + index);
}
}
}

19. Gender Determination

import java.util.regex.*;

public class GenderDetermination {

public static void main(String[] args) {


java.util.Scanner sc = new java.util.Scanner(System.in);
String s = sc.nextLine();
if (Pattern.matches("[MmFf]+", s)) {
int male = 0, female = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == 'M' || ch == 'm')
male++;
else if (ch == 'F' || ch == 'f')
female++;
}
System.out.println(male + " Male");
System.out.println(female + " Female");
} else {
System.out.println("Invalid Input");
return;
}
}
}

20. Mega Mart Customer Identification


import java.util.*;

public class MegaMartCustomerIdentification {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Customer Id");
String s = sc.nextLine();
String sname = s.substring(0, 4);
if (sname.equals("Mega")) {
String ctype = "";
if (s.contains("Gold") || s.contains("Platinum") || s.contains("Silver")) {
if (s.contains("Gold"))
ctype = "Gold";
else if (s.contains("Silver"))
ctype = "Silver";
else if (s.contains("Platinum"))
ctype = "Platinum";
int n = Integer.parseInt(s.replaceAll("[^0-9]", ""));
if (n > 99 && n < 1000)
System.out.println("Welcome Mega Mart " + ctype + "
Customer");
else {
System.out.println("Invalid Member Id");
return;
}
} else {
System.out.println("Invalid Customer Type");
return;
}
} else {
System.out.println("Invalid Shop Name");
return;
}
}
}

21. Theater Seat Details


import java.util.regex.Pattern;

public class TheaterSeatDetails {


public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
String s = sc.nextLine();
if (s.length() != 10) {
System.out.println("Invalid Input");
return;
} else {
if (Pattern.matches("[0-9]+", s.substring(5, 7)) &&
Integer.parseInt(s.substring(5, 7)) >= 1
&& Integer.parseInt(s.substring(5, 7)) <= 10) {
int seatsnumber = Integer.parseInt(s.substring(5, 7));
String eightletter = s.substring(7, 8);
if (Pattern.matches("[A-Za-z]", eightletter) &&
Pattern.matches("[0-9]+", s.substring(8, 10))) {
int n = Integer.parseInt(s.substring(8, 10));
System.out.println("Seat Number");
for (int i = 0; i < seatsnumber; i++) {
System.out.println(eightletter + (n++));
}
} else {
System.out.println("Invalid Seat Number");
return;
}
} else {
System.out.println("Invalid Count");
return;
}
}
}
}

22. Compose Rhythm


import java.util.*;

public class ComposeRhythm {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
boolean flag = false;
if (a > b) {
System.out.println("Invalid Input");
return;
} else if (a < 0 || b < 0) {
System.out.println("Negative integers cannot be prime");
return;
} else if (a <= b && a > 0 && b > 0) {
for (int i = a; i <= b; i++) {
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0)
count++;
}
if (count == 2) {
System.out.print(i + " ");
flag = true;
}
}
}
if (flag == false) {
System.out.println("There are no prime numbers in the range " + a + " to "
+ b);
return;
}
}
}

23. Combine Numbers

import java.util.*;

public class CombineNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of students");
int n = sc.nextInt();
if (n < 0) {
System.out.println(n + " is an invalid size");
return;
} else {
int[] numbers = new int[n];
System.out.println("Enter the roll number");
boolean flag = false;
String output = "";
for (int i = 0; i < n; i++) {
numbers[i] = sc.nextInt();
if (numbers[i] < 0) {
System.out.println(numbers[i] + " is an invalid roll
number");
return;
}
if (numbers[i] % 2 != 0) {
output += numbers[i];
flag = true;
} else
continue;
}
if (flag == false) {
System.out.println("The " + n + " numbers are not odd");
return;
}
System.out.println(output);
}
}
}

24. Return Product


import java.util.*;
public class ReturnProduct {
public static void main(String[] args) {
int prod = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Array size");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is an invalid array size");
return;
}
List<Integer> arr = new ArrayList<>();
System.out.println("Enter Array Element");
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
arr.add(a);
if (a <= 0) {
System.out.println(a + " is an invalid array element");
return;
}
}

for (int i : arr) {


if (!isprime(i)) {
System.out.print("No element found in " + arr.get(0));
for (int j = 1; j < n; j++) {
System.out.print("," + arr.get(j));
}
return;
} else {
prod = prod * i;
}
}
System.out.println(prod);
}

public static boolean isprime(int i) {


for (int j = 2; j < i - 1; j++) {
if (i % j == 0) {
return false;
}
}
return true;
}
}

ALTERNATIVE METHOD

import java.util.*;
import java.math.*;

public class ReturnProduct {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Array size");
int n = sc.nextInt();
System.out.println("Array elements");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

boolean flag = true;


int product = 1;
for (int i = 0; i < n; i++) {

if (CheckPrime(arr[i])) {
flag = true;
product = product * arr[i];
} else {
flag = false;

}
}
if (flag)
System.out.println(product);
else {
System.out.print("No element found in ");
for (int i = 0; i < n - 1; i++) {
System.out.print(arr[i] + ",");
}
System.out.print(arr[n - 1]);
}
}

public static boolean CheckPrime(int n) {


BigInteger b = new BigInteger(String.valueOf(n));
return b.isProbablePrime(3);
}
}

25. Even Position Caps


import java.util.Scanner;
import java.util.regex.Pattern;

public class EvenPositionCaps {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Input string");
String s = sc.nextLine();
if (!Pattern.matches("[A-Za-z]+", s)) {
System.out.println(s + " is an invalid input");
return;
} else {
if (s.length() < 5 || s.length() > 20) {
System.out.println(s + " is an invalid length");
return;
}
String output = s.charAt(0) + "";
for (int i = 0; i < s.length() - 1; i++) {
if ((i) % 2 == 0)
output += Character.toString(s.charAt(i +
1)).toUpperCase();
else
output += s.charAt(i + 1);
}
System.out.println(output);
}
}
}
26. Speed calculation/Rush Hour
import java.util.*;

public class SpeedCalculation {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the distance in kilometre");
int km = sc.nextInt();
boolean flag = false;
if (km > 0) {
System.out.println("Enter the time to reach in hours");
int hours = sc.nextInt();
if (hours > 0) {
flag = true;
int normal_speed = 30;
int customer_speed = km / hours;
if (customer_speed < normal_speed)
System.out.println("You drive a car at normal speed");
else
System.out.println("You want to increase a speed " +
(customer_speed - normal_speed)
+ " km/hr from a normal speed");
}
}
if (flag == false) {
System.out.println("Invalid Input");
return;
}
}
}

27. Find Similarity


import java.util.*;
import java.util.regex.Pattern;
public class FindSimilarity {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first String");
String input1 = sc.nextLine();
String input2 = "";
boolean flag = false;
if (Pattern.matches("[A-Za-z]+", input1)) {
System.out.println("Enter the second string");
input2 = sc.nextLine();
if (Pattern.matches("[A-Za-z]+", input2))
{
flag = true;
if (input1.length() != input2.length()) {
System.out.println("Length of " + input1 + " and " + input2 + "
does not match");
return;
}
char str1[] = input1.toUpperCase().toCharArray();
char str2[] = input2.toUpperCase().toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
if (Arrays.equals(str1, str2))
System.out.println(input1 + " and " + input2 + " are similar");
else
System.out.println(input1 + " and " + input2 + " are non similar");
}
}
if (flag == false) {
System.out.println(input1 + " or " + input2 + " is not a valid string");
return;
}
}
}

28. Adam’s Conviction


import java.util.*;
import java.util.regex.*;

public class AdamsConviction {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
if (Pattern.matches("[HhTt]+", str) && str.length() >= 2) {
int count = 0;
String[] x = str.split("");
for (int i = 0; i < str.length() - 1; i++) {
if ((x[i].equalsIgnoreCase(x[i + 1]))) {
System.out.println("Team may lose");
count = 1;
return;
}
}
if (count == 0)
System.out.println("Team will win");
} else
System.out.println("Invalid Input");
}
}
29. Holiday Fun

import java.util.*;

public class HolidayFun {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size");
int size = sc.nextInt();
if (size <= 0) {
System.out.println("Invalid size");
return;
} else {
System.out.println("Enter the numbers");
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
ArrayList<Integer> total = new ArrayList<>();
for (int i : arr) {
int numsize = String.valueOf(i).length();
if (numsize % 2 == 0) {
int evensum = 0;
int oddsum = 0;
while (i != 0) {
evensum = evensum + i % 10;
i = i / 10;
oddsum = oddsum + i % 10;
i = i / 10;
}
total.add(evensum);
} else {
int evensum = 0;
int oddsum = 0;
while (i != 0) {
oddsum = oddsum + i % 10;
i = i / 10;
evensum = evensum + i % 10;
i = i / 10;
}
total.add(oddsum);
}
}
int sum = 0;
for (int i : total) {
sum = sum + i;
}
System.out.println(sum);
}
}
}

ALTERNATIVE METHOD

import java.util.*;

public class HolidayFun {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter the size");


int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is an invalid size");
return;
}
System.out.println("Enter the numbers");
String input;
int sumEven = 0, sumOdd = 0;
for (int i = 0; i < n; i++) {
input = sc.next();
int arr[] = new int[input.length()];
for (int k = 0; k < input.length(); k++) {
arr[k] = input.charAt(k) - '0';
}
if (arr.length % 2 == 0) {
for (int j = 0; j < arr.length; j++) {
if (j % 2 != 0) {
sumEven = sumEven + arr[j];
}
}
} else if (arr.length % 2 != 0) {
for (int j = 0; j < arr.length; j++) {
if (j % 2 == 0) {
sumOdd = sumOdd + arr[j];
}
}
}
}
System.out.println(sumEven + sumOdd);
}
}
30. Global Realtors
import java.util.*;
public class GlobalRealtors {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the customer name");
String name=sc.nextLine();
System.out.println("Enter the land area");
double la=sc.nextInt();
if(la<400 || la>2200 )
{
System.out.println(la+" is out of range");
return;
}
else
{
int cost=0;
int discount=0;
if(la>=400 && la<=599)
{
cost=1700;
discount=5;
}
else if(la>=600 && la<=999)
{
cost=1950;
discount=5;
}
else if(la>=1000 && la<=1499)
{
cost=2100;
discount=4;
}
else if(la>=1500 && la<=2200)
{
cost=2300;
discount=3;
}
double tp=la*cost;
double result=tp-(tp*discount)/100;
System.out.println("Total payable amount after discount is "+result);
}
}
}

31. Words Finding

import java.util.*;
public class WordsFinding {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of words");
int n=sc.nextInt();
if(n<=0)
{
System.out.println(n+" is less than the desired limit");
return;
}
String a[]=new String[n];
String b[]=new String[n];
System.out.println("Enter the words in set1");
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
System.out.println("Enter the words in set2");
for(int i=0;i<n;i++)
b[i]=sc.nextLine();
int flag=0;
for(int i=0;i<n;i++)
{
int count=0;
for(int j=0;j<n;j++)
{
if(!a[i].equalsIgnoreCase(b[j]))
count++;
else
flag++;
}
if(count==n)
System.out.println(a[i]);
if(flag==n)
System.out.println(n+" words in a set are repeated");
}
}
}

ALTERNATIVE METHOD

import java.util.*;

public class WordsFinding {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of words");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is less then the desired limit");
return;
}
List<String> set1 = new ArrayList<>();
List<String> set2 = new ArrayList<>();
sc.nextLine();
System.out.println("Enter the words in set1");
for (int i = 0; i < n; i++) {
String a = sc.nextLine();
set1.add(a);
set2.add(a.toLowerCase());
}

List<String> set3 = new ArrayList<>();


List<String> set4 = new ArrayList<>();
System.out.println("Enter the words in set2");
for (int i = 0; i < n; i++) {
String b = sc.nextLine();
set3.add(b);
set4.add(b.toLowerCase());
}

int count = 0;
for (int i = 0; i < set1.size(); i++) {
if (!set4.contains(set2.get(i))) {
System.out.println(set1.get(i));
} else {
count++;
}
if (count == set1.size()) {
System.out.println(set1.size() + " words in a set are repeated");
}
}
}
}

32. Phone Charging Duration

import java.util.*;
public class PhoneChargingDuration {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("Enter Battery capacity");
float battery = sc.nextInt();
if (battery >= 1000 && battery <= 10000) {
System.out.println("Enter charging current value");
float current = sc.nextInt();
if (current >= 300 && current <= 2100) {
System.out.printf("%.2f Hours", (battery / current) * 1.2);
} else {
System.out.println("Invalid output current");
return;
}
} else {
System.out.println("Invalid battery capacity");
return;
}
}
}
33. Fit Secret Code

import java.util.*;

public class FitSecretCode {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the child");
String name = sc.next();
boolean flag = false;
if (name.length() >= 4) {
System.out.println("Enter the birthdate of the child:");
int bd = sc.nextInt();
if (bd >= 1 && bd <= 31) {
flag = true;
String code = name.substring(0, 4).toLowerCase() + "@" + bd;
System.out.println("Generated secret code is:\n" + code);
}
}
if (flag == false) {
System.out.println("Secret code cannot be generated");
return;
}
}
}

34. Public Distribution

import java.util.*;
import java.util.regex.Pattern;

public class PublicDistribution {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine();
if (!Pattern.matches("[OKSRW]+", a)) {
System.out.println("Invalid Input");
return;
}
if (a.length() < 2 || a.length() > 5) {
System.out.println("Invalid String");
return;
}
List<Character> arr = new ArrayList<>();
for (int i = 0; i < a.length(); i++) {
if (!(arr.contains(a.charAt(i)))) {
arr.add(a.charAt(i));
} else {
System.out.println("Invalid String");
return;
}
}
int sum = 0;
for (char i : arr) {
if (i == 'O')
sum = sum + 2 * 12;
else if (i == 'K')
sum = sum + 2 * 18;
else if (i == 'R')
sum = sum + 2 * 15;
else if (i == 'S')
sum = sum + 2 * 21;
else if (i == 'W')
sum = sum + 2 * 22;
}
System.out.println(sum);
}
}

35. Max and Min Product

import java.util.*;

public class MaxAndMinProduct {


public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
if (n < 3) {
System.out.println(n + " is too small");
return;
} else if (n > 20) {
System.out.println(n + " exceeds limit");
return;
} else {
int[] array = new int[n];
System.out.println("Enter the array elements");
for (int i = 0; i < n; i++)
array[i] = sc.nextInt();
Arrays.sort(array);
System.out.println(array[0] * array[n - 1]);
}
}
}

36. Dinner Together (similar to Lunch Together)

import java.util.*;
public class DinnerTogether {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day interval of Sam:");
int sam=sc.nextInt();
System.out.println("Enter the day interval of Riya:");
int riya=sc.nextInt();
if(riya>0 && sam>0)
{
int lcm=sam*riya/GCD(sam,riya);
System.out.println("Sam and Riya will have their dinner on day "+lcm);
}
else
{
System.out.println("Given Interval is not valid");
return;
}
}
public static int GCD(int sam, int riya) {
if(sam==0)
return riya;
else if(riya==0 || sam==riya)
return sam;
else if(sam>riya)
return GCD(sam-riya,riya);
return GCD(sam,riya-sam);
}
}
37. Order Identification

import java.util.*;
public class OrderIdentification {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
if (n >= 2 && n <= 10) {
int[] first = new int[n];
int[] second = new int[n];
System.out.println("Enter the elements");
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
first[i] = num;
second[i] = num;
}
Arrays.sort(first);
if (Arrays.equals(first, second) && first[0] < first[1]) {
for (int i = 0; i < n; i++) {
System.out.print(second[i] + " ");
}
System.out.println("are in ascending order");
} else {
for (int i = 0; i < n; i++) {
System.out.print(second[i] + " ");
}
System.out.println("are not in ascending order");
}
} else {
System.out.println(n + " is not a valid array size");
return;
}
}
}

ALTERNATIVE METHOD
import java.util.*;

public class OrderIdentification {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
if (n >= 2 && n <= 10) {
List<Integer> first = new ArrayList<>();
List<Integer> second = new ArrayList<>();
System.out.println("Enter the elements");
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
first.add(num);
second.add(num);
}
Collections.sort(first);
if (first.equals(second) && first.get(0) < first.get(1)) {
for (int i : second) {
System.out.print(i + " ");
}
System.out.println("are in ascending order");
} else {
for (int i : second) {
System.out.print(i + " ");
}
System.out.println("are not in ascending order");
}
} else {
System.out.println(n + " is not a valid array size");
return;
}
}
}
38. Product of case count

import java.util.regex.*;
import java.util.*;
public class ProductOfCaseCount {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String str = sc.next();
if (Pattern.matches("[A-Za-z]+", str)) {
int lower_count = 0;
int upper_count = 0;
if (str.length() > 10) {
System.out.println(str.length() + " is invalid String");
return;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLowerCase(ch)) {
lower_count++;
} else if (Character.isUpperCase(ch)) {
upper_count++;
} else {
continue;
}
}
int product = lower_count * upper_count;
System.out.println("Product value is " + product);
} else {
System.out.println(str + " is invalid");
return;
}
}
}

ALTERNATIVE METHOD

import java.util.regex.*;
import java.util.*;

public class ProductOfCaseCount {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String str = sc.next();
if (Pattern.matches("[A-Za-z]+", str)) {
int count = 0;
int count1 = 0;
if (str.length() > 10) {
System.out.println(str.length() + " is invalid String");
return;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
count++;
} else if (ch >= 'a' && ch <= 'z') {
count1++;
} else {
continue;
}
}
int product = count * count1;
System.out.println("Product value is " + product);
} else {
System.out.println(str + " is invalid");
return;
}
}
}

39. Security Key Generation

import java.util.regex.*;
import java.util.*;

public class SecurityKeyGeneration {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().toLowerCase();

if (str.length() < 2 || str.length() > 10) {


System.out.println("Invalid Input");
return;
}
if (Pattern.matches("[a-zA-Z]+", str)) {
int sum = 0;
for (int i = 0; i < str.length(); i++) {
sum = sum + (int) str.charAt(i);
}
int avg = sum / str.length();
char ag = (char) avg;
System.out.println(ag);
return;

} else {
System.out.println("Invalid Username");
return;
}
}
}
40. Sign Conversion

import java.util.*;

public class SignConversion {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is an invalid size");
return;
}
int[] arr = new int[n];
System.out.println("Enter the numbers");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if (arr[i] == 0) {
System.out.println(0 + " is an invalid element");
return;
}
}
int uniq[] = new int[n];
int nonuniq[] = new int[n];
int l = 0, k = 0, sum = 0;
for (int i = 0; i < n; i++) {
int temp = arr[i];
int count = 0;
for (int j = 0; j < n; j++) {
if (temp == arr[j])
count++;
}
if (count == 1) {
uniq[k] = temp;
sum = sum + uniq[k];
k++;
} else {
nonuniq[l] = -temp;
sum = sum + nonuniq[l];
l++;
}
}
System.out.println(sum);

}
}
41. Sort the first half and second half of an array

import java.util.*;
public class SortTheFirstAndSecondHalfOfAnArray {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of an array:");
int n=sc.nextInt();
if(n<=0)
{
System.out.println("Array size should be greater than 0");
return;
}
System.out.println("Enter the elements:");
int array[]=new int[n];
for(int i=0;i<n;i++)
array[i]=sc.nextInt();
if(n%2==0)
{
Arrays.sort(array,0,n/2);
for(int i=n/2;i<n-1;i++)
{
if(array[i]<array[i+1])
{
int temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
}
}
}
else
{
Arrays.sort(array,0,n/2+1);
for(int i=n/2+1;i<n-1;i++)
{
if(array[i]<array[i+1])
{
int temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
}
}
}
for(int i=0;i<n;i++)
System.out.println(array[i]);
}
}

42. Product Equals Sum

import java.util.Scanner;

public class ProductEqualSum {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
if (n < 1 || n > 10) {
System.out.println("Invalid array size");
return;
} else {
System.out.println("Enter the elements of the first array");
int a1[] = new int[n];
for (int i = 0; i < n; i++) {
a1[i] = sc.nextInt();
if (a1[i] < 10 || a1[i] > 999) {
System.out.println("Invalid Input");
return;
}
}
System.out.println("Enter the elements of the second array");
int a2[] = new int[n];
for (int i = 0; i < n; i++) {
a2[i] = sc.nextInt();
if (a2[i] < 10 || a2[i] > 999) {
System.out.println("Invalid Input");
return;
}
}

int count = 0;
for (int i = 0; i < n; i++) {
int product = 1, sum = 0;
int t1 = a1[i];
int t2 = a2[i];
while (t1 != 0) {
product = product * (t1 % 10);
t1 = t1 / 10;
}
while (t2 != 0) {
sum = sum + (t2 % 10);
t2 = t2 / 10;
}
if (product == sum) {
System.out.println(a1[i] + "," + a2[i]);
count++;
}
}
if (count == 0) {
System.out.println("No pair found");
}
}
}
}

ALTERNATIVE SOLUTION

import java.util.*;

public class ProductEqualsSum {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
if (n < 1 || n > 10) {
System.out.println("Invalid array size");
return;
} else {
int a[] = new int[n];
System.out.println("Enter the elements of the first array");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
if (a[i] < 10 || a[i] > 999) {
System.out.println("Invalid input");
return;
}
}
int b[] = new int[n];
System.out.println("Enter the elements of the second array");
for (int i = 0; i < n; i++) {
b[i] = sc.nextInt();
if (b[i] < 10 || b[i] > 999) {
System.out.println("Invalid input");
return;
}
}
int count = 0;
for (int i = 0; i < n; i++) {
int product = 1, sum = 0;
int x = a[i];
int y = b[i];
while (x != 0) {
product = product * (x % 10);
x /= 10;
}
while (y != 0) {
sum += y % 10;
y /= 10;
}
if (product == sum) {
System.out.println(a[i] + "," + b[i]);
count++;
}
}
if (count == 0) {
System.out.println("No pair found");
}
}
}
}

43. Three’s

import java.util.*;
import java.util.regex.*;

public class Three {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = " ";
String a = sc.nextLine();
s = s + a;
if (Pattern.matches("[a-zA-Z\\s]+", s)) {
int sum = 0;
for (int i = 1; i < s.length(); i++) {
if (i % 3 == 0) {
sum += (int) s.charAt(i);
}
}
System.out.println("Sum is " + sum);
} else {
System.out.println(a + " is not a valid string");
return;
}
}
}

44. Harshad Number

import java.util.*;
public class HarshadNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is an invalid limit");
} else {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
if (a[i] < 9) {

System.out.println("Provided" + a[i] + "is not valid");


return;
}
}
int b[] = new int[n];
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
b[i] = a[i];

while (a[i] != 0) {
sum = sum + a[i] % 10;
a[i] = a[i] / 10;
}
if (b[i] % sum == 0) {
System.out.println(b[i]);
} else {
count++;
}
sum = 0;
}
if (count == n) {
System.out.println("The " + n + "values are not harshad number");
}
}
}
}

ALTERNATIVE METHOD

import java.util.*;

public class HarshadNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of values");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is an invalid limit");
return;
}
System.out.println("Enter the numbers");
List<Integer> num = new ArrayList<>();
List<Integer> HarshadArray = new ArrayList<>();
for (int i = 0; i < n; i++) {
int value = sc.nextInt();
if (value <= 9) {
System.out.println("Provided " + value + " is not valid");
return;
}
num.add(value);
int sum = 0;
int temp = num.get(i);
while (temp != 0) {
sum = sum + temp % 10;
temp = temp / 10;
}
if (num.get(i) % sum == 0) {
HarshadArray.add(num.get(i));
}
}
if (HarshadArray.size() >= 1) {
for (int i : HarshadArray) {
System.out.println(i);
}
} else {
System.out.print("The " + n + " values are not harshad number");
}
}
}
45. Multiply with position

import java.util.Scanner;

public class MultiplyWithPosition {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array size");
int n = sc.nextInt();
if (n < 1 || n>10 ) {
System.out.println("Invalid size");
return;
}
int[] arr = new int[n];
System.out.println("Enter the elements");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
System.out.println((i + 1) * arr[i]);
}
}
}

46. Car Parking

import java.util.*;

public class CarPrking {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the car number");
String num = sc.nextLine();
if (num.length() != 10) {
System.out.println("Invlid State Code");
return;
}
String floor;
String code = num.substring(0, 2);
if (code.equals("TN")) {
floor = "ground";
} else if (code.equals("KA")) {
floor = "1st";
} else if (code.equals("KL")) {
floor = "2st";
} else if (code.equals("AP")) {
floor = "3st";
} else {
System.out.println("Invalid State Code");
return;
}
System.out.println("Park the car in " + floor + " floor");
}
}

47. Progress Grade Generator

import java.util.*;

public class ProgressGradeGenerator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of students");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is an invalid input");
return;
}
sc.nextLine();
String[] marks = new String[n * 2];
for (int i = 0; i < n * 2; i = i + 2) {
String s = sc.nextLine();
String[] arr = s.split(":");
int sum = 0;
for (int j = 1; j < 6; j++) {
sum = sum + Integer.parseInt(arr[j]);
}
marks[i] = arr[0];
marks[i + 1] = Integer.toString(sum);
}

for (int i = 0; i < marks.length; i = i + 2) {


int num = Integer.parseInt(marks[i + 1]);
if (num >= 400 && num <= 500) {
System.out.println(marks[i] + ":O");
}
if (num >= 300 && num <= 399) {
System.out.println(marks[i] + ":A");
}
if (num >= 250 && num <= 299) {
System.out.println(marks[i] + ":B");
}
if (num >= 200 && num <= 249) {
System.out.println(marks[i] + ":C");
}
if (num >= 0 && num <= 199) {
System.out.println(marks[i] + ":E");
}
}
}
}

48. OMR Evaluation

import java.util.*;
public class OMREvaluation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the No of questions");
int no = sc.nextInt();
if (no <= 0) {
System.out.println("Invalid Number");
return;
} else {
char anskey[] = new char[no];
System.out.println("Enter the answer key");
for (int i = 0; i < no; i++) {
char x = sc.next().charAt(0);
if (x >= 'A' && x <= 'E')
anskey[i] = x;
else {
System.out.println("Invalid Answers");
return;
}
}
char studanskey[] = new char[no];
System.out.println("Enter the student answers");
for (int i = 0; i < no; i++) {
char y = sc.next().charAt(0);
if (y >= 'A' && y <= 'E')
studanskey[i] = y;
else {
System.out.println("Invalid Answers");
return;
}
}
int count = 0;
for (int i = 0; i < no; i++) {
if (anskey[i] == studanskey[i])
count++;
}
if (count == 0)
System.out.println("All answers are wrong \nMark is 0");
else {
System.out.println("Correct answers are " + count);
System.out.println("Mark is " + ((count * 100) / no));
}
}
}
}

49. Numerology Name Checking

import java.util.*;
import java.util.regex.*;

public class NumerologyNameChecking {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name:");
String name = sc.nextLine().toLowerCase();
if (!Pattern.matches("[A-Za-z]+", name)) {
System.out.println(name + " is an Invalid name");
return;
}
int sum = 0;
for (int i = 0; i < name.length(); i++) {
sum = sum + ((int) name.charAt(i) - 96);
}
if (sum % 3 == 0 && sum % 2 == 0) {
System.out.println(sum);
System.out.println(name + " is a Numerology number");
} else {
System.out.println(sum);
System.out.println(name + " is not Numerology number");
}
}
}

50. Box Challenge

import java.util.*;
public class BoxChallenge {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int r;
System.out.println("The box contain");
int n = sc.nextInt();
if (n <= 0) {
System.out.println(n + " is not a valid input");
return;
}
int arr[] = new int[n];
System.out.println("Enter the numbers");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if (arr[i] <= 0) {
System.out.println(arr[i] + " is not a valid input");
return;
}
}
for (int i = 0; i < n; i++) {
r = arr[i] % 10;
sum = sum + r;
}
if (sum % 2 == 0) {
System.out.println(sum + " is even its valid box ");
} else {
System.out.println(sum + " is odd its invalid box");
}
}
}

51. Runner Code

import java.util.*;

public class RunnerCode {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of runners");
int n = sc.nextInt();
if (n > 10) {
System.out.println(n + " is an invalid number of runners");
return;
}
String input;
String name[] = new String[n]; // name array
Double time[] = new Double[n]; // another time array
System.out.println("Enter the details");
for (int i = 0; i < n; i++) {
input = sc.next();
String[] a = input.split(","); // a[0] stores name a[1] stores time
name[i] = a[0]; // string->string
time[i] = Double.p arseDouble(a[1]); // double<-string
if (time[i] < 8 || time[i] > 12) {
System.out.println(time[i] + " is an invalid input");
return;
}
}
boolean flag = true;
double min = time[0];
int index = 0;

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


if (time[i] < min) {
min = time[i];
index = i;
}
if (min != time[i]) {
flag = false;
}
}

if (flag)
System.out.println(n + " runners have same timing");
else {
for (int i = 0; i < n; i++) {
if (min == time[i]) {
System.out.println(name[i]);
}
}
}
}
}
52. Toll Plaza Fee Collection

import java.util.*;

public class TollPlazaFeeCollection {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the vehicle number");
String vno = sc.nextLine();
System.out.println("Enter the vehicle type");
String vtype = sc.next();
System.out.println("Enter the journey type");
String jtype = sc.next();
System.out.println("Enter the amount paid");
int amt = sc.nextInt();
int fare = 0, balance = 0;
if (vtype.contentEquals("LMV") && jtype.equals("Single")) {
fare = 50;
} else if (vtype.contentEquals("LMV") && jtype.equals("Return")) {
fare = 85;
} else if (vtype.contentEquals("LCV") && jtype.equals("Single")) {
fare = 60;
} else if (vtype.contentEquals("LCV") && jtype.equals("Return")) {
fare = 90;
} else if (vtype.contentEquals("HCM") && jtype.equals("Single")) {
fare = 150;
} else if (vtype.contentEquals("HCM") && jtype.equals("Return")) {
fare = 250;
} else if (vtype.contentEquals("TwoAxel") && jtype.equals("Single")) {
fare = 300;
} else if (vtype.contentEquals("TwoAxel") && jtype.equals("Return")) {
fare = 550;
} else if (vtype.contentEquals("ThreeAxel") && jtype.equals("Single")) {
fare = 500;
} else if (vtype.contentEquals("ThreeAxel") && jtype.equals("Return")) {
fare = 950;
}
balance = amt - fare;
if (balance < 0) {
System.out.println("Need more cash");
} else {
System.out.println("Balance " + balance);
System.out.println("Have a safe journey");
}
}
}
53. Digit Count

import java.util.*;

class DigitCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int num = sc.nextInt();
if (num < 0) {
System.out.println(num + " is an invalid number");
return;
}
System.out.println("Enter the digit");
int digit = sc.nextInt();
if (digit < 0 || digit > 9) {
System.out.println(digit + " is not in the range");
return;
}
int count = 0;
int value = num;
while (value != 0) {
int temp = value % 10;
value = value / 10;
if (temp == digit) {
count++;
}
}
if (count >= 0) {
System.out.println(digit + " appears in " + num + " is " + count);
}
}
}
ALTERNATIVE METHOD

import java.util.*;

class DigitCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
if (n < 0) {
System.out.println(n + " is an invalid number");
return;
} else {
System.out.println("Enter the digit");
int digit = sc.nextInt();
if (digit < 0 && digit > 9) {
System.out.println(digit + " is not in the range");
return;
}
String k = n + "";
String s[] = k.split("");
String d = digit + "";
int count = 0;
for (int i = 0; i < s.length; i++) {
if (d.equals(s[i]))
count++;
}
System.out.println(digit + " appears in " + n + " is " + count);
}
}
}

54. Offer to customer

import java.util.*;
public class offerToCustomer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of customers:");
int cust = sc.nextInt();
if (cust < 0) {
System.out.println("Invalid Input");
return;
}
if (cust < 3) {
System.out.println("Customer count is too low");
return;
}
if (cust > 50) {
System.out.println("Customer count is too high");
return;
}
System.out.println("Enter the customer details");
String arr[] = new String[cust];
for (int i = 0; i < cust; i++) {
arr[i] = sc.next();
}
System.out.println("Enter the plan to be searched");
String plan = sc.next();
for (int i = 0; i < cust; i++) {
String custdetails[] = arr[i].split(":");
if (plan.equals(custdetails[1])) {
System.out.println(custdetails[3] + " " + custdetails[2]);
}
}
}
}

55. Gross Number

import java.util.*;

public class GrossNumber {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of integers");
int n = sc.nextInt();
if (n <= 0) {
System.out.println("The number of integers " + n + " is invalid");
return;
}
System.out.println("Enter the integer");
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
if (a[i] < 1 || a[i] > 100) {
System.out.println(a[i] + " is not in range");
return;
}
}
int[] arr = new int[n];
for (int j = 0; j < a.length; j++) {
int count = 0;
for (int k = 1; k <= 100; k++) {
if (k % 2 == 0 || k % 3 == 0 || k % 5 == 0) {
count = count + 1;
if (a[j] == k) {
arr[j] = count;
break;
}
} else {
if (a[j] == k) {
arr[j] = 0;
break;
}
}
}
}
for(int i:arr) {
System.out.println(i);
}
}
}

ALTERNATIVE METHOD

import java.util.*;

public class GrossNmumber {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int gross[] = { 0, 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32,
33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 50, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66,
68, 69, 70, 72, 74, 75, 76, 78, 80, 82, 84, 85, 86, 87, 88, 90, 92, 93, 94, 95, 96, 98, 99, 100 };

System.out.println("Enter the number of integers");


int n = sc.nextInt();
if (n <= 0) {
System.out.println("The number of integers " + n + " is invalid");
return;
}
System.out.println("Enter the integer");
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
if (a[i] < 1 || a[i] > 100) {
System.out.println(a[i] + " is not in range");
return;
}
int flag = 0;
for (int j = 1; j < gross.length; j++) {
if (a[i] == gross[j]) {
System.out.println(j);
flag = 1;
break;
} else {
continue;
}
}
if (flag == 0) {
System.out.println("0");
}
flag = 0;
}

}
}

56. Video Game Player

import java.util.Scanner;
public class VideoGamePlayer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Amount");
int amount = sc.nextInt();
if (amount < 3000) {
System.out.println(amount + " is to less");
return;
}
sc.nextLine();
System.out.println("Enter the Video Game Player Type");
String type = sc.nextLine();

if (!(type.equals("PS1") || type.equals("PS2") || type.equals("PS3") ||


type.equals("PS4")
|| type.equals("PS5"))) {
System.out.println(type + " is invalid Type");
}
if (type.equals("PS1")) {
if (amount >= 5000)
System.out.println("You can buy PS1");
else
System.out.println("You need more amount to buy PS1");
} else if (type.equals("PS2")) {
if (amount >= 7800)
System.out.println("You can buy PS2");
else
System.out.println("You need more amount to buy PS2");
} else if (type.equals("PS3")) {
if (amount >= 9500)
System.out.println("You can buy PS3");
else
System.out.println("You need more amount to buy PS3");
} else if (type.equals("PS4")) {
if (amount >= 12000) {
System.out.println("You can buy PS4");
} else {
System.out.println("You need more amount to buy PS4");
}
} else if (type.equals("PS5")) {
if (amount >= 15000) {
System.out.println("You can buy PS5");
} else {
System.out.println("You need more amount to buy PS5");
}
}
}
}

57. Character Addition

import java.util.*;

public class CharacterAddition {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of sentences");
int n = sc.nextInt();
if (n <= 0) {
System.out.println("The number of sentences " + n + " is invalid");
return;
}
System.out.println("Enter the sentences:");
String str[] = new String[n];
for (int i = 0; i < n; i++) {
str[i] = sc.next();
}
for (int i = 0; i < str.length; i++) {
int sum = 0;
String s = str[i].replaceAll("[0-9]", "");
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
int ascii = c;
sum += ascii;
}
System.out.println(sum);
}
}
}

58. Reverse First Half

import java.util.*;
import java.util.regex.Pattern;
public class ReverseFirstHalf {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.next();
if (!Pattern.matches("[A-Za-z]+", s)) {
System.out.println(s + " is not a valid string");
return;
}
if (s.length() < 2) {
System.out.println("Size of string " + s + " is too small");
return;
}
if (s.length() % 2 == 0) {
StringBuffer sb = new StringBuffer(s);
System.out.println(sb.reverse());
} else {
StringBuffer sb = new StringBuffer(s.substring(0, s.length() / 2));
System.out.println(sb.reverse() + s.substring(s.length() / 2, s.length()));
}
}
}

59. Cake Shop Order

import java.util.*;

public class CakeShopOrder {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double d = 0;
String s1;
System.out.println("Enter the customer instruction:");
String s = sc.next();
if (s.matches("[A-Za-z0-9]*")) {
String q = s.substring(3, 4);
if (!(q.matches("[A-Za-z]*"))) {
s1 = s.substring(0, 4);
} else {
s1 = s.substring(0, 3);
}
double w = Double.parseDouble(s1);

if (w < 999) {
System.out.println("Invalid Weight");
return;
} else {
w = (w / 1000);
w = Math.round(w * 100.0) / 100.0;
}

String a = s.substring(4, s.length() - 3);

String c = s.substring(s.length() - 3, s.length() - 2);


if (c.matches("[A-Za-z]*")) {
System.out.println("Invalid Order");
return;
} else {
String s2 = s.substring(s.length() - 3);
int o = Integer.parseInt(s2);
if (o < 99) {
System.out.println("Invalid Order");
return;
}

d = 450 * w;
System.out.format("Weight of Cake in Kgs: %.2fKg", w);
System.out.println(" ");
System.out.println("Order No is: " + o);
System.out.println("Flavor name: " + a);
System.out.printf("Cake Price is: %.2f Rupees", d);
}
} else {
System.out.println("Invalid Instruction");
}
}
}
60. ODD POSITION CHAR WILL BE IN UPPERCASE & EVEN
WILL BE LOWERCASE-
INPUT: school
OUTPUT : sChOoL

import java.util.Scanner;

public class odd_char_uppercase {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.next();
for(int i=0;i<s.length();i++)
{
if(!Character.isLetter(s.charAt(i)))
{
System.out.println("Invalid String");
return;
}
}
String st="";
for(int i=0;i<s.length();i++)
{
if(i%2==0)
{
st=st+String.valueOf(s.charAt(i)).toLowerCase();
}
if(i%2!=0)
{
st=st+String.valueOf(s.charAt(i)).toUpperCase();
}
}
System.out.println(st);

}
61. Count of Uppercase and Lowercase and their difference

Enter the String: ColLEgE


Count of uppercase is: 4
Count of lowercase is : 3
Hence the answer is: 1

import java.util.Scanner;

public class Differeence_Uppercase_Count {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string:");
String s=sc.next();
int upper=0,lower=0;

if(!s.matches("^[A-Za-z]*$"))
{
System.out.println(s +" is an invalid string");
return;
}
else if(s.length()>10)
{
System.out.println(s.length() +" exceeds the limit");
return;

}
else
{
for(int i=0;i<s.length();i++)
{
if(Character.isUpperCase(s.charAt(i)))
{
upper++;
}
else
{
lower++;
}
}
System.out.println("Difference value is "+Math.abs(upper-lower));
}

62. Lottery Tickets

INPUT: Enter the Starting range 23467


Enter the Ending range 23477
OUTPUT: 23469
23472

import java.util.*;

public class Lottery_Tickets {


public static void main(String args[]) {
int i, c, n, sum, count;
sum = 0;
count = 0;
n = 0;
c = 0;
i = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Starting range");
int a = sc.nextInt();
String str = Integer.toString(a);
if (str.length() != 5) {
System.out.println(str + " is an invalid input");
System.exit(0);
}

System.out.println("Enter the Ending range");


int b = sc.nextInt();
String str1 = Integer.toString(b);
if (str1.length() != 5) {
System.out.println(str1 + "is an invalid input");
System.exit(0);
}

if (a > b) {
System.out.println(a + " and " + b + " are invalid serial numbers");
System.exit(0);

}
for (i = a; i <= b; i++) {
n = i;
while (n != 0) {
c = n % 10;
sum = sum + c;
n = n / 10;
}
if ((sum % 3 == 0) && ((sum / 3) % 2 == 0)) {
System.out.println(i + " ");
count++;
}
sum = 0;
}
if (count == 0) {
System.out.println("Eligible tickets are not available from " + a + " to " +
b);
return;
}
}
}

63. COFFEE STALL NUMEROLOGY

INPUT: Enter the Staff Name


Coffee Bar
OUTPUT: Coffee Bar satisfies the numerology logic
Here the sum of characters should be divisible by 2 then it is numerology
number

import java.util.*;
import java.util.regex.*;

public class Coffee_Numerology {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the Staff Name");
String name = sc.nextLine();
if (Pattern.matches("[a-zA-Z ]+", name)) {
String str = name.replace(" ", "");
int sum = 0;
for (int i = 0; i < str.length(); i++) {
sum = sum + i;
}
if (sum % 2 == 0) {
System.out.println(name + " satisfies the numerology logic");
} else {
System.out.println(name + " does not satisfy the numerology
logic");
}
} else {
System.out.println("Invalid Input");
}
}

64. Extract the Date, Month, Year

INPUT:13081995
OUTPUT: date: 13
month: 08
year: 1998

import java.util.*;
import java.util.regex.*;
public class date_month {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
if (!(Pattern.matches("[0-9]+", str))) {
System.out.println("Invalid Input");
return;
}
if (str.length() != 8) {
System.out.println("Enter valid date");
return;
}
String date = str.substring(0, 2);
String month = str.substring(2, 4);
String year = str.substring(4, 8);
System.out.println("date:" + date);
System.out.println("Month:" + month);
System.out.println("Year :" + year);

}
}

65. RAINFALL

INPUT: Enter the length of the roof in meters: 4


Enter the breadth of the roof in meters: 3
Enter the rainfall level: 3
OUTPUT: 360.00 Litres

formula is( length*breadth)*(rainfall_level*10);


import java.util.Scanner;

public class Rainfall {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the roof in meters");
double l = sc.nextDouble();
if (l <= 0) {
System.out.println("Invalid Length");
return;
}
System.out.println("Enter the breadth of the roof in meters");
double b = sc.nextDouble();
if (b <= 0) {
System.out.println("Invalid breadth");
return;
}
System.out.println("Enter the rainfall level");
double r = sc.nextDouble();
if (r <= 0) {
System.out.println("Invalid rainfall");
return;
}
double h;
h = (l * b) * (r * 10);
System.out.println(String.format("%.2f", h) + " Litres");
}
}

66. Fun Count/Toy Count


Input array of string
if there is 5 input
barbie
barbie
doll
doll
bike

output
barbie=2
doll=2
bike=1

import java.util.*;

public class Fun_Count {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of toyss");
int n = sc.nextInt();
System.out.println("Enter the toy set");

List<String> list = new ArrayList<String>();


for (int i = 0; i < n; i++) {
list.add(sc.next());
}

Set<String> unique = new HashSet<String>(list);


for (String key : unique) {
System.out.println(key + ": " + Collections.frequency(list, key));
}
}
}

Alternative Method

public class funn {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the no of toys");


int n=sc.nextInt();
if(n<=0)
{
System.out.println("Invalid Input");
}
if(n>15)
{
System.out.println();
}
System.out.println("Enter the toy list");
String []a=new String[n];
for(int i=0;i<n;i++)
{
a[i]=sc.next().toLowerCase();

}
Arrays.sort(a);
int count=0;
for(int i=0;i<n;i++)
{
count=1;
for(int j=i+1;j<n;j++)
{
if(a[i].equalsIgnoreCase(a[j]))
{
count++;
a[j]="0";
}
}
if(a[i]!="0")
{
System.out.println(a[i] +"=" +count);
}
}
}
}

67.Substring Count /Count of occurrences of substring

import java.util.*;
import java.util.regex.*;

public class NoOfSubstringFromString {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String s = sc.nextLine().toLowerCase();
System.out.println("Enter the substring");
String sub = sc.next().toLowerCase();
Pattern p = Pattern.compile(sub);
Matcher m = p.matcher(s);
int count = 0;
while (m.find())
count += 1;
System.out.println(count);
}
}

Alternative Method
import java.util.*;
import java.util.regex.Pattern;
public class Substring_count {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter string");
String s1=sc.next();
if(!Pattern.matches("[A-za-z]+", s1))
{
System.out.println("Invalid string ");
return;
}
s1=s1.toLowerCase();
System.out.println("Enter substring");
String s2=sc.next();
if(!Pattern.matches("[A-za-z]+", s2))
{
System.out.println("Invalid sub string ");
return;
}
int index=0;
int count=0;
while((index=s1.indexOf(s2,index))!=-1)
{
index=index+1;
count=count+1;
}
System.out.println(count);

68. Spy Number


import java.util.Scanner;

public class spy_number_abhiram {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the set size");
int t = s.nextInt();
if (t <= 0) {
System.out.println(t + " is an invalid size");
} else {
System.out.println("Enter the numbers");
int[] a = new int[t];
for (int i = 0; i < t; i++) {
a[i] = s.nextInt();
if (a[i] <= 0) {
System.out.println("Invalid number");
return;
}
}
int b = 0, c = 1, count = 0, j = 0;
int[] e = new int[t];
for (int i = 0; i < t; i++) {
int d = a[i];
while (a[i] != 0) {
b = b + a[i] % 10;
c = c * (a[i] % 10);
a[i] = a[i] / 10;
}
System.out.println((c));
if (b == c) {
e[j] = d;
count++;
j++;
}
b = 0;
c = 1;
}
if (count == 0) {
System.out.println("The " + t + " numbers are not spy numbers");
} else {
System.out.println("The spy numbers are");
for (int i = 0; i < count; i++) {
System.out.println(e[i]);
}
}
}
}
}

ALTERNATIVE METHOD

import java.util.*;

public class Spy_number {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the set size");
int n = sc.nextInt();
if (n < 0) {
System.out.println(n + "is an invalid size");
}
System.out.println("Enter the numbers");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int b = 0;
int digit = 0;
int sum;
int c = 0;
int product;
int count = 0;
int j = 0;
int arr1[] = new int[n];
for (int i = 0; i < n; i++) {
sum = 0;
product = 1;
b = arr[i];
c = arr[i];
while (arr[i] != 0) {
digit = arr[i] % 10;
sum = sum + digit;
arr[i] = arr[i] / 10;
}
while (b != 0) {
digit = b % 10;
product = product * digit;
b = b / 10;
}
if (sum == product) {

arr1[j] = c;
count++;
j++;
}
}
if (count == 0)
System.out.println("The " + n + " numbers are not spy numbers");
else {
System.out.println("The spy numbers are");
for (int i = 0; i < count; i++) {
System.out.println(arr1[i]);
}
}
}
}

69. MOVE X

import java.util.Scanner;

public class move_x {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the sentence");
String t = s.nextLine();
char[] c = t.toCharArray();
for (int i = 0; i < t.length(); i++) {
if (c[i] == ' ') {
continue;
}
if (!Character.isLetter(c[i])) {
System.out.println(t + " is an Invalid input");
return;
}
}
String a = "", b = "";
int count = 0;
for (int i = 0; i < t.length(); i++) {
if (c[i] == 'x' || c[i] == 'X') {
b = b + c[i];
count++;
} else {
a = a + c[i];
}
}
if (count == 0) {
System.out.println("Character does not exist in " + t);
return;
}
System.out.println(a + b);
}
}

70. Ranbir’s Offer


import java.util.*;
public class Ranbir_Offer {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the customer name");
String name=sc.next();
System.out.println("Enter the bill amount");
int bill_amt=sc.nextInt();
if(bill_amt<=0)
{
System.out.println("Provide valid amount");
return;
}
double total_bill=0;
if(bill_amt<1000)
{
total_bill=bill_amt;

}
else if(bill_amt>=1000 && bill_amt<=1500)
{
total_bill=bill_amt-(bill_amt*0.02);
}
else if(bill_amt>=1501 && bill_amt<=2500)
{
total_bill=bill_amt-(bill_amt*0.03);
}
else if(bill_amt>=2501 && bill_amt<=5000)
{
total_bill=bill_amt-(bill_amt*0.05);
}
else if(bill_amt>5000)
{
total_bill=bill_amt-(bill_amt*0.07);
}

System.out.println("Total Bill:" + String.format("%.2f",total_bill));

}
}

71.Create Acronym (Asked in Retest)

import java.util.*;
import java.util.regex.*;
public class Create_Acronyn {

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);

System.out.println("Enter a sentence ");


String s=sc.nextLine();
String str[]=s.split(" ");
if(str.length<2)
{
System.out.println(s + " is too short");
return;
}

for(int i=0;i<str.length;i++)
{
System.out.print(str[i].substring(0,1).toUpperCase());

}
System.out.print(" stands for ");
for(int i=0;i<str.length;i++)
{

System.out.print(str[i].substring(0,1).toUpperCase()+str[i].substring(1) + " ") ;

}
}
72.Count of Teenager (Asked in Retest)

import java.util.*;

import java.util.regex.*;

import java.math.*;

public class Count_of_Teenagers {

public static void main(String[] args)

Scanner sc =new Scanner(System.in);

System.out.println("Enter family members count");

int n=sc.nextInt();

System.out.println("Enter the age of "+ n + " members ");

int arr[]=new int[n];

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

arr[i]=sc.nextInt();

int count=0;

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

if(arr[i]>=13 && arr[i]<=19)

count++;
}

if(count>0)

System.out.println("Teenager count is " + count);

else

System.out.println("Teenager count is " + count);

73.Roshan Steels

import java.util.*;

public class Roshan_Steels {

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of customers");

int n=sc.nextInt();
if(n<=0)

System.out.println("Provide valid number of customers");

return;

double quan[]=new double[n*2];

System.out.println("Enter the quantity and price");

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

quan[i]=sc.nextDouble();

if(quan[i]<=0)

System.out.println("Provide valid quantity");

return;

quan[i+1]=sc.nextDouble();

if(quan[i+1]<=0)

System.out.println("Provide valid price");

return;

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

double kgs=quan[i]*1000;

double bill=kgs*quan[i+1];

System.out.println(String.format("%.2f",bill));
}

74.Reed Me (Question pics not available)


INPUT : Enter the customer name Abi
Enter the category History
Enter the quantity of books ordered 2900
OUTPUT : Total cost is 334080.0

import java.util.*;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the customer name");
String name = sc.nextLine();
System.out.println("Enter the category");
String category = sc.nextLine();
category = category.toLowerCase();
if(!category.equals("adventure") && !category.equals("comics") &&
!category.equals("history") && !category.equals("thriller"))
{
System.out.println(category+" is invalid category");
return;
}
System.out.println("Enter the quantity of books ordered");
int quantity = sc.nextInt();

if(quantity <= 0){


System.out.println(quantity+" is an invalid quantity");
return;
}
int discount = 0;
int price = 0;
if(category.equals("adventure"))
{
price = 150;
if(quantity>=1700)
discount = 5;
}
else if(category.equals("comics"))
{
price = 230;
if(quantity>=1950)
discount = 5;
}
else if(category.equals("history"))
{
price = 120;
if(quantity>=2600)
discount = 4;
}
else if(category.equals("thriller") && quantity>=1700)
{
price = 190;
if(quantity>=6300)
discount = 3;
}

float totalcost = (price*quantity) - (price*quantity*discount/100);


System.out.println("Total cost is "+totalcost);
}
}

75. Online Shopping (Question pics not available)

input: enter the product


laptop
Actual price

45000
exchange?
yes
bill amount:
27000.00

import java.util.*;

public class arraysquare {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Product:");

String prod=sc.nextLine();

int ed=0,pd=0;

if(prod.equals("Mobile"))

ed=15;

pd=15;

}
else if(prod.equals("Laptop"))

ed=20;

pd=20;

else if(prod.equals("Headset"))

ed=0;

pd=10;

else if(prod.equals("Charger"))

ed=0;

pd=5;

else{

System.out.println("Not available");

return;

System.out.println("Enter the actual Price: ");

int price=sc.nextInt();

if(price<100)

System.out.println("Invalid Price");

return;

System.out.println("Do u want to Exchange?");

String exc=sc.next();

if(exc.equals("Yes") || exc.equals("yes"))

double ap=price-(price*pd)/100;

double ev=(price*ed)/100;
double total=ap-ev;

System.out.printf("Total=%.2f",total);

else if(exc.equals("No") || exc.equals("no"))

double total=price-(price*pd)/100;

System.out.printf("Total=%.2f",total);

else

System.out.println("Invalid Option");

You might also like