Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
91 views

Code

1. The document contains code snippets for 7 different Java programs that deal with topics like calculating LCM, word repetition score, battery charging time, odd numbered students, billing amount calculation, uppercase/lowercase string conversion, and rating scores. 2. It provides the input and output for each program, as well as the full Java code to solve each problem. 3. The code examples demonstrate different techniques in Java like using arrays, strings, loops, conditional statements, math functions, and taking user input to solve various problems.

Uploaded by

Vijji Vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Code

1. The document contains code snippets for 7 different Java programs that deal with topics like calculating LCM, word repetition score, battery charging time, odd numbered students, billing amount calculation, uppercase/lowercase string conversion, and rating scores. 2. It provides the input and output for each program, as well as the full Java code to solve each problem. 3. The code examples demonstrate different techniques in Java like using arrays, strings, loops, conditional statements, math functions, and taking user input to solve various problems.

Uploaded by

Vijji Vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

JAVA

1. LCM OF DAY INTERVAL/LUNCH TOGETHER/DINNER TOGETHER

INPUT: Enter the day interval of Tony : 4


Enter the day interval of Potts: 6

OUTPUT: Tony and Potts will have dinner together on 12th day.

import java.util.Scanner;
public class lcm {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the day interval of Sam:");
int n1 = sc.nextInt();
System.out.println("Enter the day interval of Riya:");
int n2 = sc.nextInt();
if(n1<=0 || n2<=0)
{
System.out.println("Given interval is not valid");
return;
}
int lcm;
lcm = (n1 > n2) ? n1 : n2;
while(true) {
if( lcm % n1 == 0 && lcm % n2 == 0 ) {
System.out.printf("Sam and Riya will have their dinner on day "+lcm);
break;
}
++lcm;
}
}
}

ALTERNATE METHOD

import java.util.Scanner;
public class Lunchlcm {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day interval of Tony");
int n1=sc.nextInt();
System.out.println("Enter the day interval of Potts");
int n2=sc.nextInt();
if(n1<=0 && n2<=0)
{
System.out.println("Given interval is not valid");
return;
}
int a=n1;
int b=n2;
while(n2>0)
{
if (n1 > n2)
{
n1 = n1 - n2;
} else
{
n2 = n2 - n1;
}
}
int gcd = n1;
int lcm = (a * b) / gcd;

System.out.println("Tony and Potts will have lunch together on " + lcm + " day");
}
}

*********************************************************************************************
*********************************************************************************************
**
2.SCORE OF REPEATING WORDS/ WORD REPETITION

INPUT: Enter the String: GOOD


Enter the sentence: GOOD FOOD GOOD LIFE

OUTPUT: Score is 2

import java.util.*;
class PrintMessage
{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.next();
int count=0;
int sum=2;
for(int i=0;i<s.length();i++)
{
if(Character.isDigit(s.charAt(i)) || Character.isLetter(s.charAt(i)) || s.charAt(i)==' ')
{
continue;
}
else
{
System.out.println(s+" is not valid String");
return;
}
}
System.out.println("Enter the sentence");
sc.nextLine();
String sentence=sc.nextLine();
String[] w=sentence.split(" ");
for(int i=0;i<w.length;i++)
{
if(w[i].equalsIgnoreCase(s))
{
count++;
if(count>2)
{
sum=sum*2;
}
}
}
if(count>2)
{
System.out.println("Score is "+sum);
}
else
{
System.out.println("Score is"+count);
}
}
}

*ALTERNATE METHOD

import java.util.Scanner;
public class CountWords {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String word, sentence;ed
int count = 0;
System.out.println("Enter a word:");
word = in.nextLine();
System.out.println("Enter a sentence");
sentence = in.nextLine();

if (!(word.matches("[a-z]+"))) {
System.out.println("Invalid Input");
return;
}
String words[] = sentence.toLowerCase().split(" ");
for (int i = 0; i < words.length; i++) {
if (word.equals(words[i])) {
count++;
}
}
System.out.println("Score is:" + ((int) Math.pow(2, count - 1)));
}
}
*********************************************************************************************
*********************************************************************************************
***

3. BATTERY CAPACITY

INPUT: Enter battery capacity :1230


Enter charging current value: 400

OUTPUT: 3.69 Hours

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter battery capacity:");
double capacity = sc.nextDouble();
if(!(capacity>=1000 && capacity<=10000)) {
System.out.println("Invalid battery capacity");
return;
}
System.out.println("Enter charging current value:");
double current = sc.nextDouble();
if(!(current>=300 && current<=2100)) {
System.out.println("Invalid output current");
return;
}
double time=0.0f;
time = (capacity/current)*1.2;
System.out.println(String.format("%.2f",time)+" Hours");
}

*********************************************************************************************
*********************************************************************************************
***
4.STUDENTS WHOSE ROLLNO ARE ODD

INPUT: Entr the set of students :5


Enter the roll number :1
3
4
5
6

OUTPUT: 135

import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println("Enter the set of students");
Scanner s=new Scanner(System.in);
int setn=s.nextInt();
if(setn<=0)
{
System.out.println(setn+" is an invalid size");
return;
}
System.out.println("Enter the roll number");
int[] rolls=new int[setn];
int oddflag=0;
for(int i=0;i<setn;i++)
{
rolls[i]=s.nextInt();
if(rolls[i]<0)
{
System.out.println(rolls[i]+" is an invalid roll number");
return;
}
if(rolls[i]%2!=0)
{
oddflag=1;
}
}
if(oddflag==0)
{
System.out.println("The "+setn+" numbers are not odd");
return;
}
String str="";
for(int i=0;i<setn;i++)
{
if(rolls[i]%2!=0)
{
str=str+rolls[i];
}
}
for(int n=0;n<str.length();n++)
{
System.out.println(str.charAt(n)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**

5.CALCULATING BILL AMOUNT

INPUT : Enter your name : Sam


Enter the time duration: 13
List of payment options
1)Visa card
2)Rupay card
3)Master card
Choose an option : 2

OUTPUT: Dear Sam your bill amount is 1294.80

