Final
Final
import java.util.Scanner;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(reverseWords(str));
sc.close(); // Close the scanner to avoid resource leaks
}
public static String reverseWords(String str) {
String[] words = str.split(" ");
StringBuilder result = new StringBuilder();
for (String word : words) {
result.append(new
StringBuilder(word).reverse().toString()).append(" ");
}
return result.toString().trim();
}
}
import java.util.Scanner;
public class Mnain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(reverseWords(str));
sc.close(); // Close the scanner to avoid resource leaks
}
public static String reverseWords(String str){
String words[]=str.split(" ");
String first=new StringBuilder(words[0]).reverse().toString();
words[0]=first;
String second=new StringBuilder(words[words.length-
1]).reverse().toString();
words[words.length-1]=second;
String stre=String.join(" ",words);
return stre.toString();
}
}
Palindrome
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(isPalindrome(str));
sc.close();
}
Roman number
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
return true;
}
}
return maxPorfit;
}
1A)
import java.util.*;
public class main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int n=str.length();//you can you int n=sc.nextInt();
sc.nextLine();
char ch=sc.nextLine().charAt(0);
if(n>0){
int count=0;
for(int i=0;i<n;i++){
if(ch==str.charAt(i))
count++;
}
System.out.println(count);
}
}
}
2Answer):
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int H=sc.nextInt();
int V=sc.nextInt();
int Vn=sc.nextInt();
int N=sc.nextInt();// not given if exam not given take one
double e=(double)V/Vn;
double HF=H*Math.pow(e, 2*N);
System.out.println((int)HF);//s.o.p((int)H*Math.pow((double)V/Vn,2*N))
}
}
q
4)import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int res=evenbit(arr);
System.out.println(res);
sc.close();
}
public static int evenbit(int arr[]){
int result=0;
int sum=0;
int sum2=0;
for(int i=0;i<arr.length;i++){
if(i%2!=0)
sum+=arr[i];
else
sum2^=arr[i];
}
result=sum-sum2;
return result;
}
}
5A)
import java.util.*;
public class pairs{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int A[]=new int[N];
for(int i=0;i<N;i++){
A[i]=sc.nextInt();
}
int result[]=maxPair(A,N);
if(result.length>0){
System.out.println(Arrays.toString(result));
}
else{
System.out.println("not found");
}
}
public static int[] maxPair(int A[],int N){
Arrays.sort(A);
int left=0;
int right=N-1;
int maxi=Integer.MIN_VALUE;
int resultPair[]=new int[2];
while(left<right){
int sum=A[left]+A[right];
if(sum==18 && A[right]>A[left]){
int product=A[right]*A[left];
if(product>maxi){
maxi=product;
resultPair[0]=A[right];
resultPair[1]=A[left];
}
left++;
right--;
}
else if(sum<18){
left++;
}
else{
right--;
}
}
return maxi==Integer.MIN_VALUE? new int[]{}:resultPair;
}
}
6A)
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(fibno(n));
}
public static int fibno(int n){
if(n<0){
return -1;
}
if(n==0||n==1){
return 1;
}
int a=1;
int b=1;
for(int i=2;i<=n;i++){
int temp=(a*a+b*b)%47;
a=b;
b=temp;
}
return b;
}
}
7A):
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String app[]=sc.nextLine().split(" ");
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(app[n-i-1]);
}
int result=reverseEven(arr);
System.out.println(result);
sc.close();
}
public static int reverseEven(int arr[]){
if(arr.length<=0){
return -1;
}
int sum=0;
for(int i=0;i<arr.length;i++){
if(i%2==0){
sum=sum+arr[i];
}
}
return sum;
}
}
8Answe):
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String file[]=sc.nextLine().split(" ");
int n=sc.nextInt();
int result=versions(file);
System.out.println(result);
sc.close();
}
public static int versions(String file[]){
int version=-1;
for(int i=0;i<file.length;i++){
String filename =file[i];
if(filename.matches("File_\\d+")){
int newver=Integer.parseInt(filename.split("_")[1]);
version=Math.max(version,newver);
}
}
return version;
}
}
10A)
import java.util.Scanner;
public class CandyShopping {
public static int maxCandies(int n, int[] A, int M) {
int count = 0;
for (int i = 0; i < n; i++) {
if(M>A[i]){
if (A[i] % 5 == 0) {
count++;
}
else if (M >= A[i]) {
M -= A[i];
count++;
}
else {
break;
}
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = sc.nextInt();
}
int M = sc.nextInt();
System.out.println(maxCandies(n, A, M));
}
}
Or
import java.util.Scanner;
int M = sc.nextInt();
System.out.println(maxCandies(n, A, M));
}
}
Or
import java.util.Scanner;
public class CandyShopping {
public static int maxCandies(int n, int[] A, int M) {
int count = 0;
for (int i = 0; i < n; i++) {
if (A[i] % 5 == 0) {
count++; // Buy without spending money
} else if (M >= A[i]) {
M -= A[i]; // Buy if enough money
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = sc.nextInt();
}
int M = sc.nextInt();
System.out.println(maxCandies(n, A, M));
}
}
12Answe):
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int D=sc.nextInt();
int result = (int)(Math.PI * D * D);
//float result=3.14f*D*D;
//int resultt=(int)result;
//because asking about the tree shadow of upper which like circle so so
we keep logic and read all formulas
System.out.println(result);
}
}
14 ans):
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String firstname=sc.nextLine();
String Last=sc.nextLine();
System.out.println(firstname.toLowerCase()+"
"+Last.toUpperCase());
}
}
Orrr if full name is given
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String str[]=sc.nextLine().split(" ");
str[0]=str[0].toLowerCase();
str[str.length-1]=str[str.length-1].toUpperCase();
String result=String.join(" ",str);
System.out.println(result);
}
}
15A):
import java.util.Scanner;
public class PicnicPrimeSum {
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static int sumOfPrimes(int N) {
if (N <= 1) {
return 0; // No primes for N = 0 or N = 1
}
int sum = 0;
for (int i = 2; i <= N; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input: The number N
int N = sc.nextInt();
// Output: Sum of all primes till N
System.out.println(sumOfPrimes(N));
}
}
16A):
import java.util.*;
public class Even{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<0){
System.out.println("error because negtavie number");
return;
}
String a=Integer.toString(n);
a=a.replace("0","5");
System.out.println(Integer.parseInt(a));
}
}
17answe):
import java.util.HashSet;
import java.util.Scanner;
public class UniquePairsMultipleOfThree {
public static int countAndPrintUniquePairs(int[] A, int N) {
HashSet<String> pairs = new HashSet<>();
int count = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if ((A[i] * A[j]) % 3 == 0) {
String pair = Math.min(A[i], A[j]) + "," +
Math.max(A[i], A[j]);
if (!pairs.contains(pair)) {
pairs.add(pair);
System.out.println("(" + A[i] + ", " + A[j] + ")");
count++;
}
}
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = sc.nextInt();
}
int result = countAndPrintUniquePairs(A, N);
System.out.println("Total number of unique pairs: " + result);
}
}