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