import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String s=sc.nextLine();double sal=0;double disc=0;
for(int i=0;i<s.length();++i)
{
if(!Character.isLetter(s.charAt(i)))
{
System.out.println("Invalid Name");
return;
}
}
System.out.println("Enter the time duration");
int n=sc.nextInt();int n1;
if(n<=0 || n>24)
{
System.out.println("Invalid duration");
return;
}
System.out.println("List of payment options");
System.out.println("1)Visa card");
System.out.println("2)Rupay card");
System.out.println("3)Master card");
System.out.println("Chosse an option");
while(true)
{
n1=sc.nextInt();
if(n1<0||n1>3)
{
System.out.println("Try again");
}
else
{
break;
}
}
if( n1==1)
{
if(n>=5)
{
sal=120*n;
disc=(sal*0.25);
sal=sal-disc;
}
else
{
sal=n*120;
}
}
if(n1==2)
{
if(n>=5)
{
sal=120*n;
disc=(sal*0.17);
sal=(120*n)-disc;
}
else
{
sal=n*120;
}
}
if(n1==3)
{
sal=n*120;
}
System.out.print("Dear "+s+" your bill amount is ");
System.out.printf("%.2f", sal);
}
}

*********************************************************************************************
*********************************************************************************************
***
6.ODD POSITION CHAR WILL BE IN UPPERCASE & EVEN WILL BE LOWERCASE-

INPUT : school

OUTPUT : sChOol

import java.util.Scanner;
public class UPLC{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
String str = sc.next();
int strlen = str.length();
for(int i = 0 ; i < str.length(); i++){
char ch = str.charAt(i);
if(!(ch >= 'a' && ch <='z' || ch >= 'A' && ch <='Z' )){
System.out.println(str+ " is an invalid input");
return;
}
}

if(strlen >= 5 && strlen <= 20){


StringBuffer updateString = new StringBuffer();
char[] charArr = str.toCharArray();
for(int i = 0 ; i < charArr.length; i++){
char ch = charArr[i];
if(i % 2 != 0){
ch = Character.toUpperCase(ch);
}
updateString.append(ch);
}
System.out.println(updateString.toString());
}
else {
System.out.println(str+ " is an invalid Length");
}
}
}

*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE

INPUT: ENTER JESSON SCORE


1
2
3
4
8
ENTER JAMES SCORE
0
2
6
5
6

OUTPUT: JESSON SCORE : 2


JAMES SCORE : 2

import java.util.Scanner;
public class Ratingscore {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Jesson Score");
int[] a1 = new int[5];
int[] a2 = new int[5];
int f = 0;
int t = 0;
int sum = 0;
int sum1 = 0;
for (int i = 0; i < 5; i++) {
a1[i] = sc.nextInt();
if (a1[i] < 0) {
f = 1;
t = a1[i];
break;
}
}
if (f == 1) {
System.out.println(t + " is invalid");
} else {
System.out.println("Enter James Score");
for (int j = 0; j < 5; j++) {
a2[j] = sc.nextInt();
if (a2[j] < 0) {
f = 1;
t = a2[j];
break;
}
}
if (f == 1) {
System.out.println(t + " is invalid");
} else {
for (int k = 0; k < 5; k++) {
if (a1[k] > a2[k]) {
sum++;
}
if (a1[k] < a2[k]) {
sum1++;
}
}
System.out.println("Jesson Score");
System.out.println(sum);
System.out.println("James Score");
System.out.println(sum1);
}
}
}
}

**ALTERNATE METHOD:

import java.util.Scanner;
public class minmax {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int jesson=0,james=0,c=0;
int a[]=new int[5];
int b[]=new int[5];
System.out.println("Enter Jesson Score");
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
if(a[i]<0)
{
System.out.println("Invalid");
Runtime.getRuntime().halt(0);
}
}
System.out.println("Enter James Score");
for(int i=0;i<b.length;i++)
{
b[i]=sc.nextInt();
if(b[i]<0)
{
System.out.println("Invalid");
Runtime.getRuntime().halt(0);
}
}
if(a.length==b.length)
{
for(int i=0;i<a.length;i++)
{
if(a[i]>b[i])
{
jesson++;
}
else if(a[i]<b[i])
{
james++;
}
else if(a[i]==b[i])
{
c++;
}
}
System.out.println("Jesson Score: "+jesson);
System.out.println("James Score: "+james);
}
else{
System.out.println("Length not same");
}
}
}

*********************************************************************************************
*******************************************************************************

8.COUNT OF UPPERCASE AND COUNT OF LOWERCASE & OUTPUT WILL BE IN THE FORM OF (UP-LC
)

INPUT : HosTEL

OUTPUT: 4-2=2

import java.util.Scanner;

public class ContNext {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
int uc=0,lc=0;
if(str.matches("[A-Za-z ]+")&&str.length()<10) {
for(int i=0;i<str.length();i++)
if(Character.isUpperCase(str.charAt(i))) {
uc++;
}
else if(Character.isLowerCase(str.charAt(i))){
lc++;
}
System.out.println(uc-lc);
}
}
}
*********************************************************************************************
*******************************************************************************
9) PRODUCT of COUNT of UPPERcase and LOWERcase letters

INPUT: ABsdEr

OUTPUT: 6 (3*3)

import java.util.Scanner;
public class minmax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String");
String s = sc.nextLine();
int upper = 0;
int lower = 0;
int i;
if (s.matches("[a-zA-Z]+")) {
for (i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'A' && c <= 'Z') {
upper++;
}
if (c >= 'a' && c <= 'z') {
lower++;
}
}
System.out.println(upper * lower);
}
else
{
System.out.println("Invalid String");
}

}
}

*********************************************************************************************
******************************************************************************
10.Count of UPPERcase and LOWERcase and their difference

Enter the String: ColLEgE


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

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String ");
String str = sc.next();
int strlen = str.length();

for(int i =0; i < str.length() ; i++){ char


ch = str.charAt(i);
if(!(ch >='a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')){
System.out.println(str + " is an invalid String");
return;
}
}

if(strlen > 0 && strlen <= 10){


int upper = 0, lower = 0, ans = 0;

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


{
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z')
upper++;
else if(ch >= 'a' && ch <= 'z')
lower++;
}
ans = upper - lower;
System.out.println("count of uppercase is :"+ upper);
System.out.println("count of lowercase is :"+ lower);
System.out.println("Hence the ans is :"+ ans);
}
else {
System.out.println(str + " is an invalid String");
}
}
}
*********************************************************************************************
*******************************************************************************
11.PUBLIC DISTRIBUTION

INPUT : OKR

OUTPUT : 90

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int l=str.length();
if(l>5 || l<2)
{
System.out.println("Invalid Input");
}
else
{
int sum=0,flag=0;
for(int i=0;i<l;i++)
{
char c=str.charAt(i);
if(c=='O')
{
sum=sum+24;
}
else if(c=='K')
{
sum=sum+36;
}
else if(c=='S')
{
sum=sum+42;
}
else if(c=='R')
{
sum=sum+30;
}
else if(c=='W')
{
sum=sum+44;
}
else
{
flag=1;
}
}
if(flag==1)
{
System.out.println("Invalid Input");
}
else
{
System.out.println(sum);
}
}
}
}
*********************************************************************************************
******************************************************************************

