Hands On Merged
Hands On Merged
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
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
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
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
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
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;
}
}
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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
*********************************************************************************************
******************************************************************************
18. HARSHAD NUMBER (Sum of digit completely divide that number)
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.VIDEO GAME (PS1)
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
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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
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;
}
}
}
*********************************************************************************************
*********************************************************************************************
***
import java.util.Arrays;
import java.util.Scanner;
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
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;
*******************************************************************************
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);
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
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.
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;
Gender Determination :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int count=0,count1=0;
if(!(s.matches("[mfMF]+")))
{
System.out.println("Invalid Input");
return;
}
else
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='m' ||s.charAt(i)=='M')
count++;
else
count1++;
}
System.out.println(count+" "+"Male");
System.out.println(count1+" "+"Female");
}}
LCM OF DAY INTERVAL/LUNCH/DINNER TOGETHER :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day interval of person 1:");
int a=sc.nextInt();
System.out.println("Enter the day interval of person 2:");
int b=sc.nextInt();
if(a<=0 || b<=0)
{
System.out.println("Given interval is not valid");
return;
3
}
int lcm;
lcm=(a>b)?a:b;
while(true){
if(lcm%a==0 && lcm%b==0){
System.out.println("person 1 and person 2 will have
their dinner on day "+lcm);
break;
}
lcm++;
}
}}
import java.util.*;
int n=sc.nextInt();
int count=0;
if(n<1 || n>10){
System.exit(0);
else{
for(int i=0;i<n;i++){
int b=sc.nextInt();
a1[i]=b;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int c=sc.nextInt();
a2[i]=c;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int mul=1,r=0,x=a1[i];
while(x>0){
r=x%10;
mul*=r;
5
x=x/10;
for(int j=0;j<n;j++){
int sum=0,p=0,y=a2[j];
while(y>0){
p=y%10;
sum+=p;
y=y/10;
if(mul==sum){
count++;
System.out.print(a1[i]+","+a2[j]);
System.out.println(" ");
}}}
if(count==0){
System.exit(0);
}}}}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
int index=0;
6
int count=0;
while((index=s1.indexOf(s2,index))!=-1){
index=index+1;
count+=1;
}
System.out.println(count);
}
}
Fun Count
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=0){
System.out.println("Invalid Input");
return;
}
if(n>15){
System.out.println("Number exceeds the limit");
return;
}
Map<String,Integer> a=new HashMap<>();
for(int i=0;i<n;i++){
String s=sc.next().toLowerCase();
if(a.containsKey(s))
a.put(s,1+a.get(s));
else
a.put(s,1);
}
Map<String,Integer> tm=new TreeMap<>(a);
for(Map.Entry<String,Integer>data:tm.entrySet()){
System.out.println(data.getKey()+"= "+data.getValue());
}
}
}
7
SPEED ESTIMATION :
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the distance in kilometer");
int km=sc.nextInt();
if(km<0) {
System.out.println("Invalid Input");
return;
}
System.out.println("Enter the time to reach in hours");
int hours=sc.nextInt();
if(hours<0){
System.out.println("Invalid Input");
return;
}
int speed=km/hours;
if(speed<30){
System.out.println("You drive a car at normal speed");
return;
}
int inc=speed-30;
System.out.println("You want to increase a speed "+inc+"
km/hr from a normal speed");
}}
Prime Number
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
int count=0;
8
if(a<0 || b<0){
return;
for(int i=a;i<=b;i++){
count = 0;
for(int j=2;j<=i;j++){
if(i % j == 0)
count = count+1;
if(count == 1)
System.out.print(i+" ");
if(count!=1)
Theatre
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(s.length()==6)
{
9
String a=s.substring(0,1);
if(a.matches("[NEWS]+"))
{
String b=s.substring(1,2);
if(b.matches("[ABCDE]+"))
{
String c=s.substring(2,3);
if(c.matches("[1-4]+"))
{
String d=s.substring(3,6);
int num=Integer.parseInt(d);
if(num>0 && num<500)
{
System.out.println("Name "+a);
System.out.println("Block "+b);
System.out.println("Floor number "+c);
System.out.println("Seat number
"+num);
}
else
System.out.println("Invalid Seat number");
}
else
System.out.println("Invalid Floor number");
}
else
System.out.println("Invalid Block");
}
else
System.out.println("Invalid Name");
}
else
System.out.println("Invalid size");
}}
10
Customer details
import java.util.Scanner;
}
}
}
}
Team Win/Loose
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.next();
int count=0;
if(str.length()<3) {
System.out.println("Invalid input");
return;
}
if(str.matches("[htHT]+")) {
String s=str.toLowerCase();
for(int i=0;i<s.length()-2;i++){
if(s.charAt(i)!=s.charAt(i+1) &&
s.charAt(i)==s.charAt(i+2))
count++;
}
if(count==s.length()-2)
System.out.println("Team will win");
else
System.out.println("Team may loose");
}
else {
System.out.println("Invalid Input");
return;
}
}}
Holiday Fun
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
12
int sum=0,sum1=0;
int n=sc.nextInt();
if(n>0){
String[] a=new String[n];
for(int i=0;i<n;i++){
a[i]=sc.next();
}
for(int i=0;i<n;i++){
int len=a[i].length();
if(len%2==0)
{
for(int j=1;j<len;j++){
sum=sum+Integer.parseInt(String.valueOf(a[i].charAt(j)));
j++;
}}
else
{
for(int k=0;k<len;k++){
sum1=sum1+Integer.parseInt(String.valueOf(a[i].charAt(k)));
k++;
}}
}
System.out.println(sum+sum1);
}
else
System.out.println("Invalid size");
}}
Reverse Prime
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0,c=0,f=0,r=0,prime=0;
int n=sc.nextInt();
if(n>0) {
int[] a=new int[n];
13
for(int i=0;i<n;i++) {
int num=sc.nextInt();
if(num>9)
a[i]=num;
else {
System.out.println("Provided "+num+" is not valid");
return;
}}
for(int i=0;i<n;i++) {
count=0;
for(int j=2;j<a[i];j++)
{
if(a[i]%j==0)
count++;
}
if(count==0) {
prime++;
f=a[i];
r=0;
while(a[i]!=0) {
r=r*10;
r=r+a[i]%10;
a[i] =a[i]/10;
}
for(int k=2;k<r;k++) {
if(r%k==0)
c++;
}
if(c==0)
System.out.println(f);
}}}
else {
System.out.println(n+" is less than a limit");
return;
}
if(prime==0)
System.out.println(n+" values are not prime numbers");
}}
14
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0;
System.out.println("Enter the size of array ");
int n=sc.nextInt();
int[] sum=new int[n];
if(n>=1 && n<=10)
{
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();
int[] b=new int[n];
System.out.println("Enter the elements of second array ");
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]%2==0 && b[i]%2==0)
{
count++;
sum[i]=a[i]+b[i];
}
else
sum[i]=0;
}
if(count==0)
System.out.println("There are no even elements");
else
for(int result:sum)
System.out.println(result);
}
else
System.out.println("Invalid");
}}
15
Groceries Bill
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int a=0,b=0,c=0,d=0,count=0;
if(s.matches("[ORWKorwk]+")) {
for(int i=0;i<s.length();i++) {
for(int j=0;j<s.length();j++) {
if(s.charAt(i)==s.charAt(j))
count++;
}}
if(count==s.length()) {
for(int k=0;k<s.length();k++)
{
if(s.charAt(k)=='O')
a=4*20;
else if(s.charAt(k)=='R')
b=3*5;
else if(s.charAt(k)=='W')
c=2*10;
else
d=5*15;
}
System.out.println(a+b+c+d);
}
else
System.out.println("Duplicates are not allowed");
}
else
System.out.println("Invalid");
}}
16
Cake Order
import java.util.*;
if (cd.matches("[a-zA-Z0-9]*")) {
String w=cd.substring(0, 4);
if(!w.matches("[0-9]*")){
System.out.println("Invalid weight");
return;
}
float wg=Integer.parseInt(w);
if (wg>999) {
float fwg=wg/1000;
double weight= (Math.round(fwg * 100.0) / 100.0);
String o=cd.substring(cd.length()-3);
if (!o.matches("[0-9]*")) {
System.out.println("Invalid order number");
return;
}
int on=Integer.parseInt(o);
if(on>99) {
String flavour=cd.substring(4,cd.length()-3);
double cakePrice=450*weight;
17
}
1. Maze Runner
Amy wants to create a game. Her game had 2 players, according to the movement of the players
points will vary. According to the direction, characters will be assigned. Only four characters can
be assigned, L-left, R-right, B-bonus, F-foul, if the direction is left add 50 points, if the direction is
right subtract 50 points, if there is a bonus double the points or if there is foul reduce the total
points to its half.
So according to the string calculate the points and compare it among the players. At the end of
the game, if the player1 have scored more points than player 2, print “Player 1 Wins” or else print
“Player 2 Wins”. At the end of the game, if both have secured equal points print “Draw Nobody
Wins” .Initial points are awarded to the players and the score calculation is done with that initial
points.
Note: Characters L,R,B,F are valid. If there is any other character present in the string print
“Invalid Input”. Number of characters must be same between the players or else print “Invalid
Input”. Initial points can be between 1000 to 5000 or else print “Invalid Input”.
Sample input 1:
1200
LLRRF
1000
RRLFB
Sample output 1:
Player 2 Wins
Sample input 2:
7000
Sample output 2:
Invalid Input
Sample input 3:
1900
LLRKF
Sample output 3:
Invalid Input
2. Holiday Trip:
Ana wants to book a ticket to Mumbai. She want to call few of her friends with her. Help her to
calculate the ticket amount by which they can go for their trip within their budget. There are
different flights like Air India, Spice Jet, Go Air, IndiGo.
According to the flight name travel cost varies. Each flight will have divisions like Economy class,
Travel class and Business class. Cost of the ticket will vary for each class. If the passenger pays
the ticket cost using card there is a percentage of reduction in the cost of the ticket. If the number
of passengers are more than 5, 10% discount is given and if it is more than 10, 15% discount is
given for IndiGo and Spice Jet alone from the final amount.
Air India:
Travel class:3200
Economy class:6950
Business class:10,340
Card Payment:10.5%
Spice Jet:
Travel class:3450
Economy class:7945
Business class:9500
Card Payment:7.5%
Go Air:
Travel class:3300
Economy class:7250
Business class:9890
Card Payment:9.5%
IndiGo:
Travel class:3490
Economy class:7560
Business class:9990
Card Payment:8.5%
Sample input 1:
Air India
Travel class
Card Payment:
Sample output 1:
22912
Explanation: (3200*8)-(3200*8*(10.5/100))
Write a java program to get two input from the user, the first input will be a string holding time in
24 hours format(HH:MM) and the second input will be an integer that holds the time interval in
minutes (MM). And display the time at which the next notifications has to be sent for that day. If
the time to send the next notification exceeds for the day display “Notification cannot be sent for
the day”.
If the range of hours (HH) in time should be between 00 and 23 (both inclusive) and the range of
minutes (MM) should be between 00 and 59(both inclusive). If not display “Input is not valid” and
terminate the program.
Sample Input 1:
22:00
05
Sample Output 1:
22:05
Sample Input 2:
23:45
20
Sample Output 2:
Sample Input 3:
25:50
Sample Output 3:
23:50
60
Sample Output 4:
David fueled his vehicle in a fuel station. He remembered that he asked the filling person to fill
the fuel for certain amount in rupees. But he don’t know the quantity of fuel in liters. Help him to
calculate the fuel in Liters from amount in Rupees by getting the rate per liter as input.
Note that, both the inputs must be a positive number. Otherwise print “Invalid Input”
The result should be in two decimal places. To get two decimal places refer the below-mentioned
print statement :
float cost=1105.00f;
System.out.printf(“%.2f”,cost);
Sample Input 1:
1000
89.35
Sample Output 1:
11.19 Liters
Sample Input 2:
-1000
Sample Output 2:
Invalid Input
Nancy was playing with her daughter, they were playing with words. That day her daughter was
taught vowels in her class. So they were playing with vowels. Nancy asked her daughter to
remove the vowels from the word, if they are present in continuous pattern alone.
Note: The word should not have more than 8 letters if so print “Word length exceeds the limit”
and less than 3 letters, if so print “Word length is too short”. Word cannot have numbers or
special character, if so print “Invalid Input”.
Sample input 1:
solution
Sample output 1:
solutn
Sample input 2:
hi
Sample output 2:
Sample input 3:
absurdity
Sample output 3:
Sample input 4:
Enter the word:
ba&ery
Sample output 4:
Invalid Input
6. MAC Validation
A MAC (Media Access Control) address is the unique identifier that is assigned by the
manufacturer to a piece of network hardware.
A MAC address consists of six sets of two characters, each separated by a colon.
00:1B:44:11:3A:B7 is an example of a MAC address. Each character should hold only
hexadecimal values ( 0 to 9 and A to F).
Write a java program to check the given string is a valid MAC address or not, if the given string is
valid then display “Valid MAC address” else display “Invalid MAC address”.
Note: If the given string contains less than six sets of two characters, each separated by a colon
display “String is too short”. If the given string contains more than six sets of two characters,
each separated by a colon display “String is too long”. If the given string contains any other
symbols other than colon (:) display “Invalid Input”.
Assumption: Each set in the given string will have only two characters and alphabets in the
Upper case.
Sample Input 1:
56:41:AB:5A:A5:60
Sample Output 1:
Sample Input 2:
62:55:VB:ZZ:A5:S6
Sample Output 2:
Sample Input 3:
62:55:CB
Sample Output 3:
Sample Input 4:
62:55:CB:62:55:CB:23
Sample Output 4:
Sample Input 5:
62&55*CB%62:55@CB
Sample Output 5:
Invalid Input
7. Unique products
John runs a department store and has multiple items in it. Each items will have a 3 digit bar code
ID to identify the items. He wants to segregate items which has Unique number in the 3 digit bar
code ID. A Unique number is a positive integer with no duplicate digits. For example 135, 214 are
all unique numbers whereas 311, 300 are not.Help him by your java skills.
Note: The number of items should be between the range 5 and 20 (both inclusive) else display
“Invalid item count” and terminate the program. The bar code ID of each item should be only a 3
digit number, if not display “Invalid bar code ID” and terminate the program. If there is no bar
code ID with Unique number then display “There are no item with Unique number in the bar code
ID”.
Sample Input 1:
10
111
222
123
125
654
354
844
141
200
201
Sample Output 1:
123
125
654
354
201
Sample Input 2:
111
222
110
333
100
Sample Output 2:
25
Sample Output 3:
Sample Input 4:
111
1000
Sample Output 4:
8.Toggle word
Reema is an English teacher, she teaches upper case and lower case letters to her students.
She wanted to test her students, whether her students have understood the concept. She wanted
to conduct it as a game. If the students perform well, she would like to present gifts. Help her to
conduct the game using Java program.
If a word is given, each alternate character in the word should be converted in to upper case ie,
In the given word, the first character should start with upper case followed by each alternate
character as upper case.
For example:
Note: Word should not have more than 8 letters if so print “Word length exceeds the limit” and
less than 3 letters, if so print “Word length is too short”. Word cannot have numbers or special
character, if so print “Invalid Input”.
Sample input 1:
aviation
Sample output 1:
AvIaTiOn
Sample input 2:
it
Sample output 2:
Sample input 3:
abundance
Sample output 3:
Sample input 4:
m@ngo
Sample output 4:
Invalid Input
Write a program to find the product of the prime digits in the given input number.
If the input number is negative or greater than 32767 Print it as "Invalid Input".
If the input number does not have any prime digits,print it as “No Prime Digit Found”
If the input number has single prime digit,then print that prime digit.
Please note that '1' and '0' are neither prime nor composite.
Input and Output Format:
Sample Input 1:
2017
Sample Output 1:
14
Explanation:
Number '2' and '7' are the prime digits in the given input and number '1' and '0' are neither prime
nor composite hence the product of prime digits will results in 14(2*7).
Sample Input 2:
483
Sample Output 2:
Sample Input 3:
-67
Sample Output 3:
Invalid Input
James needs to find the estimated delivery time of his parcel. Get the distance(in Km) between
the seller and buyer as input. Consider that the delivery vehicle can travel with 10 meter per
second as average speed. Calculate the delivery time and display the output in Minutes (rounded
value) as given in sample data.
Formula: T = D / S
(1Km=1000m, 1min=60sec).
Note that, the input should be in Kilometers and output should be in minutes.
Write a Java program to calculate the above. Input must be a positive number, otherwise print
“Invalid Input”.
The result should be in two decimal places. To get two decimal places refer the below-mentioned
print statement :
float cost=1105.00f;
System.out.printf(“%.2f”,cost);
Sample Input 1:
100
Sample Output 1:
Estimated Time:
166.67 Minutes
Sample Input 2:
-50
Sample Output 2:
Invalid Input
1. Maze Runner
Solution:
import java.util.*;
class Main
int points1=sc.nextInt();
if(points1<1000 || points1>5000)
System.out.println("Invalid Input");
else
String s1=sc.next();
int flag=0;
char ch1[]=s1.toCharArray();
for(char c:ch1)
//System.out.println(c);
flag++;
}
}
if(flag!=s1.length())
System.out.println("Invalid Input");
else
int points2=sc.nextInt();
if(points2<1000 || points2>5000)
System.out.println("Invalid Input");
else
String s2=sc.next();
if(s1.length()!=s2.length())
System.out.println("Invalid Input");
else
int flag1=0;
char ch2[]=s2.toCharArray();
for(char c:ch2)
{
if(c=='L' ||c=='R' || c=='F' ||c=='B')
flag1++;
if(flag1!=s2.length())
System.out.println("Invalid Input");
else
for(char c:ch1)
if(c=='L')
points1+=50;
if(c=='R')
points1-=50;
if(c=='B')
points1*=2;
if(c=='F')
{
points1/=2;
for(char c:ch2)
if(c=='L')
points2+=50;
if(c=='R')
points2-=50;
if(c=='B')
points2*=2;
if(c=='F')
points2/=2;
if(points1>points2)
System.out.println("Player 1 Wins");
else if(points1<points2)
{
System.out.println("Player 2 Wins");
else
2. Holiday Trip:
Solution:
import java.util.*;
String fname=sc.nextLine();
System.out.println("Enter the class:");
String fclass=sc.nextLine();
int no=sc.nextInt();
System.out.println("Card Payment:");
char pay=sc.next().charAt(0);
int cost=0;
switch(fname){
// System.out.println("Hai1111");
switch(fclass){
//System.out.println("Hai");
cost=3200;
break;
cost=6950;
break;
cost=10340;
break;
}
}
break;
switch(fclass){
if(no<=5){
cost=3450;
else if(no>5&&no<=10){
cost=(int)(3450*0.9);
else{
cost=(int)(3450*0.85);
break;
if(no<=5){
cost=7945;
else if(no>5&&no<=10){
cost=(int)(7945*0.9);
else{
cost=(int)(7945*0.85);
}
break;
if(no<=5){
cost=9500;
else if(no>5&&no<=10){
cost=(int)(9500*0.9);
else{
cost=(int)(9500*0.85);
break;
break;
switch(fclass){
cost=3300;
break;
break;
cost=9890;
break;
break;
case "IndiGo":{
switch(fclass){
if(no<=5){
cost=3490;
else if(no>5&&no<=10){
cost=(int)(3490*0.9);
else{
cost=(int)(3490*0.85);
break;
if(no<=5){
cost=7560;
else if(no>5&&no<=10){
cost=(int)(7560*0.9);
else{
cost=(int)(7560*0.85);
break;
if(no<=5){
cost=9990;
else if(no>5&&no<=10){
cost=(int)(9990*0.9);
else{
cost=(int)(9990*0.85);
break;
break;
int res=cost*no;
if(pay=='Y')
switch(fname){
res=(int)(res*0.895);
break;
res=(int)(res*0.925);
break;
res=(int)(res*0.905);
break;
case "IndiGo":
res=(int)(res*0.915);
break;
System.out.println(res);
}
Solution:
import java.util.Scanner;
String s=sc.next();
String ch[]=s.split(":");
for(int i=0;i<2;i++)
int m1=Integer.valueOf(ch[i]);
m[i]=m1;
{ int n=sc.nextInt();
int k=m[1]+n;
if(k>59)
if(l>23)
else
{m[0]=l;
count++;}
else
{ m[1]=k;
count++; }
System.out.println(m[0]+":0"+m[1]);
System.out.println(m[0]+":"+m[1]);
else
else
}
4.Fuel Quantity Calculation
Solution:
import java.util.*;
int rs=read.nextInt();
if(rs<1)
System.out.println("Invalid Input");
else
float liter=read.nextFloat();
if(liter<0)
System.out.println("Invalid Input");
else
float petrol=rs/liter;
System.out.printf("%.2f Liters",petrol);
}
}
}}
Solution:
import java.util.Scanner;
for(int i=0;i<s.length();i++)
if(Character.isLetter(s.charAt(i)))
count++;
if(count==s.length())
for(int i=0;i<s.length();i++)
{ if(i<s.length())
if(((s.charAt(i)=='a')||(s.charAt(i)=='e')||(s.charAt(i)=='i')||(s.charAt(i)=='o')||(s.charAt(i)=='u')
)&&
((s.charAt(i+1)=='a')||(s.charAt(i+1)=='e')||(s.charAt(i+1)=='i')||(s.charAt(i+1)=='o')||(s.char
At(i+1)=='u')))
{i++;
continue;
else
System.out.print(s.charAt(i));
else
System.out.print(s.charAt(i));
//System.out.println(s);
else
System.out.println("Invalid Input");
if(s.length()>8)
if(s.length()<3)
6. MAC Validation
Solution:
import java.lang.*;
import java.util.*;
String s=sc.nextLine();
int len=s.length();
int dc=0,cc=0,colonc=0,flag=0;
if(len==17){
char[] a=s.toCharArray();
for(char b:a){
if(Character.isLetter(b)){
if(b>=65&&b<=70)
cc++;
if(Character.isDigit(b)){
dc++;
if(b==':'){
colonc++;
if(!(Character.isLetter(b)||Character.isDigit(b)||b==':')){
flag++;
break;
}
}
if(flag==1){
System.out.println("Invalid Input");
// System.exit(0);
if(flag==0){
int res=cc+dc+colonc;
if(res==17){
else{
else if(len<17){
else{
}
}
7. Unique products
Solution:
import java.util.Scanner;
int item_count=sc.nextInt();
if(item_count<5 || item_count>20)
else
int flag=1;
int count=0;
for(int i=0;i<item_count;i++)
int it=sc.nextInt();
if(it<100 || it>999)
flag=0;
break;
else
item[i]=it;
if(flag!=0)
for(int j:item)
String s="";
s=s.valueOf(j);
if(s.charAt(0)!=s.charAt(1)&&s.charAt(0)!=s.charAt(2)&&s.charAt(2)!=
s.charAt(1))
System.out.println(j);
count++;
}
if(count==0)
else
8.Toggle word
Solution:
import java.util.*;
class Main
String s=sc.next();
int length=s.length();
if(length<3)
else if(length>8)
else
char c[]=s.toCharArray();
int flag=0;
for(char c1:c)
if(Character.isLetter(c1))
flag++;
}
if(flag!=length)
System.out.println("Invalid Input");
else
int i=0;
for(char c1:c)
if(i%2==0)
System.out.print(Character.toUpperCase(c1));
i++;
else
System.out.print(c1);
i++;
Solution:
import java.util.*;
class Main
int num=sc.nextInt();
int product=1;
if(num<0 || num>32767)
System.out.println("Invalid Input");
else
while(num>0)
int a=num%10;
product*=a;
num=num/10;
if(product==1)
}
else
System.out.println(product);
Solution:
import java.util.*;
int distance=read.nextInt();
if(distance<1)
System.out.println("Invalid Input");
else
float dism=distance*1000;
float div=dism/10;
float time=div/60;
System.out.println("Estimated Time:");
System.out.printf("%.2f Minutes",time);
}}}
JAVA
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
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
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) COMBINE NUMBERS / STUDENTS WHOSE ROLLNO ARE ODD
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
andLOWERcaseletters
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
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;
}
}
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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]);
}
}
}
}
}
****ALTERNATE METHOD****
}
boolean flag = true;
double min=time[0];
int index=0;
for(int i=0;i<n;i++)
{
if(time[i]<min)
{
min=time[i];
//index=i;
}
if(min!=time[i])
{
flag=false;
System.out.println(name[i-1]);
}
}
if (flag) System.out.println(n+" runners have same timing");
}
}
*********************************************************************************************
******************************************************************************
17.REEDME
*********************************************************************************************
******************************************************************************
18)HARSHAD NUMBER (Sum of digit completely divide that
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) VIDEO GAME PLAYER (PS1)
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) ABSOLUTE DIFFERENCE / ALTERNATE SUM DIFFERENCE
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
ADDITION
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. REVERSE FIRST HALF/MIDDLE LETTER 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");
}
}
}
***ALNERNATE METHOD****
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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]);
}
}
}
}
*********************************************************************************************
*********************************************************************************************
*
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;
}
}
***ALTERNATE METHOD*****
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Enter the String");
Scanner sc=new Scanner(System. in);
String str=sc.nextLine();
String output="";
String[] word=str.split(" ");
for(int i=0;i<word.length;i++)
{
for(int j=0;j<word[i].length();j++)
{
int count=0;
char c=word[i].charAt(j);
for(int k=0;k<word[i].length();k++)
{
if(c==word[i].charAt(k))
count++;
}
if(count==1)
{
output+=Character.toLowerCase(c);
}
}
output+=" ";
}
System.out.println(output);
}
}
*********************************************************************************************
*********************************************************************************************
*
27. BOX SIZE
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;
}
}
}
****ALTERNATE METHOD***********
Input:
Enter size:5
Enter numbers:
2
34
456
67
123
Explanation:
22=(2+4+6+7+3)
Similarly for invalid box also
}
********************************************************************************************
****************************************************
28. ORDER IDENTIFICATION:(check where elements are in ascending
import java.util.Arrays;
import java.util.Scanner;
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;
}
}
***ALTERNATE METHOD***
********************************************************************************************
********************************************************************************************
********************************
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
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;
*******************************************************************************
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);
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);
}
}
****ALTERNATE METHOD****
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));
}
}
}
}
**********************************************************************************************
***************************************************
INPUT:
Enter the array size
5
Sample Output:
12
6
21
36
20
Logic:
12*1=12
3*2=6
7*3=21
9*4=36
4*5=20
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size:");
int n=sc.nextInt();
int res=0;
if(n<1 || n>10)
{
System.out.println("Invalid Array Size");
return;
}
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
res=a[i]*(i+1);
System.out.println(res);
}
}
}
**********************************************************************************************
******************************************************************
36) CAR PARKING
Sample Input:
Enter the car number
ap 37 1498
Sample Output
Park the car in 3rd floor
import java.util.*;
ALTERNATE METHOD:
Sample Input:
Sample output
6 appears in 56164 is 2
Explanation:
Digit 6 appears 2 times in the number 56164
package accenture;
import java.util.*;
if(ch[i]==d)
{
count++;
}
}
if(count>1)
{
System.out.println(d+" appears "+n+" is "+count);
}
}
Sample input:
Enter the number of words
5
Enter the words in set1:
good
bad
hello
Nothing
North
package practice;
import java.util.*;
public class RepeatedWords {
}
**********************************************************************************************
******************************************************
39) SUM OF INTEGERS AT EVEN OR ODD PLACES
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)
import java.util.*;
public class oddevenindex {
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the size of the array: ");
int n = Integer.parseInt(sc.nextLine());
String[] arr = new String[n];
int[] fin = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextLine();
}
int sum = 0;
int total =0;
if(n%2!=0)
{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
else{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
System.out.println("Output: "+total);
}
}
*****ALTERNATE METHOD*****
package practice;
import java.util.*;
public class SumOfEvenOrOdd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size");
int n = sc.nextInt();
if(n<=0){
System.out.println("Invalid size");
return;
}
System.out.println("Enter the elements:");
int sum = 0;
String str[] = new String[n];
for(int i=0;i<n;i++){
str[i] = sc.next();
}
for(int i = 0;i<n;i++){
String s = str[i];
if(s.length()%2 == 0){
for(int j = 1;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}else{
for(int j = 0;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}
}
System.out.println(sum);
}
}
**********************************************************************************************
**********************************************************************
40) SUM OF EVEN ELEMENTS
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.
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
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
package practice;
import java.util.*;
public class SumOfEvenElements {
Sample input:
Enter the string
Entertainment
Enter substring
en
Sample output: 2
package accenture;
import java.util.*;
import java.util.regex.*;
public class countofoccurencesofsubstringinastring {
}
**********************************************************************************************
******************************************************************
42) LARGEST SORTED SUBSTRING FROM INPUT STRING
package com.company;
import java.util.Scanner;
Sample input 1:
A2S3D06D13
Sample output 2:
Seat Number
D13
D14
D15
D16
D17
Sample input 2:
A2S3D02C01
Sample Output2:
Seat Number
C1
C2
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
import java.util.Scanner;
46) THREE’S
Sample input:
Enter the string
Long live
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);
}
}
**********************************************************************************************
**********************************************************************************
47) EVEN POSITION CAPS (CAPITAL LETTERS)
import java.util.Scanner;
import java.util.regex.Pattern;
***ALTERNATE METHOD***
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Input string");
String str = sc.next();
if(Pattern.matches("[A-Za-z]+",str)){
if(str.length()<5 || str.length()>20){
System.out.println(str+" is an invalid length");
return;
}
String output = str.charAt(0)+"";
for(int i=0;i<str.length()-1;i++){
if(i%2==0){
output = output+ Character.toString(str.charAt(i+1)).toUpperCase();
}
else
output = output + str.charAt(i+1);
}
System.out.println(output);
}
else{
System.out.println(str+" is an invalid input");
return;
}
}
}
**********************************************************************************************
****************************************************************************************
}
}
}
}
**********************************************************************************************
*******************************************************************************
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
if(km<=0){
System.out.println("Invalid Input");
return;
}
int sp = km/time;
if(sp>30){
int in_sp = sp-30;
System.out.println("You want to increase a speed "+in_sp+" km/hr from a normal speed");
}
else{
System.out.println("You drive a car at a normal speed");
}
}
}
***********************************************************************************************
*********************************************************************************
50) SUM OF SQUARES (RE-TEST)
Sample input
Enter the number of values
4
Enter the values
2
7
9
8
Sample output
130
Explanation
Odd numbers are 7,9 = 7*7 +9*9 = 49+81=130
import java.util.*;
public 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();
if(n<=0)
{
System.out.println(n+" is an invalid size");
return;
}
int a[]=new int[n];
int count=0;
int sum=0;
System.out.println("Enter the values");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(a[i]%2!=0)
count=count+1;
}
if(count<=0)
{
System.out.println("These "+n+" values are not a odd digit");
return;
}
else
{
for(int i=0;i<n;i++)
{
if(a[i]%2!=0)
{
sum=sum+a[i]*a[i];
}
}
System.out.println(sum);
}
}
}
JAVA END
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.
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
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
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
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
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
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();
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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
*********************************************************************************************
******************************************************************************
18. HARSHAD NUMBER (Sum of digit completely divide that number)
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)
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
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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]);
}
}
}
}
*********************************************************************************************
*********************************************************************************************
*
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
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;
}
}
}
*********************************************************************************************
*********************************************************************************************
***
import java.util.Arrays;
import java.util.Scanner;
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
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;
*******************************************************************************
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);
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
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.
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
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)
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
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
import java.util.*;
public class Program{
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
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
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 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");
}
}
If first letter is not same then reverse first word and concatenate with last word
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);
}
}
}
output
barbie=2
doll=2
bike=1
Speed Calculation
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");
}
}
}
1.Admas
Input:hththt
Output:-Team May Win
package sda;
import java.util.*;
public class Admas {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int len=str.length();
//boolean flag=false;
if(len>=2) {
boolean flag=false;
for(int i=0;i<str.length()-1;i=i+2) {
char ch=str.toLowerCase().charAt(i);
if(ch=='h') {
char ch1=str.toLowerCase().charAt(i+1);
if(ch1=='t') {
flag=true;
}else if(ch1=='h') {
//flag=false;
System.out.println("Team may lose");
return;
}
else {
System.out.println("Invalid string");
return;
}
}
else if(ch=='t') {
char ch1=str.toLowerCase().charAt(i+1);
if(ch1=='h') {
flag=true;
}else if(ch1=='t') {
//flag=false;
System.out.println("Team may lose");
return;
}
else {
System.out.println("Invalid string");
return;
}
}
}
if(flag==true) {
System.out.println("win");
}else {
System.out.println("lose");
}
}
}
}
package sda;
import java.util.*;
public class AlterNateSumDiff {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
int arr[]=new int[no];
int diff=0,sum=0;
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<no/2;i++) {
diff=arr[i]-arr[no-i-1];
sum+=diff;
}
System.out.println(sum);
}
}
3.Asscending Array:
package sda;
import java.util.*;
public class AssendingArray {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array size:");
int no=sc.nextInt();
int count=0,f=0;
if(no>=2 && no<=10) {
int arr[]=new int[no];
int copy_arr[]=new int[no];
System.out.println("Enter the array element:");
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
copy_arr[i]=arr[i];
}for(int i=0;i<no-1;i++) {
if(arr[i]==arr[i+1]) {
f++;
}
}if(f==no) {
System.out.println("Same array");
return;
}
Arrays.sort(copy_arr);
System.out.println(copy_arr);
for(int i=0;i<no;i++) {
if(copy_arr[i]==arr[i]) {
count++;
}
}
System.out.println(count);
if(count==no) {
for(int i=0;i<no;i++) {
System.out.print(arr[i]+" ");
}
System.out.print(" are in ascending order");
}else {
for(int i=0;i<no;i++) {
System.out.print(arr[i]+" ");
}
System.out.print(" are not in ascending order");
}
}else {
System.out.print("Invalid array size");
}
}
4.Bike Rent:
package sda;
import java.util.*;
public class BIkeRent {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String name=sc.nextLine();
if(name.matches("[a-zA-Z]+")) {
System.out.println("Enter the time duration");
int time=sc.nextInt();
if(time>=1 && time<=24) {
System.out.println("List of the Payment
option;");
System.out.println("1) Visa Card");
System.out.println("2) Rupay card");
System.out.println("3) Master card");
System.out.println("Choose an option\n");
//int op=sc.nextInt();
while(true) {
int op=sc.nextInt();
double bill;
double bill1;
if(op==1) {
if(time>=5) {
bill=(time*120);
bill1=(bill-bill*0.25);
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill1));
}else {
bill=time*120;
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill));
}
}else if(op==2) {
if(time>=5) {
bill=(time*120);
bill1=(bill-bill*0.17);
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill1));
}else {
bill=time*120;
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill));
}
}else if(op==3) {
bill=(time*120);
System.out.println("Dear "+name+" your
bill is "+String.format("%.2f",bill));
}
else {
System.out.println("TRy again");
//break;
//continue;
}
}
}
}
}
}
else {
if(weight<999) {
System.out.println("Invalid Weight");
return;
}
double kg=(Double.parseDouble(w))/1000;
System.out.println("Cake weight is "+kg+"kg");
System.out.println("Cake Flavor is "+flavor);
System.out.println("Cake No is "+order);
double price=450*kg;
System.out.println("Price is "+String.format("%.2f",price));
}
}
}
6.Character Addition
package sda;
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 no=sc.nextInt();
if(no>0) {
String arr[]=new String[no];
for(int i=0;i<no;i++) {
arr[i]=sc.next();
sc.nextLine();
}
int asci[]=new int[no];
for(int i=0;i<no;i++) {
int sum=0;
for(int j=0;j<arr[i].length();j++) {
if(Character.isAlphabetic(arr[i].charAt(j))) {
sum=arr[i].charAt(j)+sum;
}else {
if(Character.isDigit(arr[i].charAt(j)))
{
continue;
}
}
}asci[i]=sum;
}for(int i=0;i<no;i++) {
System.out.println(asci[i]);
}
}else {
System.out.println("Invalid input");
}
}
7.Product Case Count:
package sda;
import java.util.*;
import java.lang.*;
public class CountUpperLowerChar {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String:");
String str=sc.nextLine();
if(str.matches("[a-zA-Z]+")) {
int ucount=0,lcount=0;
for(int i=0;i<str.length();i++) {
if(str.charAt(i)>='A' && str.charAt(i)<='Z') {
ucount++;
}
else {
lcount++;
}
}
//System.out.print(ucount+" "+ lcount);
System.out.print("Product value is "+ucount*lcount);
}else {
System.out.println("Invalid String");
}
}
}
8.Non unique char:
package sda;
import java.util.*;
public class DescString {
public static void main(String[] arge) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
StringBuilder sb=new StringBuilder(str);
for(int i=0;i<sb.length()-1;i++) {
for(int j=i+1;j<sb.length();j++) {
if(sb.charAt(i)==sb.charAt(j)) {
sb.deleteCharAt(j);
j--;
}
}
}
char[] c=sb.toString().toCharArray();
Arrays.sort(c);
sb=new StringBuilder(new String(c));
sb.reverse();
System.out.println(sb);
}
9.Divide by three:
package sda;
import java.util.*;
public class DivdeByThree {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String str=sc.nextLine();
if(!str.matches("[]a-zA-Z ]+")) {
System.out.println("Invalid String");
return;
}else {
int sum=0,c=0;
for(int i=0;i<str.length();i++) {
if((i+1)%3==0) {
//System.out.println(c);
sum+=str.charAt(i);
c++;
}
}if(c==0) {
System.out.println("sum is "+sum);
}else {
System.out.println("sum is "+sum);
}
}
}
10.Fastest Runner:
Input:
Shraddha,9.45
Aanushree 9.34
Output:
Aanushree,9.34
package sda;
import java.util.*
public class FastRunner {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of runners:");
int no=sc.nextInt();
if(no>0 && no<=10) {
System.out.println("Enter the runners:");
String run[]=new String[no];
String name[]=new String[no];
String run_time[]=new String[no];
for(int i=0;i<no;i++) {
run[i]=sc.next();
sc.nextLine();
String time[]=run[i].split(",");
name[i]=time[0];
run_time[i]=time[1];
float time1=Float.parseFloat(time[1]);
if(time1<8.00 || time1>12.00) {
System.out.println(time[1]+" is an invalid
input");
return;
}
}
String min=run_time[0];
int count=0;
float min1=Float.parseFloat(min);
for(int j=0;j<no;j++)
{
float val=Float.parseFloat(run_time[j]);
if(val<=min1)
min1=val;
}
for(int i=0;i<no;i++) {
float val=Float.parseFloat(run_time[i]);
if(val==min1) {
System.out.println(name[i]);
count++;
}
}
if(count==no) {
System.out.println("All the runner have same
time");
return;
}
}else {
System.out.println("Invalid");
}
}
}
11.Fun Count
package sda;
import java.util.*;
public class FunCount {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of element in array");
int no=sc.nextInt();
int count=1;
String arr[]=new String[no];
//String count1[]=new String[no];
for(int i=0;i<no;i++) {
arr[i]=sc.next();
sc.nextLine();
}for(int i=0;i<no;i++) {
for(int j=i+1;j<no;j++) {
if(arr[i].equals(arr[j])) {
count++;
arr[j]="";
}
}if(arr[i]!="") {
System.out.println(arr[i]+"="+count);
}
}
12.Gender Count:
package sda;
import java.util.*;
public class GenderCount {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
sc.close();
int mcount=0,fcount=0;
for(int i=0;i<str.length();i++) {
char ch=str.toLowerCase().charAt(i);
if(ch=='m') {
mcount++;
}else if(ch=='f') {
fcount++;
}else {
System.out.println("Invalid input");
return;
}
}
System.out.println(mcount+" Male");
System.out.println(fcount+" Female");
}
13.Harshad Number
package sda;
import java.util.*;
public class HarShadNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number:");
int no=sc.nextInt();
if(no<=0) {
System.out.println("Invalid Input");
System.exit(0);
}
int rem=0;
int arr[]=new int[no];
int arr1[]=new int[no];
System.out.println("Enter the Element:");
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
arr1[i]=arr[i];
}
int sum1[]=new int[no];
for(int i=0;i<no;i++) {
int sum=0;
while(arr[i]>0) {
rem=arr[i]%10;
sum=sum+rem;
arr[i]/=10;
sum1[i]=sum;
//sum=0;
}}
for(int i=0;i<no;i++) {
if(arr1[i]%sum1[i]==0) {
System.out.println(arr1[i]);
//System.out.println(sum1[i]);
}
}
}
}
14.Longest String:
package sda;
import java.util.*;
public class LongestString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string:");
String str=sc.nextLine();
int findex=0,lindex=0,length=0;
for(int i=0;i<str.length();i++) {
int asci=str.charAt(i);
System.out.println(str.charAt(i)+"1"+asci);
int count=1;
for(int j=i+1;j<str.length();j++) {
if(str.charAt(j)==++asci) {
System.out.println(str.charAt(j)+"2"+asci);
count++;
continue;
}else {
if(count>length) {
System.out.println(count+"3");
findex=i;
lindex=j;
length=count;
break;
}
}
}
}System.out.println(str.substring(findex,lindex));
}
}
15.Maximun No ADDition
package sda;
import java.util.*;
public class MaxAdd {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of element in array:");
int no=sc.nextInt();
int max=0;
System.out.println("Enter the of element of array:");
int arr[]=new int[no];
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
if(max<arr[i]) {
max=arr[i];
}
}
System.out.println(max);
}
package sda;
import java.util.*;
public class MaxIndex {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of elements in arrays: ");
int no=sc.nextInt();
if(no<5) {
System.out.println(no+" is too low");
return;
}
if(no>10) {
System.out.println(no+" is too high");
}
int arr[]=new int[no];
int max=arr[0];
int index=0;
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
if(arr[i]>max) {
max=arr[i];
System.out.println(max+" "+arr[i]);
index=i;
System.out.println(index);
}
}
for(int i=0;i<no;i++) {
if(i!=index) {
System.out.println(arr[i]+index);
}else {
System.out.println(arr[i]);
}
}
}
}else {
System.out.println(no+" exceeds limit");
}
}else {
System.out.println(no+" is too small");
18.Mega Mart
package sda;
import java.util.*;
public class MeghaMart {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the customer id:");
String str=sc.nextLine();
String name=str.substring(0,4);
String mid=str.substring(str.length()-3,str.length());
System.out.println(mid);
String cus_type=str.substring(4,str.length()-3);
System.out.println(cus_type);
//System.out.println(mem_id);
String sname="Mega";
if(!name.equals(sname)) {
System.out.println("Invalid shop name");
return;
}else if(!mid.contains("[0-9]+")){
System.out.println("Invalid member id");
}
else if(mid.contains("[0-9]+")){
int mem_id=Integer.parseInt(str.substring(str.length()-
3,str.length()));
19.MRP
package sda;
import java.util.*;
}
else if(mrp1>=51 && mrp1<=500) {
dis=mrp1-15;
}
else if(mrp1>=501 && mrp1<=5000)
{
dis=mrp1-105;
}
else {
dis=mrp1-1005;
}
}
}
int temp=str.charAt(str.length()-1);
System.out.print(temp+" "+(char)temp);
for(int j=0;j<seq;j++) {
temp=temp-sub;
System.out.print(temp+" "+(char)temp);
if(temp>122) {
// System.out.print(temp+"
"+(char)temp);
temp=temp-26;
System.out.print((char)temp);
}else if(temp<97) {
//System.out.print(temp+"
"+(char)temp);
temp=temp+26;
System.out.print((char)temp);
}else {
System.out.print((char)temp);
}
}
}
package sda;
import java.util.*;
public class NoFind {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbe");
int n=sc.nextInt();
System.out.println("Enter number to be search");
int sea=sc.nextInt();
int count=0;
while(n!=0) {
int rem=n%10;
//System.out.println(rem);
if(rem==sea) {
count++;
}
n=n/10;
//System.out.println(n);
}
System.out.println(count);
}
22.Numerology
package sda;
import java.util.*;
public class Numerology{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your Name");
String str1=sc.next();
String str=str1.toUpperCase();
int value=0,sum=0;
if(str.matches("[A-Z]+")){
for(int i=0;i<str.length();i++){
value=(int)(str.charAt(i));
sum+=value;
}
if(sum%2==0 && sum%3==0) {
System.out.println(sum+"\n"+str1+" is a Numerology
name");
}
else{
System.out.println(sum+" is not a Numerology name");
return;
}
}
else {
System.out.println(str1+" is an invalid name");
}
}
}
24.Customer Offer
package sda;
import java.util.*;
public class OfferCustomer {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Invalid string");
int no=sc.nextInt();
//sc.nextLine();
String str[]=new String[no];
// String str1[]=new
if(no>3) {
System.out.println("Customer count is too low");
return;
} if(no>0) {
System.out.println("Invalid input");
return;
}if(no<50){
System.out.println("Customer count is too high");
return;
}for(int i=0;i<3;i++) {
str[i]=sc.nextLine();
}
System.out.println("Enter plan id to be searched:");
String pid=sc.next();
for(int i=0;i<no;i++) {
String arr[]=str[i].split(":");
if(pid.equals(arr[1])){
System.out.println(arr[3]+" "+arr[2]);
}
}
}
}
25.Prime Number
package sda;
/*import java.util.*;
public class PrimeNo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int no1=sc.nextInt();
int no2=sc.nextInt();
//int f=0;
if(no1<0 || no2<0) {
System.out.println("Negative integer can not be
prime");
}else if(no1>no2) {
System.out.println("Invalid input");
}else if (no1==0 && no2==1) {
System.out.println("There are no prime number in the
range 0 to 1");
}
else {
for(int i=no1;i<=no2;i++) {
int f=0;
for(int j=1;j<=i;j++) {
if(i%j==0) {
f++;
}
}if(f==2) {
System.out.println(i);
}
}
}
sc.close();
}
}*/
import java.util.*;
public class PrimeNo {
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;
}
}
}
package sda;
import java.util.*;
public class PrimeProduct {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of element in array");
int no=sc.nextInt();
if(no>0) {
System.out.println("Array element");
int pd=1,f=0,f1=0;
int arr[]=new int[no];
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
if(arr[i]>0) {
for(int j=2;j<arr[i]/2;j++) {
if(arr[i]%j==0)
{
f1=1;
}
}if(f1==0) {
System.out.println(arr[i]);
pd=arr[i]*pd;
f++;
}
}else {
System.out.println(arr[i]+" Invalid array
element");
}
}if(f!=0) {
System.out.println(pd);
}else {
System.out.print("No element found in ");
for(int k=0;k<no;k++)
System.out.print(arr[k]);
}
}else {
System.out.println(no+ " Invalid array size");
}
}
27.Public Distrubution
package sda;
import java.util.*;
public class PublicDistrubution {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
if(str.length()<2 || str.length()>5) {
System.out.println("invalid input");
return;
}
int amount,total=0;
if(str.matches("[ORWKS]+")) {
for(int i=0;i<str.length();i++) {
if(str.charAt(i)=='O') {
amount=12*2;
total=total+amount;
}else if(str.charAt(i)=='K') {
amount=18*2;
total=total+amount;
}else if(str.charAt(i)=='W') {
amount=22*2;
total=total+amount;
}else if(str.charAt(i)=='R') {
amount=15*2;
total=total+amount;
}else if(str.charAt(i)=='S') {
amount=21*2;
total=total+amount;
}
}
System.out.println(total);
}else {
System.out.println("Invalid input");
}
}
}
28.Reverse String
package sda;
import java.util.*;
public class ReserveString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
String str1[]=str.split(" ");
int len=str1.length;
String rev = "";
//System.out.println(len);
int f=0;
char ch=str1[0].toUpperCase().charAt(0);
if(len>=3 ) {
for(int i=0;i<len;i++)
if(str1[i].matches("[a-zA-Z]+")) {
if(ch==str1[i].toUpperCase().charAt(0)) {
f++;
}
}else {
System.out.println("Invalid word");
return;
}
if(f==len) {
String s1=str1[len-1];
int l1=s1.length();
for(int i=l1-1;i>=0;i--) {
rev=rev+s1.charAt(i);
}
System.out.println(rev+str1[0]);
}else {
String s1=str1[0];
for(int i=s1.length()-1;i>=0;i--) {
rev=rev+s1.charAt(i);
}
System.out.println(rev+str1[len-1]);
}
}else {
System.out.println("Invalid Sentence");
}
}
}
29.Reverse Prime number
package sda;
import java.util.*;
public class ReversePrime {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of values");
int no=sc.nextInt();
if(no>0) {
int c=0;
int arr[]=new int[no];
System.out.println("Enter the numbers");
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<no;i++) {
int f=0,f2=0;
//System.out.println(arr[i]);
if(arr[i]>9) {
for(int j=2;j<arr[i]/2;j++) {
if(arr[i]%j==0) {
f=1;
}
}if(f==0) {
//f1=1;
// System.out.println(arr[i]);
int sum=0;
int prime=arr[i];
while(prime!=0)
{
int rem=prime%10;
sum=rem+sum*10;
prime/=10;
}
//System.out.println(sum);
for(int k=2;k<sum/2;k++) {
if(sum%k==0) {
f2=1;
}
}if(f2==0) {
System.out.println(arr[i]);
}
}else {
c++;
}
}else {
System.out.println(arr[i]+" is not valid");
return;
}
}if(c==no) {
System.out.println(no+" is not contain any
reverse prime");
return;
}
}else {
System.out.println(no+" is not valid");
}
}
}
30.Reverse Palindrome
package sda;
import java.util.*;
public class RevPalin {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
String str1[]=str.split(" ");
String rev[]=Arrays.copyOf(str1, str1.length);
for(int i=0;i<str1.length;i++) {
StringBuilder sb=new StringBuilder(rev[i]);
sb.reverse();
rev[i]=sb.toString();
System.out.print(rev[i]+" ");
}
for(int i=0;i<str1.length;i++)
if(rev[i].equals(str1[i]) && str1[i].length()>1) {
System.out.println(rev[i]);
}
31.
Same String
Input:Entertaiment
Substring: en
Output:2
package sda;
import java.util.*;
for(int i=0;i<str.length()-1;i++) {
String str1=str.substring(i,i+sub_str1.length());
System.out.println(str1);
if(sub_str1.equals(str1)) {
count++;
}
}
if(count!=0) {
System.out.println(sub_str1+" found in given string is
"+count);
}
else {
System.out.println("No matching found");
}else {
System.out.println("Invalid string");
package sda;
import java.util.*;
public class SecondLast {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of element in array");
int n=sc.nextInt();
System.out.println("Enter the elements in array");
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println(a[n-2]);
}
}
33.Secreate Code
package sda;
import java.util.*;
public class SecreateCode {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name of the child");
String str=sc.nextLine();
int len=str.length();
if(len>=4) {
if(str.matches("[a-zA-Z]+")) {
System.out.println("Enter the birthdate of the
child");
int no=sc.nextInt();
if(no>=1 && no<=31) {
System.out.println(str.substring(0,4)+"@"+no);
}else {
System.out.println("Secreat code can not be
genrated");
return;
}
}else {
System.out.println("Secreat code can not be
genrated");
return;
}
}else {
System.out.println("Secreat code can not be genrated");
return;
}
}
}
34.
Compare Sum and Product
Output:35,168
package sda;
import java.util.*;
public class SumCompareProduct {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of size of First Arrya:");
int no1=sc.nextInt();
System.out.println("Enter no of size of second Arrya:");
int no2=sc.nextInt();
int arr[]=new int[no1];
int arr1[]=new int[no2];
int sum1=1,rem=0,sum2=0,count=0;
if(no1>0 && no2>0) {
if(no1==no2) {
System.out.println("enter the element in first
array");
for(int i=0;i<no1;i++ ) {
arr[i]=sc.nextInt();
}
for(int i=0;i<no1;i++) {
int a1[]=new int[no1];
a1[i]=arr[i];
while(a1[i]!=0) {
rem=a1[i]%10;
sum1=rem*sum1;
a1[i]=a1[i]/10;
}
pa[i]=sum1;
sum1=1;
System.out.println(pa[i]);
}
System.out.println("enter the element in second
arrays");
for(int j=0;j<no2;j++) {
arr1[j]=sc.nextInt();
}
for(int j=0;j<no2;j++) {
int a2[]=new int[no2];
a2[j]=arr1[j];
while(a2[j]!=0) {
rem=a2[j]%10;
sum2=rem+sum2;
//sa[j]=sum2;
a2[j]=a2[j]/10;
}
sa[j]=sum2;
sum2=0;
System.out.println(sa[j]);
}
for(int k=0;k<no1;k++) {
if(pa[k]==sa[k]) {
System.out.println(arr[k]+","+arr1[k]);
count++;
}
}if(count==0) {
System.out.println("No match Found");
}else {
System.out.println("Array size is
diffrent");
}
}else {
System.out.println("Invalid array size");
}
}
}
36.Toy shop
package sda;
import java.util.*;
public class ToyShop {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of months");
int month=sc.nextInt();
if(month>0) {
System.out.println("Enter the number of toys purchased
monthly");
int toys[]=new int[month];
for(int i=0;i<month;i++) {
toys[i]=sc.nextInt();
if(toys[i]<10) {
System.out.println(toys[i]+" is an invalid
number of toy");
return;
}
}
int max=toys[0];
for(int i=1;i<month;i++) {
if(max<=toys[i]) {
max=toys[i];
}
}
for(int i=0;i<month;i++) {
if(toys[i]==max) {
System.out.println("month "+(i+1));
}
}
}else {
System.out.println("Invalid month");
}
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
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
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
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
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
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;
}
}
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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
*********************************************************************************************
******************************************************************************
18. HARSHAD NUMBER (Sum of digit completely divide that number)
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.VIDEO GAME (PS1)
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
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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
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;
}
}
}
*********************************************************************************************
*********************************************************************************************
***
import java.util.Arrays;
import java.util.Scanner;
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
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;
*******************************************************************************
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);
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
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.
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;
Gender Determination :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int count=0,count1=0;
if(!(s.matches("[mfMF]+")))
{
System.out.println("Invalid Input");
return;
}
else
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='m' ||s.charAt(i)=='M')
count++;
else
count1++;
}
System.out.println(count+" "+"Male");
System.out.println(count1+" "+"Female");
}}
LCM OF DAY INTERVAL/LUNCH/DINNER TOGETHER :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day interval of person 1:");
int a=sc.nextInt();
System.out.println("Enter the day interval of person 2:");
int b=sc.nextInt();
if(a<=0 || b<=0)
{
System.out.println("Given interval is not valid");
return;
3
}
int lcm;
lcm=(a>b)?a:b;
while(true){
if(lcm%a==0 && lcm%b==0){
System.out.println("person 1 and person 2 will have
their dinner on day "+lcm);
break;
}
lcm++;
}
}}
import java.util.*;
int n=sc.nextInt();
int count=0;
if(n<1 || n>10){
System.exit(0);
else{
for(int i=0;i<n;i++){
int b=sc.nextInt();
a1[i]=b;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int c=sc.nextInt();
a2[i]=c;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int mul=1,r=0,x=a1[i];
while(x>0){
r=x%10;
mul*=r;
5
x=x/10;
for(int j=0;j<n;j++){
int sum=0,p=0,y=a2[j];
while(y>0){
p=y%10;
sum+=p;
y=y/10;
if(mul==sum){
count++;
System.out.print(a1[i]+","+a2[j]);
System.out.println(" ");
}}}
if(count==0){
System.exit(0);
}}}}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
int index=0;
6
int count=0;
while((index=s1.indexOf(s2,index))!=-1){
index=index+1;
count+=1;
}
System.out.println(count);
}
}
Fun Count
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=0){
System.out.println("Invalid Input");
return;
}
if(n>15){
System.out.println("Number exceeds the limit");
return;
}
Map<String,Integer> a=new HashMap<>();
for(int i=0;i<n;i++){
String s=sc.next().toLowerCase();
if(a.containsKey(s))
a.put(s,1+a.get(s));
else
a.put(s,1);
}
Map<String,Integer> tm=new TreeMap<>(a);
for(Map.Entry<String,Integer>data:tm.entrySet()){
System.out.println(data.getKey()+"= "+data.getValue());
}
}
}
7
SPEED ESTIMATION :
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the distance in kilometer");
int km=sc.nextInt();
if(km<0) {
System.out.println("Invalid Input");
return;
}
System.out.println("Enter the time to reach in hours");
int hours=sc.nextInt();
if(hours<0){
System.out.println("Invalid Input");
return;
}
int speed=km/hours;
if(speed<30){
System.out.println("You drive a car at normal speed");
return;
}
int inc=speed-30;
System.out.println("You want to increase a speed "+inc+"
km/hr from a normal speed");
}}
Prime Number
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
int count=0;
8
if(a<0 || b<0){
return;
for(int i=a;i<=b;i++){
count = 0;
for(int j=2;j<=i;j++){
if(i % j == 0)
count = count+1;
if(count == 1)
System.out.print(i+" ");
if(count!=1)
Theatre
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(s.length()==6)
{
9
String a=s.substring(0,1);
if(a.matches("[NEWS]+"))
{
String b=s.substring(1,2);
if(b.matches("[ABCDE]+"))
{
String c=s.substring(2,3);
if(c.matches("[1-4]+"))
{
String d=s.substring(3,6);
int num=Integer.parseInt(d);
if(num>0 && num<500)
{
System.out.println("Name "+a);
System.out.println("Block "+b);
System.out.println("Floor number "+c);
System.out.println("Seat number
"+num);
}
else
System.out.println("Invalid Seat number");
}
else
System.out.println("Invalid Floor number");
}
else
System.out.println("Invalid Block");
}
else
System.out.println("Invalid Name");
}
else
System.out.println("Invalid size");
}}
10
Customer details
import java.util.Scanner;
}
}
}
}
Team Win/Loose
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.next();
int count=0;
if(str.length()<3) {
System.out.println("Invalid input");
return;
}
if(str.matches("[htHT]+")) {
String s=str.toLowerCase();
for(int i=0;i<s.length()-2;i++){
if(s.charAt(i)!=s.charAt(i+1) &&
s.charAt(i)==s.charAt(i+2))
count++;
}
if(count==s.length()-2)
System.out.println("Team will win");
else
System.out.println("Team may loose");
}
else {
System.out.println("Invalid Input");
return;
}
}}
Holiday Fun
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
12
int sum=0,sum1=0;
int n=sc.nextInt();
if(n>0){
String[] a=new String[n];
for(int i=0;i<n;i++){
a[i]=sc.next();
}
for(int i=0;i<n;i++){
int len=a[i].length();
if(len%2==0)
{
for(int j=1;j<len;j++){
sum=sum+Integer.parseInt(String.valueOf(a[i].charAt(j)));
j++;
}}
else
{
for(int k=0;k<len;k++){
sum1=sum1+Integer.parseInt(String.valueOf(a[i].charAt(k)));
k++;
}}
}
System.out.println(sum+sum1);
}
else
System.out.println("Invalid size");
}}
Reverse Prime
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0,c=0,f=0,r=0,prime=0;
int n=sc.nextInt();
if(n>0) {
int[] a=new int[n];
13
for(int i=0;i<n;i++) {
int num=sc.nextInt();
if(num>9)
a[i]=num;
else {
System.out.println("Provided "+num+" is not valid");
return;
}}
for(int i=0;i<n;i++) {
count=0;
for(int j=2;j<a[i];j++)
{
if(a[i]%j==0)
count++;
}
if(count==0) {
prime++;
f=a[i];
r=0;
while(a[i]!=0) {
r=r*10;
r=r+a[i]%10;
a[i] =a[i]/10;
}
for(int k=2;k<r;k++) {
if(r%k==0)
c++;
}
if(c==0)
System.out.println(f);
}}}
else {
System.out.println(n+" is less than a limit");
return;
}
if(prime==0)
System.out.println(n+" values are not prime numbers");
}}
14
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0;
System.out.println("Enter the size of array ");
int n=sc.nextInt();
int[] sum=new int[n];
if(n>=1 && n<=10)
{
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();
int[] b=new int[n];
System.out.println("Enter the elements of second array ");
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]%2==0 && b[i]%2==0)
{
count++;
sum[i]=a[i]+b[i];
}
else
sum[i]=0;
}
if(count==0)
System.out.println("There are no even elements");
else
for(int result:sum)
System.out.println(result);
}
else
System.out.println("Invalid");
}}
15
Groceries Bill
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int a=0,b=0,c=0,d=0,count=0;
if(s.matches("[ORWKorwk]+")) {
for(int i=0;i<s.length();i++) {
for(int j=0;j<s.length();j++) {
if(s.charAt(i)==s.charAt(j))
count++;
}}
if(count==s.length()) {
for(int k=0;k<s.length();k++)
{
if(s.charAt(k)=='O')
a=4*20;
else if(s.charAt(k)=='R')
b=3*5;
else if(s.charAt(k)=='W')
c=2*10;
else
d=5*15;
}
System.out.println(a+b+c+d);
}
else
System.out.println("Duplicates are not allowed");
}
else
System.out.println("Invalid");
}}
16
Cake Order
import java.util.*;
if (cd.matches("[a-zA-Z0-9]*")) {
String w=cd.substring(0, 4);
if(!w.matches("[0-9]*")){
System.out.println("Invalid weight");
return;
}
float wg=Integer.parseInt(w);
if (wg>999) {
float fwg=wg/1000;
double weight= (Math.round(fwg * 100.0) / 100.0);
String o=cd.substring(cd.length()-3);
if (!o.matches("[0-9]*")) {
System.out.println("Invalid order number");
return;
}
int on=Integer.parseInt(o);
if(on>99) {
String flavour=cd.substring(4,cd.length()-3);
double cakePrice=450*weight;
17
}
1. Maze Runner
Amy wants to create a game. Her game had 2 players, according to the movement of the players
points will vary. According to the direction, characters will be assigned. Only four characters can
be assigned, L-left, R-right, B-bonus, F-foul, if the direction is left add 50 points, if the direction is
right subtract 50 points, if there is a bonus double the points or if there is foul reduce the total
points to its half.
So according to the string calculate the points and compare it among the players. At the end of
the game, if the player1 have scored more points than player 2, print “Player 1 Wins” or else print
“Player 2 Wins”. At the end of the game, if both have secured equal points print “Draw Nobody
Wins” .Initial points are awarded to the players and the score calculation is done with that initial
points.
Note: Characters L,R,B,F are valid. If there is any other character present in the string print
“Invalid Input”. Number of characters must be same between the players or else print “Invalid
Input”. Initial points can be between 1000 to 5000 or else print “Invalid Input”.
Sample input 1:
1200
LLRRF
1000
RRLFB
Sample output 1:
Player 2 Wins
Sample input 2:
7000
Sample output 2:
Invalid Input
Sample input 3:
1900
LLRKF
Sample output 3:
Invalid Input
2. Holiday Trip:
Ana wants to book a ticket to Mumbai. She want to call few of her friends with her. Help her to
calculate the ticket amount by which they can go for their trip within their budget. There are
different flights like Air India, Spice Jet, Go Air, IndiGo.
According to the flight name travel cost varies. Each flight will have divisions like Economy class,
Travel class and Business class. Cost of the ticket will vary for each class. If the passenger pays
the ticket cost using card there is a percentage of reduction in the cost of the ticket. If the number
of passengers are more than 5, 10% discount is given and if it is more than 10, 15% discount is
given for IndiGo and Spice Jet alone from the final amount.
Air India:
Travel class:3200
Economy class:6950
Business class:10,340
Card Payment:10.5%
Spice Jet:
Travel class:3450
Economy class:7945
Business class:9500
Card Payment:7.5%
Go Air:
Travel class:3300
Economy class:7250
Business class:9890
Card Payment:9.5%
IndiGo:
Travel class:3490
Economy class:7560
Business class:9990
Card Payment:8.5%
Sample input 1:
Air India
Travel class
Card Payment:
Sample output 1:
22912
Explanation: (3200*8)-(3200*8*(10.5/100))
Write a java program to get two input from the user, the first input will be a string holding time in
24 hours format(HH:MM) and the second input will be an integer that holds the time interval in
minutes (MM). And display the time at which the next notifications has to be sent for that day. If
the time to send the next notification exceeds for the day display “Notification cannot be sent for
the day”.
If the range of hours (HH) in time should be between 00 and 23 (both inclusive) and the range of
minutes (MM) should be between 00 and 59(both inclusive). If not display “Input is not valid” and
terminate the program.
Sample Input 1:
22:00
05
Sample Output 1:
22:05
Sample Input 2:
23:45
20
Sample Output 2:
Sample Input 3:
25:50
Sample Output 3:
23:50
60
Sample Output 4:
David fueled his vehicle in a fuel station. He remembered that he asked the filling person to fill
the fuel for certain amount in rupees. But he don’t know the quantity of fuel in liters. Help him to
calculate the fuel in Liters from amount in Rupees by getting the rate per liter as input.
Note that, both the inputs must be a positive number. Otherwise print “Invalid Input”
The result should be in two decimal places. To get two decimal places refer the below-mentioned
print statement :
float cost=1105.00f;
System.out.printf(“%.2f”,cost);
Sample Input 1:
1000
89.35
Sample Output 1:
11.19 Liters
Sample Input 2:
-1000
Sample Output 2:
Invalid Input
Nancy was playing with her daughter, they were playing with words. That day her daughter was
taught vowels in her class. So they were playing with vowels. Nancy asked her daughter to
remove the vowels from the word, if they are present in continuous pattern alone.
Note: The word should not have more than 8 letters if so print “Word length exceeds the limit”
and less than 3 letters, if so print “Word length is too short”. Word cannot have numbers or
special character, if so print “Invalid Input”.
Sample input 1:
solution
Sample output 1:
solutn
Sample input 2:
hi
Sample output 2:
Sample input 3:
absurdity
Sample output 3:
Sample input 4:
Enter the word:
ba&ery
Sample output 4:
Invalid Input
6. MAC Validation
A MAC (Media Access Control) address is the unique identifier that is assigned by the
manufacturer to a piece of network hardware.
A MAC address consists of six sets of two characters, each separated by a colon.
00:1B:44:11:3A:B7 is an example of a MAC address. Each character should hold only
hexadecimal values ( 0 to 9 and A to F).
Write a java program to check the given string is a valid MAC address or not, if the given string is
valid then display “Valid MAC address” else display “Invalid MAC address”.
Note: If the given string contains less than six sets of two characters, each separated by a colon
display “String is too short”. If the given string contains more than six sets of two characters,
each separated by a colon display “String is too long”. If the given string contains any other
symbols other than colon (:) display “Invalid Input”.
Assumption: Each set in the given string will have only two characters and alphabets in the
Upper case.
Sample Input 1:
56:41:AB:5A:A5:60
Sample Output 1:
Sample Input 2:
62:55:VB:ZZ:A5:S6
Sample Output 2:
Sample Input 3:
62:55:CB
Sample Output 3:
Sample Input 4:
62:55:CB:62:55:CB:23
Sample Output 4:
Sample Input 5:
62&55*CB%62:55@CB
Sample Output 5:
Invalid Input
7. Unique products
John runs a department store and has multiple items in it. Each items will have a 3 digit bar code
ID to identify the items. He wants to segregate items which has Unique number in the 3 digit bar
code ID. A Unique number is a positive integer with no duplicate digits. For example 135, 214 are
all unique numbers whereas 311, 300 are not.Help him by your java skills.
Note: The number of items should be between the range 5 and 20 (both inclusive) else display
“Invalid item count” and terminate the program. The bar code ID of each item should be only a 3
digit number, if not display “Invalid bar code ID” and terminate the program. If there is no bar
code ID with Unique number then display “There are no item with Unique number in the bar code
ID”.
Sample Input 1:
10
111
222
123
125
654
354
844
141
200
201
Sample Output 1:
123
125
654
354
201
Sample Input 2:
111
222
110
333
100
Sample Output 2:
25
Sample Output 3:
Sample Input 4:
111
1000
Sample Output 4:
8.Toggle word
Reema is an English teacher, she teaches upper case and lower case letters to her students.
She wanted to test her students, whether her students have understood the concept. She wanted
to conduct it as a game. If the students perform well, she would like to present gifts. Help her to
conduct the game using Java program.
If a word is given, each alternate character in the word should be converted in to upper case ie,
In the given word, the first character should start with upper case followed by each alternate
character as upper case.
For example:
Note: Word should not have more than 8 letters if so print “Word length exceeds the limit” and
less than 3 letters, if so print “Word length is too short”. Word cannot have numbers or special
character, if so print “Invalid Input”.
Sample input 1:
aviation
Sample output 1:
AvIaTiOn
Sample input 2:
it
Sample output 2:
Sample input 3:
abundance
Sample output 3:
Sample input 4:
m@ngo
Sample output 4:
Invalid Input
Write a program to find the product of the prime digits in the given input number.
If the input number is negative or greater than 32767 Print it as "Invalid Input".
If the input number does not have any prime digits,print it as “No Prime Digit Found”
If the input number has single prime digit,then print that prime digit.
Please note that '1' and '0' are neither prime nor composite.
Input and Output Format:
Sample Input 1:
2017
Sample Output 1:
14
Explanation:
Number '2' and '7' are the prime digits in the given input and number '1' and '0' are neither prime
nor composite hence the product of prime digits will results in 14(2*7).
Sample Input 2:
483
Sample Output 2:
Sample Input 3:
-67
Sample Output 3:
Invalid Input
James needs to find the estimated delivery time of his parcel. Get the distance(in Km) between
the seller and buyer as input. Consider that the delivery vehicle can travel with 10 meter per
second as average speed. Calculate the delivery time and display the output in Minutes (rounded
value) as given in sample data.
Formula: T = D / S
(1Km=1000m, 1min=60sec).
Note that, the input should be in Kilometers and output should be in minutes.
Write a Java program to calculate the above. Input must be a positive number, otherwise print
“Invalid Input”.
The result should be in two decimal places. To get two decimal places refer the below-mentioned
print statement :
float cost=1105.00f;
System.out.printf(“%.2f”,cost);
Sample Input 1:
100
Sample Output 1:
Estimated Time:
166.67 Minutes
Sample Input 2:
-50
Sample Output 2:
Invalid Input
1. Maze Runner
Solution:
import java.util.*;
class Main
int points1=sc.nextInt();
if(points1<1000 || points1>5000)
System.out.println("Invalid Input");
else
String s1=sc.next();
int flag=0;
char ch1[]=s1.toCharArray();
for(char c:ch1)
//System.out.println(c);
flag++;
}
}
if(flag!=s1.length())
System.out.println("Invalid Input");
else
int points2=sc.nextInt();
if(points2<1000 || points2>5000)
System.out.println("Invalid Input");
else
String s2=sc.next();
if(s1.length()!=s2.length())
System.out.println("Invalid Input");
else
int flag1=0;
char ch2[]=s2.toCharArray();
for(char c:ch2)
{
if(c=='L' ||c=='R' || c=='F' ||c=='B')
flag1++;
if(flag1!=s2.length())
System.out.println("Invalid Input");
else
for(char c:ch1)
if(c=='L')
points1+=50;
if(c=='R')
points1-=50;
if(c=='B')
points1*=2;
if(c=='F')
{
points1/=2;
for(char c:ch2)
if(c=='L')
points2+=50;
if(c=='R')
points2-=50;
if(c=='B')
points2*=2;
if(c=='F')
points2/=2;
if(points1>points2)
System.out.println("Player 1 Wins");
else if(points1<points2)
{
System.out.println("Player 2 Wins");
else
2. Holiday Trip:
Solution:
import java.util.*;
String fname=sc.nextLine();
System.out.println("Enter the class:");
String fclass=sc.nextLine();
int no=sc.nextInt();
System.out.println("Card Payment:");
char pay=sc.next().charAt(0);
int cost=0;
switch(fname){
// System.out.println("Hai1111");
switch(fclass){
//System.out.println("Hai");
cost=3200;
break;
cost=6950;
break;
cost=10340;
break;
}
}
break;
switch(fclass){
if(no<=5){
cost=3450;
else if(no>5&&no<=10){
cost=(int)(3450*0.9);
else{
cost=(int)(3450*0.85);
break;
if(no<=5){
cost=7945;
else if(no>5&&no<=10){
cost=(int)(7945*0.9);
else{
cost=(int)(7945*0.85);
}
break;
if(no<=5){
cost=9500;
else if(no>5&&no<=10){
cost=(int)(9500*0.9);
else{
cost=(int)(9500*0.85);
break;
break;
switch(fclass){
cost=3300;
break;
break;
cost=9890;
break;
break;
case "IndiGo":{
switch(fclass){
if(no<=5){
cost=3490;
else if(no>5&&no<=10){
cost=(int)(3490*0.9);
else{
cost=(int)(3490*0.85);
break;
if(no<=5){
cost=7560;
else if(no>5&&no<=10){
cost=(int)(7560*0.9);
else{
cost=(int)(7560*0.85);
break;
if(no<=5){
cost=9990;
else if(no>5&&no<=10){
cost=(int)(9990*0.9);
else{
cost=(int)(9990*0.85);
break;
break;
int res=cost*no;
if(pay=='Y')
switch(fname){
res=(int)(res*0.895);
break;
res=(int)(res*0.925);
break;
res=(int)(res*0.905);
break;
case "IndiGo":
res=(int)(res*0.915);
break;
System.out.println(res);
}
Solution:
import java.util.Scanner;
String s=sc.next();
String ch[]=s.split(":");
for(int i=0;i<2;i++)
int m1=Integer.valueOf(ch[i]);
m[i]=m1;
{ int n=sc.nextInt();
int k=m[1]+n;
if(k>59)
if(l>23)
else
{m[0]=l;
count++;}
else
{ m[1]=k;
count++; }
System.out.println(m[0]+":0"+m[1]);
System.out.println(m[0]+":"+m[1]);
else
else
}
4.Fuel Quantity Calculation
Solution:
import java.util.*;
int rs=read.nextInt();
if(rs<1)
System.out.println("Invalid Input");
else
float liter=read.nextFloat();
if(liter<0)
System.out.println("Invalid Input");
else
float petrol=rs/liter;
System.out.printf("%.2f Liters",petrol);
}
}
}}
Solution:
import java.util.Scanner;
for(int i=0;i<s.length();i++)
if(Character.isLetter(s.charAt(i)))
count++;
if(count==s.length())
for(int i=0;i<s.length();i++)
{ if(i<s.length())
if(((s.charAt(i)=='a')||(s.charAt(i)=='e')||(s.charAt(i)=='i')||(s.charAt(i)=='o')||(s.charAt(i)=='u')
)&&
((s.charAt(i+1)=='a')||(s.charAt(i+1)=='e')||(s.charAt(i+1)=='i')||(s.charAt(i+1)=='o')||(s.char
At(i+1)=='u')))
{i++;
continue;
else
System.out.print(s.charAt(i));
else
System.out.print(s.charAt(i));
//System.out.println(s);
else
System.out.println("Invalid Input");
if(s.length()>8)
if(s.length()<3)
6. MAC Validation
Solution:
import java.lang.*;
import java.util.*;
String s=sc.nextLine();
int len=s.length();
int dc=0,cc=0,colonc=0,flag=0;
if(len==17){
char[] a=s.toCharArray();
for(char b:a){
if(Character.isLetter(b)){
if(b>=65&&b<=70)
cc++;
if(Character.isDigit(b)){
dc++;
if(b==':'){
colonc++;
if(!(Character.isLetter(b)||Character.isDigit(b)||b==':')){
flag++;
break;
}
}
if(flag==1){
System.out.println("Invalid Input");
// System.exit(0);
if(flag==0){
int res=cc+dc+colonc;
if(res==17){
else{
else if(len<17){
else{
}
}
7. Unique products
Solution:
import java.util.Scanner;
int item_count=sc.nextInt();
if(item_count<5 || item_count>20)
else
int flag=1;
int count=0;
for(int i=0;i<item_count;i++)
int it=sc.nextInt();
if(it<100 || it>999)
flag=0;
break;
else
item[i]=it;
if(flag!=0)
for(int j:item)
String s="";
s=s.valueOf(j);
if(s.charAt(0)!=s.charAt(1)&&s.charAt(0)!=s.charAt(2)&&s.charAt(2)!=
s.charAt(1))
System.out.println(j);
count++;
}
if(count==0)
else
8.Toggle word
Solution:
import java.util.*;
class Main
String s=sc.next();
int length=s.length();
if(length<3)
else if(length>8)
else
char c[]=s.toCharArray();
int flag=0;
for(char c1:c)
if(Character.isLetter(c1))
flag++;
}
if(flag!=length)
System.out.println("Invalid Input");
else
int i=0;
for(char c1:c)
if(i%2==0)
System.out.print(Character.toUpperCase(c1));
i++;
else
System.out.print(c1);
i++;
Solution:
import java.util.*;
class Main
int num=sc.nextInt();
int product=1;
if(num<0 || num>32767)
System.out.println("Invalid Input");
else
while(num>0)
int a=num%10;
product*=a;
num=num/10;
if(product==1)
}
else
System.out.println(product);
Solution:
import java.util.*;
int distance=read.nextInt();
if(distance<1)
System.out.println("Invalid Input");
else
float dism=distance*1000;
float div=dism/10;
float time=div/60;
System.out.println("Estimated Time:");
System.out.printf("%.2f Minutes",time);
}}}
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
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)
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
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{
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
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
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 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");
}
}
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[]){
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
output
barbie=2
doll=2
bike=1
Speed Calculation
Gender Determination :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int count=0,count1=0;
if(!(s.matches("[mfMF]+")))
{
System.out.println("Invalid Input");
return;
}
else
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='m' ||s.charAt(i)=='M')
count++;
else
count1++;
}
System.out.println(count+" "+"Male");
System.out.println(count1+" "+"Female");
}}
LCM OF DAY INTERVAL/LUNCH/DINNER TOGETHER :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day interval of person 1:");
int a=sc.nextInt();
System.out.println("Enter the day interval of person 2:");
int b=sc.nextInt();
if(a<=0 || b<=0)
{
System.out.println("Given interval is not valid");
return;
3
}
int lcm;
lcm=(a>b)?a:b;
while(true){
if(lcm%a==0 && lcm%b==0){
System.out.println("person 1 and person 2 will have
their dinner on day "+lcm);
break;
}
lcm++;
}
}}
import java.util.*;
int n=sc.nextInt();
int count=0;
if(n<1 || n>10){
System.exit(0);
else{
for(int i=0;i<n;i++){
int b=sc.nextInt();
a1[i]=b;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int c=sc.nextInt();
a2[i]=c;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int mul=1,r=0,x=a1[i];
while(x>0){
r=x%10;
mul*=r;
5
x=x/10;
for(int j=0;j<n;j++){
int sum=0,p=0,y=a2[j];
while(y>0){
p=y%10;
sum+=p;
y=y/10;
if(mul==sum){
count++;
System.out.print(a1[i]+","+a2[j]);
System.out.println(" ");
}}}
if(count==0){
System.exit(0);
}}}}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
int index=0;
6
int count=0;
while((index=s1.indexOf(s2,index))!=-1){
index=index+1;
count+=1;
}
System.out.println(count);
}
}
Fun Count
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=0){
System.out.println("Invalid Input");
return;
}
if(n>15){
System.out.println("Number exceeds the limit");
return;
}
Map<String,Integer> a=new HashMap<>();
for(int i=0;i<n;i++){
String s=sc.next().toLowerCase();
if(a.containsKey(s))
a.put(s,1+a.get(s));
else
a.put(s,1);
}
Map<String,Integer> tm=new TreeMap<>(a);
for(Map.Entry<String,Integer>data:tm.entrySet()){
System.out.println(data.getKey()+"= "+data.getValue());
}
}
}
7
SPEED ESTIMATION :
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the distance in kilometer");
int km=sc.nextInt();
if(km<0) {
System.out.println("Invalid Input");
return;
}
System.out.println("Enter the time to reach in hours");
int hours=sc.nextInt();
if(hours<0){
System.out.println("Invalid Input");
return;
}
int speed=km/hours;
if(speed<30){
System.out.println("You drive a car at normal speed");
return;
}
int inc=speed-30;
System.out.println("You want to increase a speed "+inc+"
km/hr from a normal speed");
}}
Prime Number
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
int count=0;
8
if(a<0 || b<0){
return;
for(int i=a;i<=b;i++){
count = 0;
for(int j=2;j<=i;j++){
if(i % j == 0)
count = count+1;
if(count == 1)
System.out.print(i+" ");
if(count!=1)
Theatre
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(s.length()==6)
{
9
String a=s.substring(0,1);
if(a.matches("[NEWS]+"))
{
String b=s.substring(1,2);
if(b.matches("[ABCDE]+"))
{
String c=s.substring(2,3);
if(c.matches("[1-4]+"))
{
String d=s.substring(3,6);
int num=Integer.parseInt(d);
if(num>0 && num<500)
{
System.out.println("Name "+a);
System.out.println("Block "+b);
System.out.println("Floor number "+c);
System.out.println("Seat number
"+num);
}
else
System.out.println("Invalid Seat number");
}
else
System.out.println("Invalid Floor number");
}
else
System.out.println("Invalid Block");
}
else
System.out.println("Invalid Name");
}
else
System.out.println("Invalid size");
}}
10
Customer details
import java.util.Scanner;
}
}
}
}
Team Win/Loose
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.next();
int count=0;
if(str.length()<3) {
System.out.println("Invalid input");
return;
}
if(str.matches("[htHT]+")) {
String s=str.toLowerCase();
for(int i=0;i<s.length()-2;i++){
if(s.charAt(i)!=s.charAt(i+1) &&
s.charAt(i)==s.charAt(i+2))
count++;
}
if(count==s.length()-2)
System.out.println("Team will win");
else
System.out.println("Team may loose");
}
else {
System.out.println("Invalid Input");
return;
}
}}
Holiday Fun
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
12
int sum=0,sum1=0;
int n=sc.nextInt();
if(n>0){
String[] a=new String[n];
for(int i=0;i<n;i++){
a[i]=sc.next();
}
for(int i=0;i<n;i++){
int len=a[i].length();
if(len%2==0)
{
for(int j=1;j<len;j++){
sum=sum+Integer.parseInt(String.valueOf(a[i].charAt(j)));
j++;
}}
else
{
for(int k=0;k<len;k++){
sum1=sum1+Integer.parseInt(String.valueOf(a[i].charAt(k)));
k++;
}}
}
System.out.println(sum+sum1);
}
else
System.out.println("Invalid size");
}}
Reverse Prime
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0,c=0,f=0,r=0,prime=0;
int n=sc.nextInt();
if(n>0) {
int[] a=new int[n];
13
for(int i=0;i<n;i++) {
int num=sc.nextInt();
if(num>9)
a[i]=num;
else {
System.out.println("Provided "+num+" is not valid");
return;
}}
for(int i=0;i<n;i++) {
count=0;
for(int j=2;j<a[i];j++)
{
if(a[i]%j==0)
count++;
}
if(count==0) {
prime++;
f=a[i];
r=0;
while(a[i]!=0) {
r=r*10;
r=r+a[i]%10;
a[i] =a[i]/10;
}
for(int k=2;k<r;k++) {
if(r%k==0)
c++;
}
if(c==0)
System.out.println(f);
}}}
else {
System.out.println(n+" is less than a limit");
return;
}
if(prime==0)
System.out.println(n+" values are not prime numbers");
}}
14
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0;
System.out.println("Enter the size of array ");
int n=sc.nextInt();
int[] sum=new int[n];
if(n>=1 && n<=10)
{
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();
int[] b=new int[n];
System.out.println("Enter the elements of second array ");
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]%2==0 && b[i]%2==0)
{
count++;
sum[i]=a[i]+b[i];
}
else
sum[i]=0;
}
if(count==0)
System.out.println("There are no even elements");
else
for(int result:sum)
System.out.println(result);
}
else
System.out.println("Invalid");
}}
15
Groceries Bill
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int a=0,b=0,c=0,d=0,count=0;
if(s.matches("[ORWKorwk]+")) {
for(int i=0;i<s.length();i++) {
for(int j=0;j<s.length();j++) {
if(s.charAt(i)==s.charAt(j))
count++;
}}
if(count==s.length()) {
for(int k=0;k<s.length();k++)
{
if(s.charAt(k)=='O')
a=4*20;
else if(s.charAt(k)=='R')
b=3*5;
else if(s.charAt(k)=='W')
c=2*10;
else
d=5*15;
}
System.out.println(a+b+c+d);
}
else
System.out.println("Duplicates are not allowed");
}
else
System.out.println("Invalid");
}}
16
Cake Order
import java.util.*;
if (cd.matches("[a-zA-Z0-9]*")) {
String w=cd.substring(0, 4);
if(!w.matches("[0-9]*")){
System.out.println("Invalid weight");
return;
}
float wg=Integer.parseInt(w);
if (wg>999) {
float fwg=wg/1000;
double weight= (Math.round(fwg * 100.0) / 100.0);
String o=cd.substring(cd.length()-3);
if (!o.matches("[0-9]*")) {
System.out.println("Invalid order number");
return;
}
int on=Integer.parseInt(o);
if(on>99) {
String flavour=cd.substring(4,cd.length()-3);
double cakePrice=450*weight;
17
}
Java Codes
1.Absolute Difference
public class AbsoluteDifference {
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 sum=0;
if((n<=0) && (n%2!=0))
{
System.out.println(n+" is an invalid size");
return;
}
System.out.println("Enter the numbers");
int[] a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(a[i]<=0)
{
System.out.println(a[i]+" is an invalid number");
return;
}
}
for(int i=0;i<n/2;i++)
{
sum+=Math.abs(a[i]-a[n-i-1]);
}
System.out.println(sum);
}
}
}
}
4.Car Parking
public class CarParking {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the car number");
String car=sc.nextLine();
String str[]=car.split(" ");
int len=car.length();
if(len!=10)
{
System.out.println("Invalid State Code");
return;
}
String floor;
String code=car.substring(0,2);
if(code.equals("TN"))
{
floor="ground";
}
else if(code.equals("KA"))
{
floor="1st";
}
else if(code.equals("KL"))
{
floor="2nd";
}
else if(code.equals("AP"))
{
floor="3rd";
}
else{
System.out.println("Invalid State Code");
return;
}
System.out.println("Park the car in "+floor+" floor");
}
}
7.Digit Count
public class DigitCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
String num = Integer.toString(n);
if (n<=0) {
System.out.println(num + " is an invalid number");
return;
} else {
System.out.println("Enter the digit");
int d = sc.nextInt();
String dig = Integer.toString(d);
if (!dig.matches("[0-9]")) {
System.out.println(dig + " is not in range");
return;
} else {
int count = 0;
for (int i = 0; i < num.length(); i++) {
int ch = Integer.parseInt(String.valueOf(num.charAt(i)));
if (ch == d) {
count++;
}
}
System.out.println(d+" appears in "+num+" is "+count);
}
}
9.Fun Count
public class FC2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of toys");
int n=sc.nextInt();
if(n<=0)
{
System.out.println("Invalid Input");
}
else if(n>15)
{
System.out.println("Number exceeds the limit");
}
else{
String[] ar1=new String[n];
System.out.println("Enter the toy list");
for(int i=0;i<n;i++)
{
ar1[i]=sc.next().toLowerCase();
}
Arrays.sort(ar1);
int count=0;
for(int i=0;i<n;i++)
{
count=1;
for(int j=i+1;j<n;j++)
{
if(ar1[i].equalsIgnoreCase(ar1[j]))
{
count++;
ar1[j]="0";
}
}
if(ar1[i]!="0")
{
System.out.println(ar1[i]+"="+count);
}
}
}
}
}
10.Gender Determination
public class GenderDetermination {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int mc=0;
int fc=0;
if(!str.matches("[mMfF]*"))
{
System.out.println("Invalid Input");
}
else{
for(int i=0;i<str.length();i++)
{
char temp=str.charAt(i);
if((str.charAt(i)=='M') || (str.charAt(i)=='m'))
{
mc++;
}
else if((str.charAt(i)=='F') || (str.charAt(i)=='f'))
{
fc++;
}
}
System.out.println(mc+" Male");
System.out.println(fc+" Female");
}
}
}
14.Lottery Ticket
public class LotteryTicket {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i=0,c=0,n=0,sum=0,count=0;
System.out.println("Enter the starting range");
int a=sc.nextInt();
String str1=Integer.toString(a);
if(str1.length()!=5)
{
System.out.println(str1+" is an invalid length");
return;
}
System.out.println("Enter the ending range");
int b=sc.nextInt();
String str2=Integer.toString(b);
if(str2.length()!=5)
{
System.out.println(str2+" is an invalid length");
return;
}
if(a>b)
{
System.out.println(a+" and "+b+" are invalid serial numbers");
return;
}
for(i=a;i<=b;i++)
{
n=i;
while(n!=0)
{
c=n%10;
sum=sum+c;
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;
}
}
}
}
}
24.Threes
public class ThreesASCII {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the String");
String str =in.nextLine();
int len=str.length();
for(int i=0;i<len;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<len;i++)
{
if((i+1)%3==0)
{
sum+=(int)(str.charAt(i));
}
}
System.out.println("Sum is "+sum);
}
}
25.Toy Shop
public class ToyShop {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of months");
int m=sc.nextInt();
if(m<=0)
{
System.out.println(m+" is an invalid number of month");
return;
}
int[] a=new int[m];
System.out.println("Enter the number of toys to be purchased monthly");
for(int i=0;i<m;i++)
{
a[i]=sc.nextInt();
if(a[i]<=10)
{
System.out.println(a[i]+" is an invalid number of toys");
return;
}
}
int max=0,index=1;
for(int i=0;i<m;i++)
{
if(a[i]>=max)
{
max=a[i];
index=i;
}
}
for(int k=0;k<m;k++)
{
if(a[k]==max)
System.out.println("Month "+(k+1));
}
}}
26.Unique Characters
public class Unique {
28.Word Find
public class WordFind {
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 than the desired limit");
return;
}
sc.nextLine();
String set1[]=new String[n];
String set2[]=new String[n];
System.out.println("Enter the words in set1");
for(int i=0;i<n;i++)
{
set1[i]=sc.nextLine();
}
System.out.println("Enter the words in set2");
for(int i=0;i<n;i++)
{
set2[i]=sc.nextLine();
}
int count=0;
int flag=1;
for(int i=0;i<set1.length;i++)
{
String first=set1[i];
for(int j=0;j<set2.length;j++)
{
String second=set2[j];
if(first.equalsIgnoreCase(second))
{
count++;
flag=0;
}
}
if(flag==1)
{
System.out.println(set1[i]);
}
flag=1;
}
if(n==count)
{
System.out.println(n+" words in a set are repeated");
}
}
29.MegaMart
public class MegaMart {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Customer Id");
String str = sc.next();
String name = str.substring(0,4);
if(name.equals("Mega")){
String ctype ="";
if(str.contains("Gold") || str.contains("Silver") || str.contains("Platinum")){
if(str.contains("Gold"))
ctype = "Gold";
else if(str.contains("Platinum"))
ctype="Platinum";
else if(str.contains("Silver"))
ctype="Silver";
String n =str.substring(str.length()-3,str.length());
if(n.matches("[a-z]{1}[0-9]{2}"))
{
System.out.println("Invalid Member Id");
return;
}
int n1 =Integer.parseInt(n);
if(n1>99 && n1 <1000)
System.out.println("Welcome Mega Mart "+ctype+" Customer");
else{
System.out.println("Invalid Member Id");
}
}
else{
System.out.println("Invalid Customer Type");
}
}
else{
System.out.println("Invalid Shop Name");
}
}
}
31.Bike Rent
public class BillAmount {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String name=sc.nextLine();
int len=name.length();
double fare=0;
double disc=0;
for(int i=0;i<len;i++)
{
if(!Character.isLetter(name.charAt(i)))
{
System.out.println(name+" is an invalid name");
return;
}
}
System.out.println("Enter the time of duration");
int n=sc.nextInt();
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("Choose an option");
int op=sc.nextInt();
if((op<0) || (op>=3))
{
System.out.println("Try Again");
return;
}
if(op==1)
{
if(n>=5)
{
fare=120*n;
disc=(fare*0.25);
fare=fare-disc;
}
else{
fare=120*n;
}
}
if(op==2)
{
if(n>=5)
{
fare=120*n;
disc=(fare*0.17);
fare=(120*n)-disc;
}
else{
fare=n*120;
}
}
if(op==3)
{
if(n>=5)
{
fare=120*n;
}
}
System.out.print("Dear "+name+" your bill amount is ");
System.out.printf("%.2f",fare);
}
}
32.Speed Calculation
public class SpeedCalculation {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the distance in kilometres");
int dis=sc.nextInt();
if(dis<0)
{
System.out.println("Invalid input");
return;
}
System.out.println("Enter the time to reach in hours");
int hour=sc.nextInt();
if(hour<0)
{
System.out.println("Invalid input");
return;
}
int speed=dis/hour;
if(speed>30)
{
int ini_sp=speed-30;
System.out.println("You want to increase a speed "+ini_sp+" km/hr from a normal
speed");
}
else{
System.out.println("You drive a car at a normal speed");
}
}
}
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
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
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
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
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
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();
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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
*********************************************************************************************
******************************************************************************
18. HARSHAD NUMBER (Sum of digit completely divide that number)
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)
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
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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]);
}
}
}
}
*********************************************************************************************
*********************************************************************************************
*
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
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;
}
}
}
*********************************************************************************************
*********************************************************************************************
***
import java.util.Arrays;
import java.util.Scanner;
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
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;
*******************************************************************************
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);
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
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.
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
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)
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
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
import java.util.*;
public class Program{
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
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
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 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");
}
}
If first letter is not same then reverse first word and concatenate with last word
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);
}
}
}
output
barbie=2
doll=2
bike=1
Speed Calculation
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");
}
}
}
JAVA
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
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
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) COMBINE NUMBERS / STUDENTS WHOSE ROLLNO ARE ODD
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
andLOWERcaseletters
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
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;
}
}
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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]);
}
}
}
}
}
****ALTERNATE METHOD****
}
boolean flag = true;
double min=time[0];
int index=0;
for(int i=0;i<n;i++)
{
if(time[i]<min)
{
min=time[i];
//index=i;
}
if(min!=time[i])
{
flag=false;
System.out.println(name[i-1]);
}
}
if (flag) System.out.println(n+" runners have same timing");
}
}
*********************************************************************************************
******************************************************************************
17.REEDME
*********************************************************************************************
******************************************************************************
18)HARSHAD NUMBER (Sum of digit completely divide that
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) VIDEO GAME PLAYER (PS1)
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) ABSOLUTE DIFFERENCE / ALTERNATE SUM DIFFERENCE
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
ADDITION
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. REVERSE FIRST HALF/MIDDLE LETTER 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");
}
}
}
***ALNERNATE METHOD****
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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]);
}
}
}
}
*********************************************************************************************
*********************************************************************************************
*
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;
}
}
***ALTERNATE METHOD*****
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Enter the String");
Scanner sc=new Scanner(System. in);
String str=sc.nextLine();
String output="";
String[] word=str.split(" ");
for(int i=0;i<word.length;i++)
{
for(int j=0;j<word[i].length();j++)
{
int count=0;
char c=word[i].charAt(j);
for(int k=0;k<word[i].length();k++)
{
if(c==word[i].charAt(k))
count++;
}
if(count==1)
{
output+=Character.toLowerCase(c);
}
}
output+=" ";
}
System.out.println(output);
}
}
*********************************************************************************************
*********************************************************************************************
*
27. BOX SIZE
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;
}
}
}
****ALTERNATE METHOD***********
Input:
Enter size:5
Enter numbers:
2
34
456
67
123
Explanation:
22=(2+4+6+7+3)
Similarly for invalid box also
}
********************************************************************************************
****************************************************
28. ORDER IDENTIFICATION:(check where elements are in ascending
import java.util.Arrays;
import java.util.Scanner;
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;
}
}
***ALTERNATE METHOD***
********************************************************************************************
********************************************************************************************
********************************
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
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;
*******************************************************************************
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);
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);
}
}
****ALTERNATE METHOD****
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));
}
}
}
}
**********************************************************************************************
***************************************************
INPUT:
Enter the array size
5
Sample Output:
12
6
21
36
20
Logic:
12*1=12
3*2=6
7*3=21
9*4=36
4*5=20
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size:");
int n=sc.nextInt();
int res=0;
if(n<1 || n>10)
{
System.out.println("Invalid Array Size");
return;
}
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
res=a[i]*(i+1);
System.out.println(res);
}
}
}
**********************************************************************************************
******************************************************************
36) CAR PARKING
Sample Input:
Enter the car number
ap 37 1498
Sample Output
Park the car in 3rd floor
import java.util.*;
ALTERNATE METHOD:
Sample Input:
Sample output
6 appears in 56164 is 2
Explanation:
Digit 6 appears 2 times in the number 56164
package accenture;
import java.util.*;
if(ch[i]==d)
{
count++;
}
}
if(count>1)
{
System.out.println(d+" appears "+n+" is "+count);
}
}
Sample input:
Enter the number of words
5
Enter the words in set1:
good
bad
hello
Nothing
North
package practice;
import java.util.*;
public class RepeatedWords {
}
**********************************************************************************************
******************************************************
39) SUM OF INTEGERS AT EVEN OR ODD PLACES
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)
import java.util.*;
public class oddevenindex {
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the size of the array: ");
int n = Integer.parseInt(sc.nextLine());
String[] arr = new String[n];
int[] fin = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextLine();
}
int sum = 0;
int total =0;
if(n%2!=0)
{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
else{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
System.out.println("Output: "+total);
}
}
*****ALTERNATE METHOD*****
package practice;
import java.util.*;
public class SumOfEvenOrOdd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size");
int n = sc.nextInt();
if(n<=0){
System.out.println("Invalid size");
return;
}
System.out.println("Enter the elements:");
int sum = 0;
String str[] = new String[n];
for(int i=0;i<n;i++){
str[i] = sc.next();
}
for(int i = 0;i<n;i++){
String s = str[i];
if(s.length()%2 == 0){
for(int j = 1;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}else{
for(int j = 0;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}
}
System.out.println(sum);
}
}
**********************************************************************************************
**********************************************************************
40) SUM OF EVEN ELEMENTS
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.
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
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
package practice;
import java.util.*;
public class SumOfEvenElements {
Sample input:
Enter the string
Entertainment
Enter substring
en
Sample output: 2
package accenture;
import java.util.*;
import java.util.regex.*;
public class countofoccurencesofsubstringinastring {
}
**********************************************************************************************
******************************************************************
42) LARGEST SORTED SUBSTRING FROM INPUT STRING
package com.company;
import java.util.Scanner;
Sample input 1:
A2S3D06D13
Sample output 2:
Seat Number
D13
D14
D15
D16
D17
Sample input 2:
A2S3D02C01
Sample Output2:
Seat Number
C1
C2
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
import java.util.Scanner;
46) THREE’S
Sample input:
Enter the string
Long live
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);
}
}
**********************************************************************************************
**********************************************************************************
47) EVEN POSITION CAPS (CAPITAL LETTERS)
import java.util.Scanner;
import java.util.regex.Pattern;
***ALTERNATE METHOD***
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Input string");
String str = sc.next();
if(Pattern.matches("[A-Za-z]+",str)){
if(str.length()<5 || str.length()>20){
System.out.println(str+" is an invalid length");
return;
}
String output = str.charAt(0)+"";
for(int i=0;i<str.length()-1;i++){
if(i%2==0){
output = output+ Character.toString(str.charAt(i+1)).toUpperCase();
}
else
output = output + str.charAt(i+1);
}
System.out.println(output);
}
else{
System.out.println(str+" is an invalid input");
return;
}
}
}
**********************************************************************************************
****************************************************************************************
}
}
}
}
**********************************************************************************************
*******************************************************************************
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
if(km<=0){
System.out.println("Invalid Input");
return;
}
int sp = km/time;
if(sp>30){
int in_sp = sp-30;
System.out.println("You want to increase a speed "+in_sp+" km/hr from a normal speed");
}
else{
System.out.println("You drive a car at a normal speed");
}
}
}
***********************************************************************************************
*********************************************************************************
50) SUM OF SQUARES (RE-TEST)
Sample input
Enter the number of values
4
Enter the values
2
7
9
8
Sample output
130
Explanation
Odd numbers are 7,9 = 7*7 +9*9 = 49+81=130
import java.util.*;
public 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();
if(n<=0)
{
System.out.println(n+" is an invalid size");
return;
}
int a[]=new int[n];
int count=0;
int sum=0;
System.out.println("Enter the values");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(a[i]%2!=0)
count=count+1;
}
if(count<=0)
{
System.out.println("These "+n+" values are not a odd digit");
return;
}
else
{
for(int i=0;i<n;i++)
{
if(a[i]%2!=0)
{
sum=sum+a[i]*a[i];
}
}
System.out.println(sum);
}
}
}
JAVA END
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.
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
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
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) COMBINE NUMBERS / STUDENTS WHOSE ROLLNO ARE ODD
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
andLOWERcaseletters
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
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;
}
}
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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]);
}
}
}
}
}
****ALTERNATE METHOD****
}
boolean flag = true;
double min=time[0];
int index=0;
for(int i=0;i<n;i++)
{
if(time[i]<min)
{
min=time[i];
//index=i;
}
if(min!=time[i])
{
flag=false;
System.out.println(name[i-1]);
}
}
if (flag) System.out.println(n+" runners have same timing");
}
}
*********************************************************************************************
******************************************************************************
17.REEDME
*********************************************************************************************
******************************************************************************
18)HARSHAD NUMBER (Sum of digit completely divide that
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) VIDEO GAME PLAYER (PS1)
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) ABSOLUTE DIFFERENCE / ALTERNATE SUM DIFFERENCE
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
ADDITION
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. REVERSE FIRST HALF/MIDDLE LETTER 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");
}
}
}
***ALNERNATE METHOD****
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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]);
}
}
}
}
*********************************************************************************************
*********************************************************************************************
*
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;
}
}
***ALTERNATE METHOD*****
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Enter the String");
Scanner sc=new Scanner(System. in);
String str=sc.nextLine();
String output="";
String[] word=str.split(" ");
for(int i=0;i<word.length;i++)
{
for(int j=0;j<word[i].length();j++)
{
int count=0;
char c=word[i].charAt(j);
for(int k=0;k<word[i].length();k++)
{
if(c==word[i].charAt(k))
count++;
}
if(count==1)
{
output+=Character.toLowerCase(c);
}
}
output+=" ";
}
System.out.println(output);
}
}
*********************************************************************************************
*********************************************************************************************
*
27. BOX SIZE
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;
}
}
}
****ALTERNATE METHOD***********
Input:
Enter size:5
Enter numbers:
2
34
456
67
123
Explanation:
22=(2+4+6+7+3)
Similarly for invalid box also
}
********************************************************************************************
****************************************************
28. ORDER IDENTIFICATION:(check where elements are in ascending
import java.util.Arrays;
import java.util.Scanner;
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;
}
}
***ALTERNATE METHOD***
********************************************************************************************
********************************************************************************************
********************************
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
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;
*******************************************************************************
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);
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);
}
}
****ALTERNATE METHOD****
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));
}
}
}
}
**********************************************************************************************
***************************************************
INPUT:
Enter the array size
5
Sample Output:
12
6
21
36
20
Logic:
12*1=12
3*2=6
7*3=21
9*4=36
4*5=20
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size:");
int n=sc.nextInt();
int res=0;
if(n<1 || n>10)
{
System.out.println("Invalid Array Size");
return;
}
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
res=a[i]*(i+1);
System.out.println(res);
}
}
}
**********************************************************************************************
******************************************************************
36) CAR PARKING
Sample Input:
Enter the car number
ap 37 1498
Sample Output
Park the car in 3rd floor
import java.util.*;
ALTERNATE METHOD:
Sample Input:
Sample output
6 appears in 56164 is 2
Explanation:
Digit 6 appears 2 times in the number 56164
package accenture;
import java.util.*;
if(ch[i]==d)
{
count++;
}
}
if(count>1)
{
System.out.println(d+" appears "+n+" is "+count);
}
}
Sample input:
Enter the number of words
5
Enter the words in set1:
good
bad
hello
Nothing
North
package practice;
import java.util.*;
public class RepeatedWords {
}
**********************************************************************************************
******************************************************
39) SUM OF INTEGERS AT EVEN OR ODD PLACES
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)
import java.util.*;
public class oddevenindex {
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the size of the array: ");
int n = Integer.parseInt(sc.nextLine());
String[] arr = new String[n];
int[] fin = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextLine();
}
int sum = 0;
int total =0;
if(n%2!=0)
{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
else{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
System.out.println("Output: "+total);
}
}
*****ALTERNATE METHOD*****
package practice;
import java.util.*;
public class SumOfEvenOrOdd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size");
int n = sc.nextInt();
if(n<=0){
System.out.println("Invalid size");
return;
}
System.out.println("Enter the elements:");
int sum = 0;
String str[] = new String[n];
for(int i=0;i<n;i++){
str[i] = sc.next();
}
for(int i = 0;i<n;i++){
String s = str[i];
if(s.length()%2 == 0){
for(int j = 1;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}else{
for(int j = 0;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}
}
System.out.println(sum);
}
}
**********************************************************************************************
**********************************************************************
40) SUM OF EVEN ELEMENTS
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.
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
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
package practice;
import java.util.*;
public class SumOfEvenElements {
Sample input:
Enter the string
Entertainment
Enter substring
en
Sample output: 2
package accenture;
import java.util.*;
import java.util.regex.*;
public class countofoccurencesofsubstringinastring {
}
**********************************************************************************************
******************************************************************
42) LARGEST SORTED SUBSTRING FROM INPUT STRING
package com.company;
import java.util.Scanner;
Sample input 1:
A2S3D06D13
Sample output 2:
Seat Number
D13
D14
D15
D16
D17
Sample input 2:
A2S3D02C01
Sample Output2:
Seat Number
C1
C2
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
import java.util.Scanner;
46) THREE’S
Sample input:
Enter the string
Long live
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);
}
}
**********************************************************************************************
**********************************************************************************
47) EVEN POSITION CAPS (CAPITAL LETTERS)
import java.util.Scanner;
import java.util.regex.Pattern;
***ALTERNATE METHOD***
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Input string");
String str = sc.next();
if(Pattern.matches("[A-Za-z]+",str)){
if(str.length()<5 || str.length()>20){
System.out.println(str+" is an invalid length");
return;
}
String output = str.charAt(0)+"";
for(int i=0;i<str.length()-1;i++){
if(i%2==0){
output = output+ Character.toString(str.charAt(i+1)).toUpperCase();
}
else
output = output + str.charAt(i+1);
}
System.out.println(output);
}
else{
System.out.println(str+" is an invalid input");
return;
}
}
}
**********************************************************************************************
****************************************************************************************
}
}
}
}
**********************************************************************************************
*******************************************************************************
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
if(km<=0){
System.out.println("Invalid Input");
return;
}
int sp = km/time;
if(sp>30){
int in_sp = sp-30;
System.out.println("You want to increase a speed "+in_sp+" km/hr from a normal speed");
}
else{
System.out.println("You drive a car at a normal speed");
}
}
}
***********************************************************************************************
*********************************************************************************
50) SUM OF SQUARES (RE-TEST)
Sample input
Enter the number of values
4
Enter the values
2
7
9
8
Sample output
130
Explanation
Odd numbers are 7,9 = 7*7 +9*9 = 49+81=130
import java.util.*;
public 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();
if(n<=0)
{
System.out.println(n+" is an invalid size");
return;
}
int a[]=new int[n];
int count=0;
int sum=0;
System.out.println("Enter the values");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(a[i]%2!=0)
count=count+1;
}
if(count<=0)
{
System.out.println("These "+n+" values are not a odd digit");
return;
}
else
{
for(int i=0;i<n;i++)
{
if(a[i]%2!=0)
{
sum=sum+a[i]*a[i];
}
}
System.out.println(sum);
}
}
}
JAVA END
*********************************************************************************************
*********************************************************************************************
********
}
}
2
Gender Determination :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int count=0,count1=0;
if(!(s.matches("[mfMF]+")))
{
System.out.println("Invalid Input");
return;
}
else
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='m' ||s.charAt(i)=='M')
count++;
else
count1++;
}
System.out.println(count+" "+"Male");
System.out.println(count1+" "+"Female");
}}
LCM OF DAY INTERVAL/LUNCH/DINNER TOGETHER :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day interval of person 1:");
int a=sc.nextInt();
System.out.println("Enter the day interval of person 2:");
int b=sc.nextInt();
if(a<=0 || b<=0)
{
System.out.println("Given interval is not valid");
return;
7
SPEED ESTIMATION :
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the distance in kilometer");
int km=sc.nextInt();
if(km<0) {
System.out.println("Invalid Input");
return;
}
System.out.println("Enter the time to reach in hours");
int hours=sc.nextInt();
if(hours<0){
System.out.println("Invalid Input");
return;
}
int speed=km/hours;
if(speed<30){
System.out.println("You drive a car at normal speed");
return;
}
int inc=speed-30;
System.out.println("You want to increase a speed "+inc+"
km/hr from a normal speed");
}}
Prime Number
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
int count=0;
8
if(a<0 || b<0){
return;
for(int i=a;i<=b;i++){
count = 0;
for(int j=2;j<=i;j++){
if(i % j == 0)
count = count+1;
if(count == 1)
System.out.print(i+" ");
if(count!=1)
Theatre
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(s.length()==6)
{
10
Customer details
import java.util.Scanner;
}
}
}
}
Team Win/Loose
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.next();
int count=0;
if(str.length()<3) {
System.out.println("Invalid input");
return;
}
if(str.matches("[htHT]+")) {
String s=str.toLowerCase();
for(int i=0;i<s.length()-2;i++){
if(s.charAt(i)!=s.charAt(i+1) &&
s.charAt(i)==s.charAt(i+2))
count++;
}
if(count==s.length()-2)
System.out.println("Team will win");
else
System.out.println("Team may loose");
}
else {
System.out.println("Invalid Input");
return;
}
}}
Holiday Fun
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
12
int sum=0,sum1=0;
int n=sc.nextInt();
if(n>0){
String[] a=new String[n];
for(int i=0;i<n;i++){
a[i]=sc.next();
}
for(int i=0;i<n;i++){
int len=a[i].length();
if(len%2==0)
{
for(int j=1;j<len;j++){
sum=sum+Integer.parseInt(String.valueOf(a[i].charAt(j)));
j++;
}}
else
{
for(int k=0;k<len;k++){
sum1=sum1+Integer.parseInt(String.valueOf(a[i].charAt(k)));
k++;
}}
}
System.out.println(sum+sum1);
}
else
System.out.println("Invalid size");
}}
Reverse Prime
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0,c=0,f=0,r=0,prime=0;
int n=sc.nextInt();
if(n>0) {
int[] a=new int[n];
13
for(int i=0;i<n;i++) {
int num=sc.nextInt();
if(num>9)
a[i]=num;
else {
System.out.println("Provided "+num+" is not valid");
return;
}}
for(int i=0;i<n;i++) {
count=0;
for(int j=2;j<a[i];j++)
{
if(a[i]%j==0)
count++;
}
if(count==0) {
prime++;
f=a[i];
r=0;
while(a[i]!=0) {
r=r*10;
r=r+a[i]%10;
a[i] =a[i]/10;
}
for(int k=2;k<r;k++) {
if(r%k==0)
c++;
}
if(c==0)
System.out.println(f);
}}}
else {
System.out.println(n+" is less than a limit");
return;
}
if(prime==0)
System.out.println(n+" values are not prime numbers");
}}
14
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0;
System.out.println("Enter the size of array ");
int n=sc.nextInt();
int[] sum=new int[n];
if(n>=1 && n<=10)
{
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();
int[] b=new int[n];
System.out.println("Enter the elements of second array ");
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]%2==0 && b[i]%2==0)
{
count++;
sum[i]=a[i]+b[i];
}
else
sum[i]=0;
}
if(count==0)
System.out.println("There are no even elements");
else
for(int result:sum)
System.out.println(result);
}
else
System.out.println("Invalid");
}}
15
Groceries Bill
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int a=0,b=0,c=0,d=0,count=0;
if(s.matches("[ORWKorwk]+")) {
for(int i=0;i<s.length();i++) {
for(int j=0;j<s.length();j++) {
if(s.charAt(i)==s.charAt(j))
count++;
}}
if(count==s.length()) {
for(int k=0;k<s.length();k++)
{
if(s.charAt(k)=='O')
a=4*20;
else if(s.charAt(k)=='R')
b=3*5;
else if(s.charAt(k)=='W')
c=2*10;
else
d=5*15;
}
System.out.println(a+b+c+d);
}
else
System.out.println("Duplicates are not allowed");
}
else
System.out.println("Invalid");
}}
16
Cake Order
import java.util.*;
if (cd.matches("[a-zA-Z0-9]*")) {
String w=cd.substring(0, 4);
if(!w.matches("[0-9]*")){
System.out.println("Invalid weight");
return;
}
float wg=Integer.parseInt(w);
if (wg>999) {
float fwg=wg/1000;
double weight= (Math.round(fwg * 100.0) / 100.0);
String o=cd.substring(cd.length()-3);
if (!o.matches("[0-9]*")) {
System.out.println("Invalid order number");
return;
}
int on=Integer.parseInt(o);
if(on>99) {
String flavour=cd.substring(4,cd.length()-3);
double cakePrice=450*weight;
17
}
1. Maze Runner
Solution:
import java.util.*;
class Main
int points1=sc.nextInt();
if(points1<1000 || points1>5000)
System.out.println("Invalid Input");
else
String s1=sc.next();
int flag=0;
char ch1[]=s1.toCharArray();
for(char c:ch1)
//System.out.println(c);
flag++;
if(flag!=s1.length())
System.out.println("Invalid Input");
else
int points2=sc.nextInt();
if(points2<1000 || points2>5000)
System.out.println("Invalid Input");
else
String s2=sc.next();
if(s1.length()!=s2.length())
System.out.println("Invalid Input");
else
{
int flag1=0;
char ch2[]=s2.toCharArray();
for(char c:ch2)
flag1++;
if(flag1!=s2.length())
System.out.println("Invalid Input");
else
for(char c:ch1)
if(c=='L')
points1+=50;
if(c=='R')
points1-=50;
}
if(c=='B')
points1*=2;
if(c=='F')
points1/=2;
for(char c:ch2)
if(c=='L')
points2+=50;
if(c=='R')
points2-=50;
if(c=='B')
points2*=2;
if(c=='F')
{
points2/=2;
if(points1>points2)
System.out.println("Player 1 Wins");
else if(points1<points2)
System.out.println("Player 2 Wins");
else
}
2. Holiday Trip:
Solution:
import java.util.*;
String fname=sc.nextLine();
String fclass=sc.nextLine();
int no=sc.nextInt();
System.out.println("Card Payment:");
char pay=sc.next().charAt(0);
int cost=0;
switch(fname){
// System.out.println("Hai1111");
switch(fclass){
//System.out.println("Hai");
cost=3200;
break;
}
cost=6950;
break;
cost=10340;
break;
break;
switch(fclass){
if(no<=5){
cost=3450;
else if(no>5&&no<=10){
cost=(int)(3450*0.9);
else{
cost=(int)(3450*0.85);
break;
if(no<=5){
cost=7945;
else if(no>5&&no<=10){
cost=(int)(7945*0.9);
else{
cost=(int)(7945*0.85);
break;
if(no<=5){
cost=9500;
else if(no>5&&no<=10){
cost=(int)(9500*0.9);
else{
cost=(int)(9500*0.85);
}
break;
break;
switch(fclass){
cost=3300;
break;
cost=7250;
break;
cost=9890;
break;
break;
case "IndiGo":{
switch(fclass){
if(no<=5){
cost=3490;
else if(no>5&&no<=10){
cost=(int)(3490*0.9);
else{
cost=(int)(3490*0.85);
break;
if(no<=5){
cost=7560;
else if(no>5&&no<=10){
cost=(int)(7560*0.9);
else{
cost=(int)(7560*0.85);
}
break;
if(no<=5){
cost=9990;
else if(no>5&&no<=10){
cost=(int)(9990*0.9);
else{
cost=(int)(9990*0.85);
break;
break;
int res=cost*no;
if(pay=='Y')
switch(fname){
res=(int)(res*0.895);
}
break;
res=(int)(res*0.925);
break;
res=(int)(res*0.905);
break;
case "IndiGo":
res=(int)(res*0.915);
break;
System.out.println(res);
}
3. Tell the time
Solution:
import java.util.Scanner;
String s=sc.next();
String ch[]=s.split(":");
for(int i=0;i<2;i++)
int m1=Integer.valueOf(ch[i]);
m[i]=m1;
{ int n=sc.nextInt();
int k=m[1]+n;
if(k>59)
else
{m[0]=l;
count++;}
else
{ m[1]=k;
count++; }
System.out.println(m[0]+":0"+m[1]);
System.out.println(m[0]+":"+m[1]);
else
else
}
4.Fuel Quantity Calculation
Solution:
import java.util.*;
int rs=read.nextInt();
if(rs<1)
System.out.println("Invalid Input");
else
float liter=read.nextFloat();
if(liter<0)
System.out.println("Invalid Input");
else
float petrol=rs/liter;
System.out.printf("%.2f Liters",petrol);
}}
5.Vowel Pair Removal
Solution:
import java.util.Scanner;
for(int i=0;i<s.length();i++)
if(Character.isLetter(s.charAt(i)))
count++;
if(count==s.length())
for(int i=0;i<s.length();i++)
{ if(i<s.length())
if(((s.charAt(i)=='a')||(s.charAt(i)=='e')||(s.charAt(i)=='i')||(s.charAt(i)=='o')||(s.charAt(i)=='u'))&&
((s.charAt(i+1)=='a')||(s.charAt(i+1)=='e')||(s.charAt(i+1)=='i')||(s.charAt(i+1)=='o')||(s.charAt(i+1)=='u'))
)
{i++;
continue;
else
System.out.print(s.charAt(i));
else
System.out.print(s.charAt(i));
//System.out.println(s);
else
System.out.println("Invalid Input");
if(s.length()>8)
if(s.length()<3)
6. MAC Validation
Solution:
import java.lang.*;
import java.util.*;
public class Main{
String s=sc.nextLine();
int len=s.length();
int dc=0,cc=0,colonc=0,flag=0;
if(len==17){
char[] a=s.toCharArray();
for(char b:a){
if(Character.isLetter(b)){
if(b>=65&&b<=70)
cc++;
if(Character.isDigit(b)){
dc++;
if(b==':'){
colonc++;
if(!(Character.isLetter(b)||Character.isDigit(b)||b==':')){
flag++;
break;
if(flag==1){
System.out.println("Invalid Input");
// System.exit(0);
if(flag==0){
int res=cc+dc+colonc;
if(res==17){
else{
}
else if(len<17){
else{
7. Unique products
Solution:
import java.util.Scanner;
int item_count=sc.nextInt();
if(item_count<5 || item_count>20)
{
System.out.println("Invalid item count");
else
int flag=1;
int count=0;
for(int i=0;i<item_count;i++)
int it=sc.nextInt();
if(it<100 || it>999)
flag=0;
break;
else
item[i]=it;
if(flag!=0)
for(int j:item)
{
String s="";
s=s.valueOf(j);
if(s.charAt(0)!=s.charAt(1)&&s.charAt(0)!=s.charAt(2)&&s.charAt(2)!=s.charAt(1))
System.out.println(j);
count++;
if(count==0)
else
}
}
8.Toggle word
Solution:
import java.util.*;
class Main
String s=sc.next();
int length=s.length();
if(length<3)
else if(length>8)
}
else
char c[]=s.toCharArray();
int flag=0;
for(char c1:c)
if(Character.isLetter(c1))
flag++;
if(flag!=length)
System.out.println("Invalid Input");
else
int i=0;
for(char c1:c)
if(i%2==0)
System.out.print(Character.toUpperCase(c1));
i++;
}
else
System.out.print(c1);
i++;
import java.util.*;
class Main
int num=sc.nextInt();
int product=1;
if(num<0 || num>32767)
System.out.println("Invalid Input");
}
else
while(num>0)
int a=num%10;
product*=a;
num=num/10;
if(product==1)
else
System.out.println(product);
import java.util.*;
public class Main
int distance=read.nextInt();
if(distance<1)
System.out.println("Invalid Input");
else
float dism=distance*1000;
float div=dism/10;
float time=div/60;
System.out.println("Estimated Time:");
System.out.printf("%.2f Minutes",time);
}
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{
System.out.println(product);
}}
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 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");
}
}
If first letter is not same then reverse first word and concatenate with last word
NEXT MISSING LETTER
import java.util.*;
public class Accenture {
static boolean flag = true;
public static void main(String[] arg) {
if ( ! msg.equalsIgnoreCase(input) )
flag = false;
if( seq>0)
{
int diff=0, min=0;
for(int i=1 ; i<input.length() ; i++)
{
if(i==1)
diff = Math.abs((int)input.charAt(i-1) - (int)input.charAt(i));
else
{
min = Math.abs((int)input.charAt(i-1) - (int)input.charAt(i));
if( min != diff)
{
System.out.println(input+" not in correct sequence");
return;
}
}
}
import java.util.*;
public class JavaApplication55 {
null
public static void main(String[] args) {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.next();
int diff=str.charAt(0)-str.charAt((1));
System.out.println("Enter the sequnece");
int seq=sc.nextInt();
if(seq {
System.out.println(seq+ "is invalid");
}
for(int i = 0; i {
int val1=str.charAt(i);
int val2=str.charAt(i+1);
if(diff!=(val1-val2))
{
System.out.print(str+" not in order");
return;
}
}
int temp=str.charAt(str.length()-1);
for(int i = 0; i {
temp=temp-diff;
if(temp>122)
{
temp=temp-26;
System.out.print((char)temp);
}
else if(temp {
temp=temp+26;
System.out.print((char)temp);
}
else
{
System.out.print((char)temp);
}
}
}
}
Add with index
import java.util.*;
public class Main
{
}
1.Adams
Input:hththt
Output:-Team May Win
package sda;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int len=str.length();
//boolean flag=false;
if(len>=2) {
boolean flag=false;
for(int i=0;i<str.length()-1;i=i+2) {
char ch=str.toLowerCase().charAt(i);
if(ch=='h') {
char ch1=str.toLowerCase().charAt(i+1);
if(ch1=='t') {
flag=true;
}else if(ch1=='h') {
//flag=false;
System.out.println("Team may lose");
return;
}
else {
System.out.println("Invalid string");
return;
}
}
else if(ch=='t') {
char ch1=str.toLowerCase().charAt(i+1);
if(ch1=='h') {
flag=true;
}else if(ch1=='t') {
//flag=false;
System.out.println("Team may lose");
return;
}
else {
System.out.println("Invalid string");
return;
}
}
}
if(flag==true) {
System.out.println("win");
}else {
System.out.println("lose");
}
}
}
}
package sda;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
int arr[]=new int[no];
int diff=0,sum=0;
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<no/2;i++) {
diff=arr[i]-arr[no-i-1];
sum+=diff;
}
System.out.println(sum);
}
}
3.Ascending Array:
package sda;
import java.util.*;
public class assendingarray {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array size:");
int no=sc.nextInt();
int count=0,f=0;
if(no>=2 && no<=10) {
int arr[]=new int[no];
int copy_arr[]=new int[no];
System.out.println("Enter the array element:");
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
copy_arr[i]=arr[i];
}for(int i=0;i<no-1;i++) {
if(arr[i]==arr[i+1]) {
f++;
}
}if(f==no) {
System.out.println("Same array");
return;
}
Arrays.sort(copy_arr);
System.out.println(copy_arr);
for(int i=0;i<no;i++) {
if(copy_arr[i]==arr[i]) {
count++;
}
}
System.out.println(count);
if(count==no) {
for(int i=0;i<no;i++) {
System.out.print(arr[i]+" ");
}
System.out.print(" are in ascending order");
}else {
for(int i=0;i<no;i++) {
System.out.print(arr[i]+" ");
}
System.out.print(" are not in ascending order");
}
}else {
System.out.print("Invalid array size");
}
}
4.Bike Rent:
package sda;
import java.util.*;
public class BIkeRent {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name");
String name=sc.nextLine();
if(name.matches("[a-zA-Z]+")) {
System.out.println("Enter the time duration");
int time=sc.nextInt();
if(time>=1 && time<=24) {
System.out.println("List of the Payment
option;");
System.out.println("1) Visa Card");
System.out.println("2) Rupay card");
System.out.println("3) Master card");
System.out.println("Choose an option\n");
//int op=sc.nextInt();
while(true) {
int op=sc.nextInt();
double bill;
double bill1;
if(op==1) {
if(time>=5) {
bill=(time*120);
bill1=(bill-bill*0.25);
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill1));
}else {
bill=time*120;
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill));
}
}else if(op==2) {
if(time>=5) {
bill=(time*120);
bill1=(bill-bill*0.17);
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill1));
}else {
bill=time*120;
System.out.println("Dear
"+name+" your bill is "+String.format("%.2f",bill));
}
}else if(op==3) {
bill=(time*120);
System.out.println("Dear "+name+" your
bill is "+String.format("%.2f",bill));
}
else {
System.out.println("TRy again");
//break;
//continue;
}
}
}
}
}
}
else {
if(weight<999) {
System.out.println("Invalid Weight");
return;
}
double kg=(Double.parseDouble(w))/1000;
System.out.println("Cake weight is "+kg+"kg");
System.out.println("Cake Flavor is "+flavor);
System.out.println("Cake No is "+order);
double price=450*kg;
System.out.println("Price is "+String.format("%.2f",price));
}
}
}
6.Character Addition
package sda;
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 no=sc.nextInt();
if(no>0) {
String arr[]=new String[no];
for(int i=0;i<no;i++) {
arr[i]=sc.next();
sc.nextLine();
}
int asci[]=new int[no];
for(int i=0;i<no;i++) {
int sum=0;
for(int j=0;j<arr[i].length();j++) {
if(Character.isAlphabetic(arr[i].charAt(j))) {
sum=arr[i].charAt(j)+sum;
}else {
if(Character.isDigit(arr[i].charAt(j)))
{
continue;
}
}
}asci[i]=sum;
}for(int i=0;i<no;i++) {
System.out.println(asci[i]);
}
}else {
System.out.println("Invalid input");
}
}
7.Product Case Count:
package sda;
import java.util.*;
import java.lang.*;
public class CountUpperLowerChar {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String:");
String str=sc.nextLine();
if(str.matches("[a-zA-Z]+")) {
int ucount=0,lcount=0;
for(int i=0;i<str.length();i++) {
if(str.charAt(i)>='A' && str.charAt(i)<='Z') {
ucount++;
}
else {
lcount++;
}
}
//System.out.print(ucount+" "+ lcount);
System.out.print("Product value is "+ucount*lcount);
}else {
System.out.println("Invalid String");
}
}
}
8.Non unique char:
package sda;
import java.util.*;
public class DescString {
public static void main(String[] arge) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
StringBuilder sb=new StringBuilder(str);
for(int i=0;i<sb.length()-1;i++) {
for(int j=i+1;j<sb.length();j++) {
if(sb.charAt(i)==sb.charAt(j)) {
sb.deleteCharAt(j);
j--;
}
}
}
char[] c=sb.toString().toCharArray();
Arrays.sort(c);
sb=new StringBuilder(new String(c));
sb.reverse();
System.out.println(sb);
}
9.Divide by three:
package sda;
import java.util.*;
public class DivdeByThree {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String str=sc.nextLine();
if(!str.matches("[]a-zA-Z ]+")) {
System.out.println("Invalid String");
return;
}else {
int sum=0,c=0;
for(int i=0;i<str.length();i++) {
if((i+1)%3==0) {
//System.out.println(c);
sum+=str.charAt(i);
c++;
}
}if(c==0) {
System.out.println("sum is "+sum);
}else {
System.out.println("sum is "+sum);
}
}
}
10.Fastest Runner:
Input:
Shraddha,9.45
Aanushree 9.34
Output:
Aanushree,9.34
package sda;
import java.util.*
public class FastRunner {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of runners:");
int no=sc.nextInt();
if(no>0 && no<=10) {
System.out.println("Enter the runners:");
String run[]=new String[no];
String name[]=new String[no];
String run_time[]=new String[no];
for(int i=0;i<no;i++) {
run[i]=sc.next();
sc.nextLine();
String time[]=run[i].split(",");
name[i]=time[0];
run_time[i]=time[1];
float time1=Float.parseFloat(time[1]);
if(time1<8.00 || time1>12.00) {
System.out.println(time[1]+" is an invalid
input");
return;
}
}
String min=run_time[0];
int count=0;
float min1=Float.parseFloat(min);
for(int j=0;j<no;j++)
{
float val=Float.parseFloat(run_time[j]);
if(val<=min1)
min1=val;
}
for(int i=0;i<no;i++) {
float val=Float.parseFloat(run_time[i]);
if(val==min1) {
System.out.println(name[i]);
count++;
}
}
if(count==no) {
System.out.println("All the runner have same
time");
return;
}
}else {
System.out.println("Invalid");
}
}
}
11.Fun Count
package sda;
import java.util.*;
public class FunCount {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of element in array");
int no=sc.nextInt();
int count=1;
String arr[]=new String[no];
//String count1[]=new String[no];
for(int i=0;i<no;i++) {
arr[i]=sc.next();
sc.nextLine();
}for(int i=0;i<no;i++) {
for(int j=i+1;j<no;j++) {
if(arr[i].equals(arr[j])) {
count++;
arr[j]="";
}
}if(arr[i]!="") {
System.out.println(arr[i]+"="+count);
}
}
12.Gender Count:
package sda;
import java.util.*;
public class GenderCount {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
sc.close();
int mcount=0,fcount=0;
for(int i=0;i<str.length();i++) {
char ch=str.toLowerCase().charAt(i);
if(ch=='m') {
mcount++;
}else if(ch=='f') {
fcount++;
}else {
System.out.println("Invalid input");
return;
}
}
System.out.println(mcount+" Male");
System.out.println(fcount+" Female");
}
13.Harshad Number
package sda;
import java.util.*;
public class HarShadNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number:");
int no=sc.nextInt();
if(no<=0) {
System.out.println("Invalid Input");
System.exit(0);
}
int rem=0;
int arr[]=new int[no];
int arr1[]=new int[no];
System.out.println("Enter the Element:");
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
arr1[i]=arr[i];
}
int sum1[]=new int[no];
for(int i=0;i<no;i++) {
int sum=0;
while(arr[i]>0) {
rem=arr[i]%10;
sum=sum+rem;
arr[i]/=10;
sum1[i]=sum;
//sum=0;
}}
for(int i=0;i<no;i++) {
if(arr1[i]%sum1[i]==0) {
System.out.println(arr1[i]);
//System.out.println(sum1[i]);
}
}
}
}
14.Longest String:
package sda;
import java.util.*;
public class LongestString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string:");
String str=sc.nextLine();
int findex=0,lindex=0,length=0;
for(int i=0;i<str.length();i++) {
int asci=str.charAt(i);
System.out.println(str.charAt(i)+"1"+asci);
int count=1;
for(int j=i+1;j<str.length();j++) {
if(str.charAt(j)==++asci) {
System.out.println(str.charAt(j)+"2"+asci);
count++;
continue;
}else {
if(count>length) {
System.out.println(count+"3");
findex=i;
lindex=j;
length=count;
break;
}
}
}
}System.out.println(str.substring(findex,lindex));
}
}
15.Maximun No ADDition
package sda;
import java.util.*;
public class MaxAdd {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of element in array:");
int no=sc.nextInt();
int max=0;
System.out.println("Enter the of element of array:");
int arr[]=new int[no];
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
if(max<arr[i]) {
max=arr[i];
}
}
System.out.println(max);
}
package sda;
import java.util.*;
public class MaxIndex {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of elements in arrays: ");
int no=sc.nextInt();
if(no<5) {
System.out.println(no+" is too low");
return;
}
if(no>10) {
System.out.println(no+" is too high");
}
int arr[]=new int[no];
int max=arr[0];
int index=0;
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
if(arr[i]>max) {
max=arr[i];
System.out.println(max+" "+arr[i]);
index=i;
System.out.println(index);
}
}
for(int i=0;i<no;i++) {
if(i!=index) {
System.out.println(arr[i]+index);
}else {
System.out.println(arr[i]);
}
}
}
}else {
System.out.println(no+" exceeds limit");
}
}else {
System.out.println(no+" is too small");
18.Mega Mart
package sda;
import java.util.*;
public class MeghaMart {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the customer id:");
String str=sc.nextLine();
String name=str.substring(0,4);
String mid=str.substring(str.length()-3,str.length());
System.out.println(mid);
String cus_type=str.substring(4,str.length()-3);
System.out.println(cus_type);
//System.out.println(mem_id);
String sname="Mega";
if(!name.equals(sname)) {
System.out.println("Invalid shop name");
return;
}else if(!mid.contains("[0-9]+")){
System.out.println("Invalid member id");
}
else if(mid.contains("[0-9]+")){
int mem_id=Integer.parseInt(str.substring(str.length()-
3,str.length()));
19.MRP
package sda;
import java.util.*;
}
else if(mrp1>=51 && mrp1<=500) {
dis=mrp1-15;
}
else if(mrp1>=501 && mrp1<=5000)
{
dis=mrp1-105;
}
else {
dis=mrp1-1005;
}
}
}
int temp=str.charAt(str.length()-1);
System.out.print(temp+" "+(char)temp);
for(int j=0;j<seq;j++) {
temp=temp-sub;
System.out.print(temp+" "+(char)temp);
if(temp>122) {
// System.out.print(temp+"
"+(char)temp);
temp=temp-26;
System.out.print((char)temp);
}else if(temp<97) {
//System.out.print(temp+"
"+(char)temp);
temp=temp+26;
System.out.print((char)temp);
}else {
System.out.print((char)temp);
}
}
}
package sda;
import java.util.*;
public class NoFind {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbe");
int n=sc.nextInt();
System.out.println("Enter number to be search");
int sea=sc.nextInt();
int count=0;
while(n!=0) {
int rem=n%10;
//System.out.println(rem);
if(rem==sea) {
count++;
}
n=n/10;
//System.out.println(n);
}
System.out.println(count);
}
22.Numerology
package sda;
import java.util.*;
public class Numerology{
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your Name");
String str1=sc.next();
String str=str1.toUpperCase();
int value=0,sum=0;
if(str.matches("[A-Z]+")){
for(int i=0;i<str.length();i++){
value=(int)(str.charAt(i));
sum+=value;
}
if(sum%2==0 && sum%3==0) {
System.out.println(sum+"\n"+str1+" is a Numerology
name");
}
else{
System.out.println(sum+" is not a Numerology name");
return;
}
}
else {
System.out.println(str1+" is an invalid name");
}
}
}
24.Customer Offer
package sda;
import java.util.*;
public class OfferCustomer {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Invalid string");
int no=sc.nextInt();
//sc.nextLine();
String str[]=new String[no];
// String str1[]=new
if(no>3) {
System.out.println("Customer count is too low");
return;
} if(no>0) {
System.out.println("Invalid input");
return;
}if(no<50){
System.out.println("Customer count is too high");
return;
}for(int i=0;i<3;i++) {
str[i]=sc.nextLine();
}
System.out.println("Enter plan id to be searched:");
String pid=sc.next();
for(int i=0;i<no;i++) {
String arr[]=str[i].split(":");
if(pid.equals(arr[1])){
System.out.println(arr[3]+" "+arr[2]);
}
}
}
}
25.Prime Number
package sda;
/*import java.util.*;
public class PrimeNo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int no1=sc.nextInt();
int no2=sc.nextInt();
//int f=0;
if(no1<0 || no2<0) {
System.out.println("Negative integer can not be
prime");
}else if(no1>no2) {
System.out.println("Invalid input");
}else if (no1==0 && no2==1) {
System.out.println("There are no prime number in the
range 0 to 1");
}
else {
for(int i=no1;i<=no2;i++) {
int f=0;
for(int j=1;j<=i;j++) {
if(i%j==0) {
f++;
}
}if(f==2) {
System.out.println(i);
}
}
}
sc.close();
}
}*/
import java.util.*;
public class PrimeNo {
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;
}
}
}
package sda;
import java.util.*;
public class PrimeProduct {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of element in array");
int no=sc.nextInt();
if(no>0) {
System.out.println("Array element");
int pd=1,f=0,f1=0;
int arr[]=new int[no];
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
if(arr[i]>0) {
for(int j=2;j<arr[i]/2;j++) {
if(arr[i]%j==0)
{
f1=1;
}
}if(f1==0) {
System.out.println(arr[i]);
pd=arr[i]*pd;
f++;
}
}else {
System.out.println(arr[i]+" Invalid array
element");
}
}if(f!=0) {
System.out.println(pd);
}else {
System.out.print("No element found in ");
for(int k=0;k<no;k++)
System.out.print(arr[k]);
}
}else {
System.out.println(no+ " Invalid array size");
}
}
27.Public Distribution
package sda;
import java.util.*;
public class PublicDistrubution {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
if(str.length()<2 || str.length()>5) {
System.out.println("invalid input");
return;
}
int amount,total=0;
if(str.matches("[ORWKS]+")) {
for(int i=0;i<str.length();i++) {
if(str.charAt(i)=='O') {
amount=12*2;
total=total+amount;
}else if(str.charAt(i)=='K') {
amount=18*2;
total=total+amount;
}else if(str.charAt(i)=='W') {
amount=22*2;
total=total+amount;
}else if(str.charAt(i)=='R') {
amount=15*2;
total=total+amount;
}else if(str.charAt(i)=='S') {
amount=21*2;
total=total+amount;
}
}
System.out.println(total);
}else {
System.out.println("Invalid input");
}
}
}
28.Reverse String
package sda;
import java.util.*;
public class ReserveString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
String str1[]=str.split(" ");
int len=str1.length;
String rev = "";
//System.out.println(len);
int f=0;
char ch=str1[0].toUpperCase().charAt(0);
if(len>=3 ) {
for(int i=0;i<len;i++)
if(str1[i].matches("[a-zA-Z]+")) {
if(ch==str1[i].toUpperCase().charAt(0)) {
f++;
}
}else {
System.out.println("Invalid word");
return;
}
if(f==len) {
String s1=str1[len-1];
int l1=s1.length();
for(int i=l1-1;i>=0;i--) {
rev=rev+s1.charAt(i);
}
System.out.println(rev+str1[0]);
}else {
String s1=str1[0];
for(int i=s1.length()-1;i>=0;i--) {
rev=rev+s1.charAt(i);
}
System.out.println(rev+str1[len-1]);
}
}else {
System.out.println("Invalid Sentence");
}
}
}
29.Reverse Prime number
package sda;
import java.util.*;
public class ReversePrime {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of values");
int no=sc.nextInt();
if(no>0) {
int c=0;
int arr[]=new int[no];
System.out.println("Enter the numbers");
for(int i=0;i<no;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<no;i++) {
int f=0,f2=0;
//System.out.println(arr[i]);
if(arr[i]>9) {
for(int j=2;j<arr[i]/2;j++) {
if(arr[i]%j==0) {
f=1;
}
}if(f==0) {
//f1=1;
// System.out.println(arr[i]);
int sum=0;
int prime=arr[i];
while(prime!=0)
{
int rem=prime%10;
sum=rem+sum*10;
prime/=10;
}
//System.out.println(sum);
for(int k=2;k<sum/2;k++) {
if(sum%k==0) {
f2=1;
}
}if(f2==0) {
System.out.println(arr[i]);
}
}else {
c++;
}
}else {
System.out.println(arr[i]+" is not valid");
return;
}
}if(c==no) {
System.out.println(no+" is not contain any
reverse prime");
return;
}
}else {
System.out.println(no+" is not valid");
}
}
}
30.Reverse Palindrome
package sda;
import java.util.*;
public class RevPalin {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
String str1[]=str.split(" ");
String rev[]=Arrays.copyOf(str1, str1.length);
for(int i=0;i<str1.length;i++) {
StringBuilder sb=new StringBuilder(rev[i]);
sb.reverse();
rev[i]=sb.toString();
System.out.print(rev[i]+" ");
}
for(int i=0;i<str1.length;i++)
if(rev[i].equals(str1[i]) && str1[i].length()>1) {
System.out.println(rev[i]);
}
31.
Same String
Input:Entertaiment
Substring: en
Output:2
package sda;
import java.util.*;
for(int i=0;i<str.length()-1;i++) {
String str1=str.substring(i,i+sub_str1.length());
System.out.println(str1);
if(sub_str1.equals(str1)) {
count++;
}
}
if(count!=0) {
System.out.println(sub_str1+" found in given string is
"+count);
}
else {
System.out.println("No matching found");
}else {
System.out.println("Invalid string");
package sda;
import java.util.*;
public class SecondLast {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of element in array");
int n=sc.nextInt();
System.out.println("Enter the elements in array");
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println(a[n-2]);
}
}
33.Secreate Code
package sda;
import java.util.*;
public class SecreateCode {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name of the child");
String str=sc.nextLine();
int len=str.length();
if(len>=4) {
if(str.matches("[a-zA-Z]+")) {
System.out.println("Enter the birthdate of the
child");
int no=sc.nextInt();
if(no>=1 && no<=31) {
System.out.println(str.substring(0,4)+"@"+no);
}else {
System.out.println("Secreat code can not be
genrated");
return;
}
}else {
System.out.println("Secreat code can not be
genrated");
return;
}
}else {
System.out.println("Secreat code can not be genrated");
return;
}
}
}
34.
Compare Sum and Product
Output:35,168
package sda;
import java.util.*;
public class SumCompareProduct {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of size of First Arrya:");
int no1=sc.nextInt();
System.out.println("Enter no of size of second Arrya:");
int no2=sc.nextInt();
int arr[]=new int[no1];
int arr1[]=new int[no2];
int sum1=1,rem=0,sum2=0,count=0;
if(no1>0 && no2>0) {
if(no1==no2) {
System.out.println("enter the element in first
array");
for(int i=0;i<no1;i++ ) {
arr[i]=sc.nextInt();
}
for(int i=0;i<no1;i++) {
int a1[]=new int[no1];
a1[i]=arr[i];
while(a1[i]!=0) {
rem=a1[i]%10;
sum1=rem*sum1;
a1[i]=a1[i]/10;
}
pa[i]=sum1;
sum1=1;
System.out.println(pa[i]);
}
System.out.println("enter the element in second
arrays");
for(int j=0;j<no2;j++) {
arr1[j]=sc.nextInt();
}
for(int j=0;j<no2;j++) {
int a2[]=new int[no2];
a2[j]=arr1[j];
while(a2[j]!=0) {
rem=a2[j]%10;
sum2=rem+sum2;
//sa[j]=sum2;
a2[j]=a2[j]/10;
}
sa[j]=sum2;
sum2=0;
System.out.println(sa[j]);
}
for(int k=0;k<no1;k++) {
if(pa[k]==sa[k]) {
System.out.println(arr[k]+","+arr1[k]);
count++;
}
}if(count==0) {
System.out.println("No match Found");
}else {
System.out.println("Array size is
diffrent");
}
}else {
System.out.println("Invalid array size");
}
}
}
36.Toy shop
package sda;
import java.util.*;
public class ToyShop {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of months");
int month=sc.nextInt();
if(month>0) {
System.out.println("Enter the number of toys purchased
monthly");
int toys[]=new int[month];
for(int i=0;i<month;i++) {
toys[i]=sc.nextInt();
if(toys[i]<10) {
System.out.println(toys[i]+" is an invalid
number of toy");
return;
}
}
int max=toys[0];
for(int i=1;i<month;i++) {
if(max<=toys[i]) {
max=toys[i];
}
}
for(int i=0;i<month;i++) {
if(toys[i]==max) {
System.out.println("month "+(i+1));
}
}
}else {
System.out.println("Invalid month");
}
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));
}
}
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)
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
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
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{
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
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
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 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");
}
}
If first letter is not same then reverse first word and concatenate with last word
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
output
barbie=2
doll=2
bike=1
Speed Calculation
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
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
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
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
andLOWERcaseletters
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
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;
}
}
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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
*********************************************************************************************
******************************************************************************
18)HARSHAD NUMBER (Sum of digit completely divide that
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) VIDEO GAME PLAYER (PS1)
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
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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]);
}
}
}
}
*********************************************************************************************
*********************************************************************************************
*
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
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;
}
}
}
****ALTERNATE METHOD***********
Input:
Enter size:5
Enter numbers:
2
34
456
67
123
Explanation:
22=(2+4+6+7+3)
Similarly for invalid box also
}
*********************************************************************************************
*********************************************************************************************
28. ORDER IDENTIFICATION:(check where elements are in ascending
import java.util.Arrays;
import java.util.Scanner;
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;
}
}
***ALTERNATE METHOD***
********************************************************************************************
********************************************************************************************
********************************
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
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;
*******************************************************************************
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);
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);
}
}
**********************************************************************************************
***************************************************
INPUT:
Enter the array size
5
Sample Output:
12
6
21
36
20
Logic:
12*1=12
3*2=6
7*3=21
9*4=36
4*5=20
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size:");
int n=sc.nextInt();
int res=0;
if(n<1 || n>10)
{
System.out.println("Invalid Array Size");
return;
}
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
res=a[i]*(i+1);
System.out.println(res);
}
}
}
**********************************************************************************************
******************************************************************
36) CAR PARKING
Sample Input:
Enter the car number
ap 37 1498
Sample Output
Park the car in 3rd floor
import java.util.*;
ALTERNATE METHOD:
37) DIGIT COUNT
Sample Input:
Sample output
6 appears in 56164 is 2
Explanation:
Digit 6 appears 2 times in the number 56164
package accenture;
import java.util.*;
if(ch[i]==d)
{
count++;
}
}
if(count>1)
{
System.out.println(d+" appears "+n+" is "+count);
}
}
}
38) WORD FINDING (repeated words)
Sample input:
Enter the number of words
5
package practice;
import java.util.*;
public class RepeatedWords {
}
**********************************************************************************************
******************************************************
39) SUM OF INTEGERS AT EVEN OR ODD PLACES
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)
import java.util.*;
public class oddevenindex {
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the size of the array: ");
int n = Integer.parseInt(sc.nextLine());
String[] arr = new String[n];
int[] fin = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextLine();
}
int sum = 0;
int total =0;
if(n%2!=0)
{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
else{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
System.out.println("Output: "+total);
}
}
*****ALTERNATE METHOD*****
package practice;
import java.util.*;
public class SumOfEvenOrOdd {
}
**********************************************************************************************
**********************************************************************
40) SUM OF EVEN ELEMENTS
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.
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
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
package practice;
import java.util.*;
public class SumOfEvenElements {
Sample input:
Enter the string
Entertainment
Enter substring
en
Sample output: 2
package accenture;
import java.util.*;
import java.util.regex.*;
public class countofoccurencesofsubstringinastring {
}
**********************************************************************************************
******************************************************************
42) LARGEST SORTED SUBSTRING FROM INPUT STRING
package com.company;
import java.util.Scanner;
Sample input 1:
A2S3D06D13
Sample output 2:
Seat Number
D13
D14
D15
D16
D17
Sample input 2:
A2S3D02C01
Sample Output2:
Seat Number
C1
C2
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
import java.util.Scanner;
Sample input:
Enter the string
Long live
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);
}
}
JAVA END
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.
Gender Determination :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int count=0,count1=0;
if(!(s.matches("[mfMF]+")))
{
System.out.println("Invalid Input");
return;
}
else
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='m' ||s.charAt(i)=='M')
count++;
else
count1++;
}
System.out.println(count+" "+"Male");
System.out.println(count1+" "+"Female");
}}
LCM OF DAY INTERVAL/LUNCH/DINNER TOGETHER :
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the day interval of person 1:");
int a=sc.nextInt();
System.out.println("Enter the day interval of person 2:");
int b=sc.nextInt();
if(a<=0 || b<=0)
{
System.out.println("Given interval is not valid");
return;
3
}
int lcm;
lcm=(a>b)?a:b;
while(true){
if(lcm%a==0 && lcm%b==0){
System.out.println("person 1 and person 2 will have
their dinner on day "+lcm);
break;
}
lcm++;
}
}}
import java.util.*;
int n=sc.nextInt();
int count=0;
if(n<1 || n>10){
System.exit(0);
else{
for(int i=0;i<n;i++){
int b=sc.nextInt();
a1[i]=b;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int c=sc.nextInt();
a2[i]=c;
else{
System.out.print("Invalid input");
System.exit(0);
}}
for(int i=0;i<n;i++){
int mul=1,r=0,x=a1[i];
while(x>0){
r=x%10;
mul*=r;
5
x=x/10;
for(int j=0;j<n;j++){
int sum=0,p=0,y=a2[j];
while(y>0){
p=y%10;
sum+=p;
y=y/10;
if(mul==sum){
count++;
System.out.print(a1[i]+","+a2[j]);
System.out.println(" ");
}}}
if(count==0){
System.exit(0);
}}}}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
int index=0;
6
int count=0;
while((index=s1.indexOf(s2,index))!=-1){
index=index+1;
count+=1;
}
System.out.println(count);
}
}
Fun Count
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=0){
System.out.println("Invalid Input");
return;
}
if(n>15){
System.out.println("Number exceeds the limit");
return;
}
Map<String,Integer> a=new HashMap<>();
for(int i=0;i<n;i++){
String s=sc.next().toLowerCase();
if(a.containsKey(s))
a.put(s,1+a.get(s));
else
a.put(s,1);
}
Map<String,Integer> tm=new TreeMap<>(a);
for(Map.Entry<String,Integer>data:tm.entrySet()){
System.out.println(data.getKey()+"= "+data.getValue());
}
}
}
7
SPEED ESTIMATION :
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the distance in kilometer");
int km=sc.nextInt();
if(km<0) {
System.out.println("Invalid Input");
return;
}
System.out.println("Enter the time to reach in hours");
int hours=sc.nextInt();
if(hours<0){
System.out.println("Invalid Input");
return;
}
int speed=km/hours;
if(speed<30){
System.out.println("You drive a car at normal speed");
return;
}
int inc=speed-30;
System.out.println("You want to increase a speed "+inc+"
km/hr from a normal speed");
}}
Prime Number
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
int count=0;
8
if(a<0 || b<0){
return;
for(int i=a;i<=b;i++){
count = 0;
for(int j=2;j<=i;j++){
if(i % j == 0)
count = count+1;
if(count == 1)
System.out.print(i+" ");
if(count!=1)
Theatre
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
if(s.length()==6)
{
9
String a=s.substring(0,1);
if(a.matches("[NEWS]+"))
{
String b=s.substring(1,2);
if(b.matches("[ABCDE]+"))
{
String c=s.substring(2,3);
if(c.matches("[1-4]+"))
{
String d=s.substring(3,6);
int num=Integer.parseInt(d);
if(num>0 && num<500)
{
System.out.println("Name "+a);
System.out.println("Block "+b);
System.out.println("Floor number "+c);
System.out.println("Seat number
"+num);
}
else
System.out.println("Invalid Seat number");
}
else
System.out.println("Invalid Floor number");
}
else
System.out.println("Invalid Block");
}
else
System.out.println("Invalid Name");
}
else
System.out.println("Invalid size");
}}
10
Customer details
import java.util.Scanner;
}
}
}
}
Team Win/Loose
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.next();
int count=0;
if(str.length()<3) {
System.out.println("Invalid input");
return;
}
if(str.matches("[htHT]+")) {
String s=str.toLowerCase();
for(int i=0;i<s.length()-2;i++){
if(s.charAt(i)!=s.charAt(i+1) &&
s.charAt(i)==s.charAt(i+2))
count++;
}
if(count==s.length()-2)
System.out.println("Team will win");
else
System.out.println("Team may loose");
}
else {
System.out.println("Invalid Input");
return;
}
}}
Holiday Fun
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
12
int sum=0,sum1=0;
int n=sc.nextInt();
if(n>0){
String[] a=new String[n];
for(int i=0;i<n;i++){
a[i]=sc.next();
}
for(int i=0;i<n;i++){
int len=a[i].length();
if(len%2==0)
{
for(int j=1;j<len;j++){
sum=sum+Integer.parseInt(String.valueOf(a[i].charAt(j)));
j++;
}}
else
{
for(int k=0;k<len;k++){
sum1=sum1+Integer.parseInt(String.valueOf(a[i].charAt(k)));
k++;
}}
}
System.out.println(sum+sum1);
}
else
System.out.println("Invalid size");
}}
Reverse Prime
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0,c=0,f=0,r=0,prime=0;
int n=sc.nextInt();
if(n>0) {
int[] a=new int[n];
13
for(int i=0;i<n;i++) {
int num=sc.nextInt();
if(num>9)
a[i]=num;
else {
System.out.println("Provided "+num+" is not valid");
return;
}}
for(int i=0;i<n;i++) {
count=0;
for(int j=2;j<a[i];j++)
{
if(a[i]%j==0)
count++;
}
if(count==0) {
prime++;
f=a[i];
r=0;
while(a[i]!=0) {
r=r*10;
r=r+a[i]%10;
a[i] =a[i]/10;
}
for(int k=2;k<r;k++) {
if(r%k==0)
c++;
}
if(c==0)
System.out.println(f);
}}}
else {
System.out.println(n+" is less than a limit");
return;
}
if(prime==0)
System.out.println(n+" values are not prime numbers");
}}
14
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int count=0;
System.out.println("Enter the size of array ");
int n=sc.nextInt();
int[] sum=new int[n];
if(n>=1 && n<=10)
{
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();
int[] b=new int[n];
System.out.println("Enter the elements of second array ");
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]%2==0 && b[i]%2==0)
{
count++;
sum[i]=a[i]+b[i];
}
else
sum[i]=0;
}
if(count==0)
System.out.println("There are no even elements");
else
for(int result:sum)
System.out.println(result);
}
else
System.out.println("Invalid");
}}
15
Groceries Bill
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
int a=0,b=0,c=0,d=0,count=0;
if(s.matches("[ORWKorwk]+")) {
for(int i=0;i<s.length();i++) {
for(int j=0;j<s.length();j++) {
if(s.charAt(i)==s.charAt(j))
count++;
}}
if(count==s.length()) {
for(int k=0;k<s.length();k++)
{
if(s.charAt(k)=='O')
a=4*20;
else if(s.charAt(k)=='R')
b=3*5;
else if(s.charAt(k)=='W')
c=2*10;
else
d=5*15;
}
System.out.println(a+b+c+d);
}
else
System.out.println("Duplicates are not allowed");
}
else
System.out.println("Invalid");
}}
16
Cake Order
import java.util.*;
if (cd.matches("[a-zA-Z0-9]*")) {
String w=cd.substring(0, 4);
if(!w.matches("[0-9]*")){
System.out.println("Invalid weight");
return;
}
float wg=Integer.parseInt(w);
if (wg>999) {
float fwg=wg/1000;
double weight= (Math.round(fwg * 100.0) / 100.0);
String o=cd.substring(cd.length()-3);
if (!o.matches("[0-9]*")) {
System.out.println("Invalid order number");
return;
}
int on=Integer.parseInt(o);
if(on>99) {
String flavour=cd.substring(4,cd.length()-3);
double cakePrice=450*weight;
17
}
JAVA
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
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
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) COMBINE NUMBERS / STUDENTS WHOSE ROLLNO ARE ODD
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)+" ");
}
}
}
*********************************************************************************************
*********************************************************************************************
**
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;
}
}
*********************************************************************************************
*********************************************************************************************
****
7) RATING SCORE
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;
andLOWERcaseletters
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
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;
}
}
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
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);
}
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
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");
}
}
}
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");
}
}
*********************************************************************************************
******************************************************************************
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
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
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]);
}
}
}
}
}
****ALTERNATE METHOD****
}
boolean flag = true;
double min=time[0];
int index=0;
for(int i=0;i<n;i++)
{
if(time[i]<min)
{
min=time[i];
//index=i;
}
if(min!=time[i])
{
flag=false;
System.out.println(name[i-1]);
}
}
if (flag) System.out.println(n+" runners have same timing");
}
}
*********************************************************************************************
******************************************************************************
17.REEDME
*********************************************************************************************
******************************************************************************
18)HARSHAD NUMBER (Sum of digit completely divide that
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) VIDEO GAME PLAYER (PS1)
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) ABSOLUTE DIFFERENCE / ALTERNATE SUM DIFFERENCE
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
ADDITION
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. REVERSE FIRST HALF/MIDDLE LETTER 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");
}
}
}
***ALNERNATE METHOD****
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
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");
}
}
}
*********************************************************************************************
*********************************************************************************************
****
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]);
}
}
}
}
*********************************************************************************************
*********************************************************************************************
*
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;
}
}
***ALTERNATE METHOD*****
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Enter the String");
Scanner sc=new Scanner(System. in);
String str=sc.nextLine();
String output="";
String[] word=str.split(" ");
for(int i=0;i<word.length;i++)
{
for(int j=0;j<word[i].length();j++)
{
int count=0;
char c=word[i].charAt(j);
for(int k=0;k<word[i].length();k++)
{
if(c==word[i].charAt(k))
count++;
}
if(count==1)
{
output+=Character.toLowerCase(c);
}
}
output+=" ";
}
System.out.println(output);
}
}
*********************************************************************************************
*********************************************************************************************
*
27. BOX SIZE
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;
}
}
}
****ALTERNATE METHOD***********
Input:
Enter size:5
Enter numbers:
2
34
456
67
123
Explanation:
22=(2+4+6+7+3)
Similarly for invalid box also
}
********************************************************************************************
****************************************************
28. ORDER IDENTIFICATION:(check where elements are in ascending
import java.util.Arrays;
import java.util.Scanner;
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;
}
}
***ALTERNATE METHOD***
********************************************************************************************
********************************************************************************************
********************************
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
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;
*******************************************************************************
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);
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);
}
}
****ALTERNATE METHOD****
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));
}
}
}
}
**********************************************************************************************
***************************************************
INPUT:
Enter the array size
5
Sample Output:
12
6
21
36
20
Logic:
12*1=12
3*2=6
7*3=21
9*4=36
4*5=20
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size:");
int n=sc.nextInt();
int res=0;
if(n<1 || n>10)
{
System.out.println("Invalid Array Size");
return;
}
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
res=a[i]*(i+1);
System.out.println(res);
}
}
}
**********************************************************************************************
******************************************************************
36) CAR PARKING
Sample Input:
Enter the car number
ap 37 1498
Sample Output
Park the car in 3rd floor
import java.util.*;
ALTERNATE METHOD:
Sample Input:
Sample output
6 appears in 56164 is 2
Explanation:
Digit 6 appears 2 times in the number 56164
package accenture;
import java.util.*;
if(ch[i]==d)
{
count++;
}
}
if(count>1)
{
System.out.println(d+" appears "+n+" is "+count);
}
}
Sample input:
Enter the number of words
5
Enter the words in set1:
good
bad
hello
Nothing
North
package practice;
import java.util.*;
public class RepeatedWords {
}
**********************************************************************************************
******************************************************
39) SUM OF INTEGERS AT EVEN OR ODD PLACES
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)
import java.util.*;
public class oddevenindex {
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the size of the array: ");
int n = Integer.parseInt(sc.nextLine());
String[] arr = new String[n];
int[] fin = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextLine();
}
int sum = 0;
int total =0;
if(n%2!=0)
{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
else{
for(int i=0;i<arr.length;i++) {
String num = arr[i];
char[] digi = num.toCharArray();
sum = 0;
if (digi.length % 2 == 0) {
for (int j = 1; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
else
{
for (int j = 0; j < digi.length; j += 2) {
char x = digi[j];
sum = sum + Integer.parseInt(String.valueOf(x));
}
fin[i] = sum;
}
}
for(int x: fin)
total +=x;
}
System.out.println("Output: "+total);
}
}
*****ALTERNATE METHOD*****
package practice;
import java.util.*;
public class SumOfEvenOrOdd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size");
int n = sc.nextInt();
if(n<=0){
System.out.println("Invalid size");
return;
}
System.out.println("Enter the elements:");
int sum = 0;
String str[] = new String[n];
for(int i=0;i<n;i++){
str[i] = sc.next();
}
for(int i = 0;i<n;i++){
String s = str[i];
if(s.length()%2 == 0){
for(int j = 1;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}else{
for(int j = 0;j<s.length();j+=2){
sum+=Integer.parseInt(s.substring(j,j+1));
}
}
}
System.out.println(sum);
}
}
**********************************************************************************************
**********************************************************************
40) SUM OF EVEN ELEMENTS
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.
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
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
package practice;
import java.util.*;
public class SumOfEvenElements {
Sample input:
Enter the string
Entertainment
Enter substring
en
Sample output: 2
package accenture;
import java.util.*;
import java.util.regex.*;
public class countofoccurencesofsubstringinastring {
}
**********************************************************************************************
******************************************************************
42) LARGEST SORTED SUBSTRING FROM INPUT STRING
package com.company;
import java.util.Scanner;
Sample input 1:
A2S3D06D13
Sample output 2:
Seat Number
D13
D14
D15
D16
D17
Sample input 2:
A2S3D02C01
Sample Output2:
Seat Number
C1
C2
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
import java.util.Scanner;
46) THREE’S
Sample input:
Enter the string
Long live
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);
}
}
**********************************************************************************************
**********************************************************************************
47) EVEN POSITION CAPS (CAPITAL LETTERS)
import java.util.Scanner;
import java.util.regex.Pattern;
***ALTERNATE METHOD***
import java.util.*;
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Input string");
String str = sc.next();
if(Pattern.matches("[A-Za-z]+",str)){
if(str.length()<5 || str.length()>20){
System.out.println(str+" is an invalid length");
return;
}
String output = str.charAt(0)+"";
for(int i=0;i<str.length()-1;i++){
if(i%2==0){
output = output+ Character.toString(str.charAt(i+1)).toUpperCase();
}
else
output = output + str.charAt(i+1);
}
System.out.println(output);
}
else{
System.out.println(str+" is an invalid input");
return;
}
}
}
**********************************************************************************************
****************************************************************************************
}
}
}
}
**********************************************************************************************
*******************************************************************************
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
if(km<=0){
System.out.println("Invalid Input");
return;
}
int sp = km/time;
if(sp>30){
int in_sp = sp-30;
System.out.println("You want to increase a speed "+in_sp+" km/hr from a normal speed");
}
else{
System.out.println("You drive a car at a normal speed");
}
}
}
***********************************************************************************************
*********************************************************************************
50) SUM OF SQUARES (RE-TEST)
Sample input
Enter the number of values
4
Enter the values
2
7
9
8
Sample output
130
Explanation
Odd numbers are 7,9 = 7*7 +9*9 = 49+81=130
import java.util.*;
public 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();
if(n<=0)
{
System.out.println(n+" is an invalid size");
return;
}
int a[]=new int[n];
int count=0;
int sum=0;
System.out.println("Enter the values");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(a[i]%2!=0)
count=count+1;
}
if(count<=0)
{
System.out.println("These "+n+" values are not a odd digit");
return;
}
else
{
for(int i=0;i<n;i++)
{
if(a[i]%2!=0)
{
sum=sum+a[i]*a[i];
}
}
System.out.println(sum);
}
}
}
JAVA END
*********************************************************************************************
*********************************************************************************************
********
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;
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;
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;
11.)select policy_id,policy_name,rate_of_interest,bonus_percentage
from policy where mimum_premium_amount = 1200
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'.