12.LOTTERY TICKETS

INPUT: Enter the Starting range


23467
Enter the Ending range
23477

OUTPUT: 23469
23472

import java.util.*;
public class Main {
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);
System.exit(0);
}
}
}

*********************************************************************************************
******************************************************************************
13.COFFEE STALL NUMEROLOGY

INPUT : Enter the Staff Name


Coffee Bar

OUTPUT : Coffee Bar satisfies the numerology logic

import java.util.*;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Staff Name");
String name = sc.nextLine();
int count=0;
for(int i=0;i<name.length();i++){
if(Character.isAlphabetic(name.charAt(i)) || name.charAt(i)==' '){
count++;
}
}
if(count==name.length()){
String str = name.replace(" ","");
int sum=0;
for(int i=0;i<str.length();i++){
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");
}
}
}

*ALTERNATE METHOD USING -matches for same question

import java.util.*;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Staff Name");
String name = sc.nextLine();
if(name.matches("[a-zA-Z ]+")){
String str = name.replace(" ","");
int sum=0;
for(int i=0;i<str.length();i++){
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");
}
}
}

*ALTERNATE METHOD

import java.util.Scanner;
public class CoffeeHouse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Stall Name");
String name = sc.nextLine();
if (name.matches("^[A-Za-z ]*$")) {
int sum = 0;
String fName = name.replaceAll(" ", "");
for (int i = 0; i < fName.length(); i++)
sum = sum + i;
if (sum % 2 == 0) {
System.out.println(name + " satisfies the numerology logic");
} else {
System.out.println(name + " not satisfies the numerology logic");
}
} else
System.out.println("Invalid Input");
}
}

*********************************************************************************************
******************************************************************************

14.ANAGRAM OR TWO WORDS OF SAME LETTER

INPUT : cat
act

OUTPUT: Same

import java.util.Arrays;
import java.util.Scanner;
public class minmax {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter sentence 1");
String s1=sc.next();
s1=s1.toLowerCase();
System.out.println("Enter sentence 2");
String s2=sc.next();
s2=s2.toLowerCase();
if(s1.length()!=s2.length())
{
System.out.println("Invalid");
}
if(s1.matches("^[a-zA-Z]*")){
char c1[] = s1.toLowerCase().toCharArray();
char c2[] = s2.toLowerCase().toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
if (Arrays.equals(c1, c2)) {
System.out.println(s1 + " and " + s2 + " contain the same characters");
} else {
System.out.println(s1 + " and " + s2 + " contain the different characters");
}
}
else
{
System.out.println("Invalid");
}
}
}

ALTERNATE METHOD:

import java.util.*;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String first=sc.next();
String secound=sc.next();
String word1=Main.checker(first);
String word2=Main.checker(secound);
if(word1.equals(word2)){
System.out.println("Same");
}else{
System.out.println("Different");
}
}
public static String checker(String word){
char arr[]=word.toCharArray();
Arrays.sort(arr);
int index=0;
for(int i=0;i<arr.length;i++){
int j;
for(j=0;j<i;j++){
if(arr[j]==arr[i]){
break;
}
}
if(i==j){
arr[index++]=arr[i];
}
}
char arr1[]=Arrays.copyOf(arr,index);
String newword= new String(arr1);
return newword;
}
}

ALTERNATE METHOD

import java.util.*;
public class arraysquare {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int f1=0,f2=0;
String s1=sc.nextLine();
char c1[]=s1.toCharArray();
String s2=sc.nextLine();
char c2[]=s2.toCharArray();
for(int i=0;i<c1.length;i++)
{
f1=0;
for(int j=0;j<c2.length;j++)
{
if(c1[i]==c2[j])
{
f1=1;
break;
}
}
if(f1==0)
{
break;
}
}
for(int i=0;i<c2.length;i++)
{
f2=0;
for(int j=0;j<c1.length;j++)
{
if(c2[i]==c1[j])
{
f2=1;
break;
}
}
if(f2==0)
{
break;
}
}
if(f1==1 && f2==1)
{
System.out.println("Same Char");
}
else{
System.out.println("Different char");
}
}
}

*********************************************************************************************
*****************************************************************************
15.SUMMATION OF EVEN NUMBERS IN THE ARRAY

INPUT : Enter array size


4
Enter array elements in 1st array
2
4
6
8
Enter array elements in 2nd array
1
2
5
2

OTPUT: 0
6
0
10

import java.util.*;
public class Main
{
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");
Runtime.getRuntime().halt(0);
}
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();
}
//PROGRAM LOGIC
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("No even number is present in an Array");
}
for(int i=0;i<n;i++)
{
System.out.println(result[i]);
}
}
}

*********************************************************************************************
******************************************************************************
16.RUNNERS COMPETITION

INPUT :Enter the number of runners


5

Enter the runner details


Robert,9.38
Richard,9.35
Christiano,9.35
Williams,9.36
Vinix,9.35

OUTPUT: Richard
Christiano
Vinix

import java.util.*;
public class Main{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
System.out.println("Enter the number of runners");
int n = sc.nextInt();
if(n<=0 || n>10){
System.out.println(n+" is an invalid number of runners");
Runtime.getRuntime().halt(0);
}
String temp = "";
String name[] = new String[n];
float time[] = new float[n];
System.out.println("Enter the runner details");
for(int i=0;i<n;i++){
temp = sc.next();
String temparr[] = temp.split(",");
name[i] = temparr[0];
time[i] = Float.parseFloat(temparr[1]);
if(time[i]>12 || time[i]<8){
System.out.println(time[i]+" is an invalid input");
Runtime.getRuntime().halt(0);
}
}
int maxcount = 0;
float maxrep = 0;
for(int i=0;i<n;i++){
int count = 0;
for(int j=i+1;j<n;j++){
if(time[i]==time[j]){
count++;
}
}
if(count>=maxcount){
maxcount = count;
maxrep = time[i];
}
}
if(maxcount==n){
System.out.println(n+" runners have same timing");
}
else if(maxcount==0){
System.out.println("No runners with same time.");
}
else{
for(int i=0;i<n;i++){
if(maxrep == time[i]){
System.out.println(name[i]);
}
}
}
}
}
*********************************************************************************************
******************************************************************************

17.REEDME

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") && !categ


ory.equals("thriller")){
System.out.println(category+" is invalid category");
Runtime.getRuntime().halt(0);
}
System.out.println("Enter the quantity of books ordered");
int quantity = sc.nextInt();
if(quantity <= 0){
System.out.println(quantity+" is an invalid quantity");
Runtime.getRuntime().halt(0);
}
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);
}
}

*********************************************************************************************
******************************************************************************
18. HARSHAD NUMBER (Sum of digit completely divide that number)

For example: 18= 1+8=9 and in 18/9 remainder is 0


INPUT: Enter array size: 5
Enter array elements : 18 14 12 24 13

OUTPUT: 18 12 24

import java.util.Scanner;
public class Harshad {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of values");
int n = sc.nextInt();
int i;
System.out.println("Enter the numbers");
int[] a = new int[n];
if (n > 0)
{
int flag = 0;
int k = 0;
for (i = 0; i < n; i++)
{
k = sc.nextInt();
if (k > 9)
a[i] = k;
else
{
flag = 1;
break;
}
}
if (flag != 1)
{
int c = 0;
for (i = 0; i < n; i++)
{
int x = a[i];
int sum = 0;
do {
int r = x % 10;
sum = sum + r;
x = x / 10;
}
while (x != 0);
if (a[i] % sum == 0)
{
c++;
System.out.println(a[i]);
}
}
if (c == 0)
System.out.println("The " + n + " values are not harshad number");
}
else
System.out.println("Provided " + k + " is not valid");
} else
System.out.println(n + " is an invalid input");
}
}

ALTERNATE METHOD:

import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of values");
int n=sc.nextInt();
System.out.println("Enter the numbers");
int a[]=new int[n];
if(n>0)
{
int flag=0;
int k=0;
for(int i=0;i<n;i++)
{
k=sc.nextInt();
if(k>9) a[i]=k; else
{
flag=1;
break;
}
}
if(flag!=1)
{
int c=0;
for(int i=0;i<n;i++)
{
int x=a[i];
int sum=0;
do
{
int r=x%10; sum=sum+r; x=x/10;
}while(x!=0);
if(a[i]%sum==0)
{ c++;
System.out.println(a[i]);
}
}
if(c==0)
System.out.println("The "+n+" values are not harshad number");
}else
System.out.println("Provided "+k+" is not valid");
}else
System.out.println(n+" is an invalid input");
}
}
*********************************************************************************************
*********************************************************************************************
*******
19. IDEO GAME (PS1)

INPUT : Enter the amount


18000
Enter the Video Game Player Type
PS4

OUTPUT: You can buy PS4

public class PS {
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("Enter the Video Game Player Type");
String type = sc.next();
if (type.matches("[P][S][1-5]")) {
if (type.equals("PS1")) {
if (amount >= 5000) {
System.out.println("You can buy PS1");
} else {
System.out.println("You need more money to buy a PS1");
}
} else if (type.equals("PS2")) {
if (amount >= 7800) {
System.out.println("You can buy PS2");
} else {
System.out.println("You need more money to buy a PS2");
}
} else if (type.equals("PS3")) {
if (amount >= 9500) {
System.out.println("You can buy PS3");
} else {
System.out.println("You need more money to buy a PS3");
}
} else if (type.equals("PS4")) {
if (amount >= 12000) {
System.out.println("You can buy PS4");
} else {
System.out.println("You need more money to buy a PS4");
}
} else if (type.equals("PS5")) {
if (amount >= 15000) {
System.out.println("You can buy PS5");
} else {
System.out.println("You need more money to buy a PS5");
}
}
} else {
System.out.println(type + " is Invalid Type");
}
} else {
System.out.println(amount + " is too less");
}
}
}

*********************************************************************************************
*********************************************************************************************
****
20.ALTERNATE SUM DIFFERENCE

INPUT :Enter number


6
Enter values
12
13
6
8
9
7
(((( logic: (12-7=a
13-9=b
6-8=c
a+b+c=ans)))))

OUTPUT 2
3
4
5
5-2=3
4-3=1
So 3+1= 4
*/

import java.util.*;
public class AltDiff {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum=0;
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n/2;i++)
{
sum+=Math.abs(a[i]-a[n-i-1]);
}
System.out.println(sum);
}
}
*********************************************************************************************
*********************************************************************************************
*****
21.CHARACTER ADD

INPUT : Enter : 3
Enter the sentences:
ks436
Agh73
7222

OUTPUT: 222
240
0

import java.util.*;
public class arraysquare{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum=0;
System.out.println("Enter:");
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++)
{
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);
sum=0;
}

}
}

*********************************************************************************************
*********************************************************************************************
******
22.MIDDLE LETTER/Reverse first half of the string if string length is ODD

Input:
if length of string is even, the reverse the string:
Ex: Food
Output:
dooF

Input:
If length of the string is odd,then find the middle char and then ,reverse the string till mid char and print remain char
acters as it is.
Ex: Samsung
Output:
maSsung

package FinalAss;
import java.util.*;
public class MidLett {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();//rahul
int len=str.length();
if(len%2==0)
{
StringBuilder sb=new StringBuilder(str);
sb.reverse();
System.out.println(sb.toString());
}
else if(len%2!=0)
{
System.out.println("Length of string is " +len);
int mid=len/2;
mid++;
System.out.println("Substring of string is "+mid);
String substr=str.substring(0,mid-1);
String remainSub=str.substring(mid-1,len);
StringBuilder sb=new StringBuilder(substr);
sb.reverse();
System.out.println(sb.toString()+remainSub);

}
}
}

ALTERNATE METHOD:::

import java.util.Scanner;
import java.util.regex.Pattern;
public class minmax {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();//rahul
if(!Pattern.matches("[A-Za-z]+",str))
{
System.out.println(str+" is not a valid string");
return;
}
else if(str.length()<2)
{
System.out.println("Size of string "+str.length()+" is too small");
return;
}
int len=str.length();
if(len%2==0)
{
StringBuilder sb=new StringBuilder(str);
sb.reverse();
System.out.println(sb.toString());
}
else if(len%2!=0)
{
int mid=len/2;
String substr=str.substring(0,mid);
String remainSub=str.substring(mid,len);
StringBuilder sb=new StringBuilder(substr);
sb.reverse();
System.out.println(sb.toString()+remainSub);

}
}
}

*********************************************************************************************
*********************************************************************************************
*****

23 .ONLINE SHOPPING

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

*********************************************************************************************
*********************************************************************************************
*****
24 .OPERATOR FOUND

input:
45
23

22
output
45-23=22

import java.util.*;
public class arraysquare {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter n1 and n2:");
int n1=sc.nextInt();
if(n1>0)
{
int n2=sc.nextInt();
if(n2>0)
{
System.out.println("Enter n3:");
int n3=sc.nextInt();
if(n3>0)
{
if(n1+n2==n3)
{
System.out.println(n1+"+"+n2+"="+n3);
}
else if(n1-n2==n3)
{
System.out.println(n1+"-"+n2+"="+n3);
}
else if(n1*n2==n3)
{
System.out.println(n1+"*"+n2+"="+n3);
}
else if(n1/n2==n3)
{
System.out.println(n1+"/"+n2+"="+n3);
}
else
{
System.out.println(n3+" is an invalid answer");
}
}
else{
System.out.println("Invalid");
}
}
else{
System.out.println("Invalid");
}
}
else{
System.out.println("Invalid");
}

}
}

*********************************************************************************************
*********************************************************************************************
****

25 .PRODUCT EQUAL SUM

INPUT : Enter the array size


4
Enter the elements of the first array
12
35
56
34
Enter the elements of the second array
261
195
112
813
OUTPUT: 35,195
34,813

import java.util.*;
public class arraysquare
{
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<=0)
{
System.out.println("Invalid array size");
return;
}
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();
}
System.out.println("Enter the elements of the second array");
int b[]=new int[n];
for(int i=0;i<n;i++)
{
b[i]=sc.nextInt();
}
int x[]=new int[n];
int y[]=new int[n];
for(int i=0;i<n;i++)
{
int c=a[i];
int prod=1;
while(c!=0)
{
prod*=(c%10);
c/=10;
}
x[i]=prod;
prod=0;
}
for(int i=0;i<n;i++)
{
int s=b[i];
int sum=0;
while(s!=0)
{
sum+=(s%10);
s/=10;
}
y[i]=sum;
sum=0;
}
for(int i=0;i<n;i++)
{
if(x[i]==y[i])
{
System.out.println(a[i]+","+b[i]);
}
}
}
}

*********************************************************************************************
*********************************************************************************************
*

26. UNIQUE CHAR

input:
LIfe is inherently risky

output:
life is ihrtly risky

package FinalAss;
import java.util.*;
public class nonuniquetwo {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
for(String word : words)
{
System.out.print(getUnique(word) + " ");
}
sc.close();
}
public static String getUnique(String str)
{
StringBuffer sb = new StringBuffer();
for(int i = 0; i<str.length(); i++)
{
if(countFrequency(str.charAt(i), str) == 1)
{
sb.append(str.charAt(i));
}
}
String result = sb.toString();
return result;
}
public static int countFrequency(char c, String str)
{
int count = 0 ;
for(int i = 0; i<str.length(); i++)
{
if(str.charAt(i) == c)
{
count++;
}
}
return count;
}
}

*********************************************************************************************
*********************************************************************************************
*
27. BOX SIZE

INPUT : Enter the box size


6
Enter the numbers
5
55
3
59
42
8
OUTPUT: 32 is even its a valid box

import java.util.Scanner;
public class minmax {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the box size");
int a=sc.nextInt();
if(a<=0)
{
System.out.println(a+" is an invalid box size");
return;
}
int b[]=new int[a];
int c=0,sum=0,temp=0;
System.out.println("Enter the numbers");
for(int i=0;i<a;i++)
{
b[i]=sc.nextInt();
if(b[i]<=0)
{
System.out.println(b[i]+" is an invalid input");
return;
}
}
for(int i=0;i<a;i++)
{
c=b[i]%10;
sum+=c;
c=0;
}
if(sum%2==0)
{
System.out.println(sum+" is even its a valid box");
return;
}
else
{
System.out.println(sum+" is odd its an invalid box");
return;
}

}
}
*********************************************************************************************
*********************************************************************************************
***

28. ORDER IDENTIFICATION:(check where elements are in ascending order)

INPUT : Enter the array size : 5


Enter the elements: 32
44
55
66
77

OUTPUT : 32 44 55 66 77 are in ascending order

import java.util.Arrays;
import java.util.Scanner;

public class orderidentification {


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size");
int a=sc.nextInt();
if(a<2 || a>10)
{
System.out.println(a+" is not a valid array size");
return;
}
int b[]=new int[a];
int count=0;
System.out.println("Enter the elements");
for(int i=0;i<a;i++)
{
b[i]=sc.nextInt();
}
int d[]= Arrays.copyOf(b,b.length);
Arrays.sort(d);

if(Arrays.equals(b,d))
{
count++;
}
if(count>0)
{
for(int i=0;i<b.length;i++)
{
System.out.print(b[i]+" ");

}
System.out.print("are in ascending order");
return;
}
else
{
for(int i=0;i<a;i++)
{
System.out.print(b[i]+" ");
}
System.out.print("are not in ascending order");
return;
}
}
}
*********************************************************************************************
**************************************************************
29) DATE ,MONTH AND YEAR

INPUT:13081995

OUTPUT: date: 13
month: 08
year: 1998

import java.util.Scanner;
public class minmax {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int n=str.length();
for(int i=0;i<str.length() ;i++)
{
if(!(str.charAt(i)>='0' && str.charAt(i)<='9'))
{
System.out.println("Invalid input");
return;
}
if(n!=8)
{
System.out.println("Enter valid date");
return;
}
}
String date=str.substring(0,2);
String mon=str.substring(2,4);
String year=str.substring(4,8);

System.out.println("date:"+date);
System.out.println("Month:"+mon);
System.out.print("Year:"+year);

}
}

*********************************************************************************************
*********************************************************************************************
*
30) MEGA MART CUSTOMER IDENTIFICATION(String and SUBSTRING)

import java.util.*;
public class arraysquare{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the customer id");
String cid = sc.next();
String shop = cid.substring(0,4);
//System.out.println("shop");
if(!shop.equals("Mega")){
System.out.println("Invalid Shop Name");
Runtime.getRuntime().halt(0);
}
String type = cid.substring(4,cid.length()-3);
//System.out.println(type);
if(!type.equals("Silver") && !type.equals("Gold") && !type.equals("Platinum")){
System.out.println("Invalid Customer Type");
Runtime.getRuntime().halt(0);
}
int mid = Integer.parseInt(cid.substring(cid.length()-3,cid.length()));
//System.out.println(mid);
if(mid<=99 || mid>=1000){
System.out.println("Invalid Member id");
Runtime.getRuntime().halt(0);
}
System.out.println("Welcome Mega Mart "+type+" Customer");

}
}

*********************************************************************************************
***********************************
31) .MALE AND FEMALE COUNT

INPUT: MmFMff

OUTPUT: 3 MALE
3 FEMALE

import java.util.Scanner;
import java.util.regex.*;
public class minmax {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int count1=0;
int count2=0;
if(Pattern.matches("[MmFf ]+",s)){
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'M' || s.charAt(i) == 'm') {
count1++;
} else if (s.charAt(i) == 'F' || s.charAt(i) == 'f') {
count2++;
}
}
System.out.println(count1 + " Male");
System.out.println(count2 + " Female");
}
else
{
System.out.println("Invalid Input");
}
}
}
*********************************************************************************************
*********************************************************************************************
***
32. 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

import java.util.Scanner;
public class minmax {
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");
}
}

*********************************************************************************************
*********
33) NUMEROLOGY NAME CHECKING

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

public class arraysquare{


public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name");
String a=sc.next();
if(!Pattern.matches("[A-Za-z]+",a))
{
System.out.println(a+" is an Invalid name");
return;
}
int sum=0;
char b[]=a.toCharArray();
for(int i=0;i<b.length;i++)
{
if(b[i]=='A'||b[i]=='a')
{
sum+=1;
}
else if(b[i]=='B'||b[i]=='b')
{
sum+=2;
}
else if(b[i]=='C'||b[i]=='c')
{
sum+=3;
}
else if(b[i]=='D'||b[i]=='d')
{
sum+=4;
}
else if(b[i]=='E'||b[i]=='e')
{
sum+=5;
}
else if(b[i]=='F'||b[i]=='f')
{
sum+=6;
}
else if(b[i]=='G'||b[i]=='g')
{
sum+=7;
}
else if(b[i]=='H'||b[i]=='h')
{
sum+=8;
}
else if(b[i]=='I'||b[i]=='i')
{
sum+=9;
}
else if(b[i]=='J'||b[i]=='j')
{
sum+=10;
}
else if(b[i]=='K'||b[i]=='k')
{
sum+=11;
}
else if(b[i]=='L'||b[i]=='l')
{
sum+=12;
}
else if(b[i]=='M'||b[i]=='m')
{
sum+=13;
}
if(b[i]=='N'||b[i]=='n')
{
sum+=14;
}
if(b[i]=='O'||b[i]=='o')
{
sum+=15;
}
if(b[i]=='P'||b[i]=='p')
{
sum+=16;
}
if(b[i]=='Q'||b[i]=='q')
{
sum+=17;
}
if(b[i]=='R'||b[i]=='r')
{
sum+=18;
}
if(b[i]=='S'||b[i]=='s')
{
sum+=19;
}
if(b[i]=='T'||b[i]=='t')
{
sum+=20;
}
if(b[i]=='U'||b[i]=='u')
{
sum+=21;
}
if(b[i]=='V'||b[i]=='v')
{
sum+=22;
}
if(b[i]=='W'||b[i]=='w')
{
sum+=23;
}
if(b[i]=='X'||b[i]=='x')
{
sum+=24;
}
if(b[i]=='Y'||b[i]=='y')
{
sum+=25;
}
if(b[i]=='Z'||b[i]=='z')
{
sum+=26;
}
}
System.out.println(sum);
if(sum%3==0 && sum%2==0)
{
System.out.println(a+" is a numerology name");
}
else
{
System.out.println(a+" is not a numerology name");
}
}
}

*******************************************************************************

34)OMR

import java.util.Scanner;
public class arraysquare{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i;
System.out.println("Enter the No of questions");
int a=sc.nextInt();
if(a<0)
{
System.out.println("Invalid Number");
return;
}
int count=0;
int mark=0;
char b[]=new char[a];
System.out.println("Enter the answer key");
for (i=0;i<a;i++) {
b[i] = sc.next().charAt(0);

if ((b[i] >= 'a' && b[i] <= 'e')||(b[i] > 'E')) {


System.out.println("Invalid Answers");
return;
}
}
char c[]=new char[a];
System.out.println("Enter the student answers");
for (i=0;i<a;i++)
{
c[i]=sc.next().charAt(0);
if ((c[i] >= 'a' && c[i] <= 'e')||(b[i]>'E')) {
System.out.println("Invalid Answers");
return;
}
}

for(i=0;i<a;i++)
{
if(b[i]==c[i])
{
count++;
}
}
if(count>0)
System.out.println("Correct answers are "+count);
mark=(count*100)/a;
if(count<1)
System.out.println("All answers are wrong");
System.out.println("Mark is "+mark);

}
}
JAVA END
*********************************************************************************************
*********************************************************************************************
********

RDBMS IMPORTANT CODES(NOT TESTED)

1.)Select concat(concat(customer_name,'has taken a


policy on'),date_of_policy) as Enrollment_details
from customer JOIN Policy using (customer_id)
order by customer_name,date_of_policy;

2.)select plolicy_id,policy_name,rate_of_interest, bonus_percentage


from policy
where minimum_primeium_amount = 1200
order by policy_d;

3.)select fine_range,fine_amount
CASE
When fine_range=L1
then 5+fine_amount
when fine_range=L2
then 10+fine_amount
When fine_range=M1
then 15+fine_amount
when fine_range=M2
then 20+fine_amount
When fine_range=H1
then 25+fine_amount
when fine_range=H2
then 30+fine_amount
END as NEW_FINE_AMOUNT

4.)select leave_type,allocated_days
CASE
when leave_type='CASUAL'
then allocated_days+2
when leave_type='SICK'
then allocated_days+5
when leave_type='MATERNITY'
then allocated_days+90
when leave_type='MARRIAGE'
then allocated_days+3
when leave_type='STUDY'
then allocated_days-2
END as NEW_ALLOCATED_TYPES
from table_name
order by leave_type;

5.) select emp_id,emp_name,department_id,department_name


from Department JOIN Employee using (department_id)
JOIN Leave+Avail_details using (Emp_id)
JOIN Leave using (Leave_type)
where Allocated_days = 0
order by emp_id;
*********************************************************************************************
**

1.) Account Info - "HDVL002"


select account_type, count(account_id) as TOTAL_ACCOUNTS
from Acount_Info where IFC_Code = 'HDVL002'
group by account_type order by account_type;

2.)select customer_id,customer_name,phone_no,order_date
from Customer_info JOIN Orders using (customer_id)
where order_amount > 500
order by customer_id,order_date;

3.) select distinct customer_id,customer_name,phone_no,loan_amount as


total_loan_amount from customers JOIN loans_taken using
(customer_id)
order by customer_id;

4.) select distinct boat_id,seat_capacity,count(ride_id) as ride_count


From Boat_details JOIN Ride_details using (boat_id)
order by boat_id;

5.) select customer_id,customer_name,contact_no,mail_id


from Coustomer_details where Gender = 'M'
order by customer_id;
6.) select customer_name,total_amount_paid
from Customer_master JOIN Booking_details using (customer_id)
where total_amount_paid < (select max(total_amount_paid) from booking_details
where total_amount_paid < (select max(total_amount_paid) from booking_details))
order by customer_name;

7.) select book_code,book_title, author,rack_num


from book_details where category = 'JAVA'
order by book_code;

8.) select customer_name,policy_enrollment as Enrollment_details


from customer JOIN policy_enrolmment using (customer_id)
order by customer_name,date_of_enrollment;

9.)DECODE QUESTION
select fine_range, fine_amount,
decode(fine_range, 'L1',fine_amount+5,
'L2',fine_amount+10,
'M1',fine_amount+10,
'M2',fine_amount+10,
'H1',fine_amount+10,
'H2',fine_amount+10)new_fine_amount
from fine_details;

10.)select customer_name||" has taken policy on"|| date_of_enrollment as


enrollment_details form customer
join policyenrollent using (customer_id)
order by customr_name,date_of_enrollment;

11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
order by policy_id;

12.) select customer_id,customer_name, street,phone_no,email


from Customers JOIN loan_taken using (customer_id)
where loan_amount > = 500000 and loan_amonunt <1500000
order by customer_id;

13.)select distinct boat_id, boat_name,boat_type


from boat_details JOIN ride_details using (boat_id)
where DOT LIKE '%-AUG-%' and Shift = 'evening'
order by boat_id;

14.) select policy_id,policy_name,rate_of_interest


from policy where miniimum_ppremium_amount < 3000 AND bonus_percent > 85
order by policy_id;

RDBMS ANSWERS

1.1) Write a query to display boat_type and number of boats running under each type. Give an alias name as 'NO_OF
_BOATS'.
Note: Boat_type can be 'DELUXE' or 'SUPER DELUXE'.

select boat_type,count(Boat_id) as NO_OF_BOATS


from boat_details
group by Boat_type;

1.2) select boat_name,boat_type from boat_details


where seat_capacity between 100 and 200
order by boat_name desc;

2.1) select vehicle_type,count(vehicle_id)


from vehicle_details
group by vehicle_type
order by vehicle_type;

2.2) select driver_id,driver_name,phone_no,Driver_rating


from driver_details where driver_rating between 3 and 5
order by driver_rating desc;

3.1) select coursename from course


c join registration r on
c.courseid=r.courseid
having count(studid)>=2
group by coursename
order by coursename;

3.2) select s.studid,sum(fees) as TOTALFEES


from student s join registration r
on s.studid=r.studid join course c
on r.courseid=c.courseid
group by s.studid
order by s.studid;

3.3) select studid,count(CourseID) as NOOFCOURSES


from registration
group by Studid
order by NOOFCOURSES desc,Studid;

3.4) select student.stuid,firstname from student


join registration on registration.stuid=student.stuid
where lower(to_char(doj,'MON'))='jan';

3.5) select courseid,coursename from


course where duration between 10 and 15;

3.6) select courseid,registration.stuid from registration


join student on registration.stuid=student.stuid
where lower(to_char(doj,'MON'))=lower(to_char(dob,'MON'));
Longest substring

Input : abczgdpqrstubg
Output: pqrstu
*/
import java.util.*;
public class Main
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
int firstindex=0,lastindex=0,length=0;
for(int i=0;i<s.length();i++)
{
int asci=s.charAt(i);
int count=1;
for(int j=i+1;j<s.length();j++)
{
if(s.charAt(j)==++asci)
{
count++;
continue;
}
else
{
if(count>length)
{
firstindex=i;
lastindex=j;
length=count;
break;
}
}
}
}
System.out.println(s.substring(firstindex,lastindex));
}
}
Non Unique char Elimination from string

public class NonUniqueElimination {


public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String name=sc.nextLine(),abc="";
int count=0;
for(int i=0;i<name.length();i++)
{
for(int j=1;j<name.length()-1;j++)
{
if(name.charAt(i)==name.charAt(j))
{
name=name.replace(String.valueOf(name.charAt(j)),"");
count+=1;
}
}
}
if(count==0) {
System.out.println("All are unique character");
return;
}
else {
System.out.println(name);
}
}
Sum of integers at even or odd places

need to specify size (say 5) and then take 5 integer numbers as Input. If the
number of digits in the number is odd then we need to add all odd places digits
and if number of digits is even then we need to add all even place digits. In the
end sum of all these individual sum needs to be displayed as result.

Eg enter size :5
Enter numbers
123(1+3=4)
2536(5+6=11)
2(2)
57(7)
76542(7+5+2=14)
Output: 38 (4+11+2+7+14)

No of substring from a given string

Given a string and a substring, find the number of occurances of the substring in
the given string.
Check that the string can contain only alphabets.
Consider both string and substring in lower case.
Eg:
Enter the string
Entertainment
Enter substring
en
Output : 2
Unknown

input: psvy & 4


(find the difference between p&s i.e 2 and print next 4 letters)
Output: behk

calculate Price*Duration after the deduction of discounts in a Bike renting


Company

*Name should be only in alphabets.. If not Print Invalid


*discount eligible only when the Duration is 5 or more
*Discount table will be given for each card (like Visa,Rupay,Mastercard)
*You have to apply discount for based on the option they used.. (This data is as
1,2,3 where option 1 is visa card option 2 is Rupay)
*If Card option is given as 4,5,6,,... OR 0,-1,-2,,,.. Print "Try again" and go back to
get card option until it's a valid one..

best runner from the given inputs

Input will be given as(Name,seconds)


Jorge, 9.78
Alex, 9.65

Order identification
U need to find weather the array elements are in ascending order r not
Note:array size between 2 and 10(inclusive)
Input1:
Enter the array size:3
Enter the elements
53
52
51
Output:
53 52 51 are not in ascending order

Input2:
Enter the array size:5
Enter the elements
32
65
71
78
82
Output:
32 65 71 78 82 are in ascending order

Product of case count


you need to count uppercase letters and lower case letters in a given string
Multiply the counts and display
Note: string shouldn't contain any digits, special characters
Length shouldn't be greater than 10
Input:
YouRName
Output:
Product value is 15
Explanation
Uppercase letters=3
Lowercase letters=5
Display 3*5=15

import java.util.*;
public class Program{

public static void main(String[] args){


Scanner sc = new Scanner(System.in);
int countj=0,countk=0,i;
System.out.println("Enter string:");
String str=sc.next();
char str1[]=new char[str.length()];
for( i=0;i<str.length();i++) {
if(str.charAt(i)<=122&&str.charAt(i)>=97) {
countj++;
}
else if(str.charAt(i)<=90&&str.charAt(i)>=65) {
countk++;
}
}
System.out.println(countj+" "+countk);
int product=countj*countk;

System.out.println(product);

}}

Unknown
Here, size of the array should be taken as input, and it should be between 1 and
10 (inclusive)
If it is not in 1 and 10, print it as "invalid"
Now, by the comparing the values at the same indices of both the arrays, if both
values are even, then print the sum of the two values, otherwise print "0".
Before performing operation, make sure if no even element is present in both the
arrays, then print it as "There are no even elements in both the arrays",
otherwise perform the above operation.
For example, see the following inputs and outputs:
1. Sample input:
Enter the size of array: 3
Enter the elements in first array:
2
3
4
Enter the elements in second array:
1
5
8
Sample output:
0
0
12

2. Enter the size of array: 3


Enter the elements in first array:
5
3
1
Enter the elements in second array:
1
5
9
Sample output:
There are no even elements in both the arrays

3.Sample input:
Enter the size of array: 1
Enter the elements in first array:
5
Enter the elements in second array:
1
Sample output:There are no even elements in both the arrays
Unkown

Here, a list of grocery items are present as follows:

Rice - 3kg, 5 Rs/kg


Wheat - 2kg, 10Rs/kg
Oil - 4litres, 20Rs/litre
Kerosene - 5litres, 15Rs/litre

Now each of the items is abbreviated by R,W,O and K respectively

Sample input:
ORW
Sample output: the
115
Offer to customers
Toll fee for region code
Take input string of numbers. Extract last four numbers from the string. Calculate
the discount price of the product based on those four numbers.

We need to input the product code as a string


The length of that string shd be of 6-8
If not print invalid
And we need to extract last 4 characters from that string, that will be the MRP of
the product
If the MRP is between 0 to 10, then offer is 0₹
If MRP between 11 to 50, offer is 5₹
If MRP between 51 to 500, offer is 15
If MRP between 501 to 5000, offer is 105
If MRP is above 500, offer is 1005

We need to print the MRP as well as the price after the offer is applied

Sample input 1
20220050
Output 1
MRP : ₹50
Offer price : ₹45

Sample input 2
350004
Output 2
MRP: ₹4
Offer price :₹4

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

Reverse and Concatenate

She sells seashells


Same first letter for all words so reverse the last word and concatenate first word
Output: sllehsaesShe

If first letter is not same then reverse first word and concatenate with last word

Male female version code


import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){

Scanner sc = new Scanner(System.in);

String str = sc.next();

int count=0;
int count1=0;

if(Pattern.matches("[mfMF]+",str)){

for(int i=0;i<str.length();i++){
if(str.charAt(i)=='M' || str.charAt(i)=='m'){
count++;
}
else if( str.charAt(i)=='F' || str.charAt(i)=='f'){
count1++;
}
}
System.out.println(count+" Male");
System.out.println(count1+" Female");
}
else{
System.out.println("Not a valid input");
return;
}
}
}
LCM

Tony comes to hotel every 4(i) days and potts every 6(j) days when will they
meet? if i or j <0 print invalid, don't use system.exit (To find LCM)

Cake
Input : 4725strawberry181
Extract the weight , flavour , order id and output the cake price according to per kg price given
initially , and print all the cake details.
Output :
4.73 kg
Strawberry
181
Price:

import java.util.*;
public class cake_order {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(!s.matches("^[a-zA-z0-9]*$"))
{
System.out.println("Invalid string");
return;
}
else
{
String str=s.substring(0,4);
for(int i=0;i<str.length();i++)
{
if(!Character.isDigit(str.charAt(i)))
{
System.out.println("Invalid string");
return;
}

}
int num=Integer.parseInt(str);
if(num<1000)
{
System.out.println("Invalid weight");
return;
}
int l=s.length();
int li=l-3;
String s1=s.substring(li,l);
for(int i=li;i<l;i++)
{
if(!Character.isDigit(s.charAt(i)))
{
System.out.println("Invalid orderno");
return;
}
}
int k=Integer.parseInt(s1);
if(k<100)
{
System.out.println("Invalid Order no");
return;
}
double I=Double.parseDouble(str)/1000;
String form=String.format("%.2f", I);
System.out.println("Cake weight is "+form);
String falv="";
for(int i=4;i<li;i++)
{
falv+=s.charAt(i);
}
System.out.println("Cake flavour is "+falv);
System.out.println("Order no is "+s1);
double price=450*I;
String f=String.format("%.2f",price);
System.out.println("Price is "+f);
}
}
}

Products equals sum


Fun count

Input array of string


if there is 5 input
barbie
barbie
doll
doll
bike

output
barbie=2
doll=2
bike=1
Speed Calculation

The normal_speed 30km/h was given


Take two input hours and distance
Find speed= distance/hours
if speed > normalspeed
then
Actual speed=speed-normal_speed
print actual speed
else
print continue on speed
Add with index
Next Mixing Letters
Mega mart customer identification
Theater seat details
Compose Rhythm(Prime numbers btw two no’s)
Rating Score
Unknown
ASCii

Ascii

import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the String");
String str =in.nextLine();
for(int i=0;i<str.length();i++)
{
if((Character.isLetter(str.charAt(i)) || Character.isWhitespace(str.charAt(i))))
continue;
else{
System.out.println(str+" is not a valid string");
return;
}
}
int sum=0;
for(int i=0;i<str.length();i++){
if((i+1)%3==0){
sum+=(int)str.charAt(i);
}
}
System.out.println("Sum is "+sum);
}
}
Product equal sum

import java.util.*;
public class arraysquare
{
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;
}
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);
}
}
System.out.println("Enter the elements of the second array");
int b[]=new int[n];
for(int i=0;i<n;i++)
{
b[i]=sc.nextInt();
if(b[i]<10 || b[i]>999) {
System.out.println("Invalid input);
}
}
int x[]=new int[n];
int y[]=new int[n];
for(int i=0;i<n;i++)
{
int c=a[i];
int prod=1;
while(c!=0)
{
prod*=(c%10);
c/=10;
}
x[i]=prod;
prod=0;
}
for(int i=0;i<n;i++)
{
int s=b[i];
int sum=0;
while(s!=0)
{
sum+=(s%10);
s/=10;
}
y[i]=sum;
sum=0;
}
int count=0;
for(int i=0;i<n;i++)
{
if(x[i]==y[i])
{
System.out.println(a[i]+","+b[i]);
count++;
}
}
if(count==0) {
System.out.println("No pair found");
}
}
}

You might also like