Core Java Logical Questions
Core Java Logical Questions
while(len>0){
char c = str.charAt(ind); //it will return one character which existing in respective index
int digit = (int) c - '0'; //Here i am casting character into the integer
//So it will return ASCII value of respective number.(eg. ASCII value of '2' 50)
//And ASCII value of '0' 48. So we can get our digit subtract by zero ASCII value.
num = num*10 + digit;
len--;
ind++;
}
System.out.println(num);
}
}
↑ Go To Index
/* Write a program to reverse String using recursive function and without using reverse()
I/p: i love my india
O/p: aidni ym evol i*/
if(ind>=0){
revStr += str.charAt(ind);
ind--;
reverseLogic(str, ind);
}
return revStr;
}
↑ Go To Index
import java.util.Scanner;
class ToConvertFullNameToShortName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
String fullName = sc.nextLine().toLowerCase().trim();
char[] arr = fullName.toCharArray();
String shortName="";
int min = 0;
shortName += (char) (arr[min] - 32);
while(arr[min]==' '){
min++;
i++;
}
shortName += (char) (arr[min] - 32);
}
if (i == arr.length - 1) {
for (int k = min+1; k < arr.length; k++) {
shortName +=arr[k];
}
}
}
System.out.println("Full Name : "+fullName);
System.out.println("Short Name : "+shortName);
}
}↑ Go To Index
int flag=1;
if(ind%ind1==0){
count++;
}
}
if(count==1){
if(flag%2!=0){
System.out.print(ind+" ");
}
flag++;
}
}
}
}
↑ Go To Index
import java.util.Scanner;
public class AnagramsWords{
public static void main(String[] args) {
char arr1[]=s1.toCharArray();
char arr2[]=s2.toCharArray();
for(int i=0; i<=arr1.length-1; i++)
{
for(int j=0; j<=arr2.length-1; j++)
{
if(arr1[i]==arr2[j])
{
arr1[i]=' ';
arr2[j]=' ';
}
}
}
int count=0;
for(int i=0; i<=arr1.length-1; i++)
{
if(arr1[i]!=' ')
{
count++;
}
}
if((count==0)&&(arr1.length==arr2.length))
{
System.out.println("Both are Anagrams");
}
else
{
System.out.println("Both are not Anagrams");
}
}
}
↑ Go To Index
import java.util.Scanner;
public class FindDistinctElements{
public static void main(String[] args) {
↑ Go To Index
import java.util.Scanner;
public class CircularPrime{
public static void main(String[] args) {
int len=0;
int pow=1;
int d=num;
while(d>0)
{
d=d/10;
len++;
pow=pow*10;
}
pow=pow/10;
if(count==1)
{
c++;
}
}
if(c==len)
{
System.out.println("No. is Circuler prime");
}
else
{
System.out.println("No. is not Circuler prime");
}
}
}
↑ Go To Index
if(arr1[i]==' ')
{
if(arr1[i+1]!=' '){
count1++;
}
}
}
if(arr2[i]==' ')
{
if(arr2[i+1]!=' '){
count2++;
}
}
}
str+=arr1[i];
}
string1[index] = str;
str="";
index++;
}
else{
str += arr1[i];
}
}
index=0;
for (int i = 0; i < arr2.length; i++) {
str+=arr2[i];
}
string2[index] = str;
str="";
index++;
}
else{
str += arr2[i];
}
}
int counter = 0;
System.out.println("Common Words:");
for (int i = 0; i < string1.length; i++) {
for (int j = 0; j < string2.length; j++) {
if (string1[i].equals(string2[j])) {
counter++;
System.out.println(string1[i]);
}
}
}
if(counter==0){
System.out.println("Not found any common word");
}
}
import java.util.Scanner;
public class StringReverseByWords {
public static void main(String[] args) {
char arr[]=s.toCharArray();
int count=0;
for(int i=arr.length-1; i >=0; i--)
{
if(arr[i]==' '||i==0)
{
if(i==0)
{
i--;
count++;
}
/* Write a program to Reverse String by words and also reverse their characters
Ex- Input- i love my india
Output- aidni ym evol i*/
import java.util.Scanner;
public class StringReverseByWordsAndWordsAlsoReversed {
public static void main(String[] args) {
char arr[]=s.toCharArray();
int count=0;
int wl=arr.length-1;
for(int i=arr.length-1; i >=0; i--)
{
if(arr[i]==' '||i==0)
{
if(i==0)
count++;
}
else
{
count++;
}
}
}
}
import java.util.Scanner;
public class StringWordsReverse{
public static void main(String[] args) {
char arr[]=s.toCharArray();
int count=0;
for(int i=0; i<=arr.length-1; i++)
{
if(arr[i]==' '||i==arr.length-1)
{
if(i==arr.length-1)
{
i++;
count++;
}
for(int j=i-1; count>0; j--)
{
System.out.print(arr[j]);
count--;
}
System.out.print(" ");
}
else
{
count++;
}
}
}
}
↑ Go To Index
/* Write a program to Arrange the given word according to alphabetical order ignoring the lower
and upper case
I/p: india
O/p: adiin */
import java.util.Scanner;
public class CharSorting{
public static void main(String[] args) {
char arr[]=s.toLowerCase().toCharArray();
for(int i=0; i<=arr.length-1; i++)
{
for(int j=i+1; j<=arr.length-1; j++)
{
if(arr[i]>arr[j])
{
char temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
import java.util.Scanner;
class BinaryToDecimal{
public static void main(String[] args){
int temp=0;
int pow=1;
int out=0;
while(num!=0)
{
int n=num%10;
if(temp!=0)
{
pow=pow*2;
out=out+(n*pow);
}
else
{
temp=1;
out=out+(n*temp);
}
num=num/10;
}
System.out.println(out);
}
}
import java.util.Scanner;
class DecimalToBinary{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("Enter the decimal No.");
int num=in.nextInt();
String s="";
while(num!=0)
{
if(num%2==0)
{
num=num/2;
s="0"+s;
}
else
{
num=num/2;
s="1"+s;
}
}
System.out.println(s);
}
}
↑ Go To Index
import java.util.Scanner;
class DecimalToBinaryUsingIntValue {
public static void main(String[] args){
while(num!=0)
{
if(num%2==0)
{
num=num/2;
arr[cursor]=0;
}
else
{
num=num/2;
arr[cursor]=1;
}
cursor++;
}
}
}
↑ Go To Index
/* Write a program which print difference between Maximum and Minimum numbers of a
Array
I/p: Enter the size of Array : 5
Enter the elements of Array: {21,4,2,64,32}
O/p: 64-2=62 */
import java.util.Scanner;
class MaxMinSum{
public static void main(String[] args){
int min=arr[0];
int max=arr[size-1];
if(max<=arr[i])
{
max=arr[i];
}
}
int diff=max-min;
↑ Go To Index
/* Write a program which find out Second Smallest and Second Largest values of given Array */
Method-1
public class SecondLargestAndSecondLowest {
int[] a = { 2, 5, 1, 6, 7, 3, 8, 7 };
int max = a[0];
int min = a[0];
Method-2
import java.util.Scanner;
public class SecondLargestSecondSmallest {
public static void main(String[] args){
↑ Go To Index
/* Write a program which find out Nearest Palindrome of given number, if number is not
palindrome
import java.util.Scanner;
class NearestPalindrom{
public static void main(String ch[]){
/* Here we are finding out last digit of given number and that number we are comparing with 5
because
if that last digit greater than 5 then the nearest palindrome will be exist in forward direction
and
if last digit of number smaller than 5 then the nearest palindrome will be exist in backward
direction and
if last digit equal to 5 then nearest palindrome numbers will be exist in both direction. */
class Logic{
public int nextPalindrom(int num){
int rev, temp,i;
while (temp != 0)
{
int remainder = temp % 10;
rev = (rev * 10) + remainder;
temp = temp / 10;
}
if (rev == i)
{
break;
}
}
return i;
}
public int previousPalindrom(int num){
int rev, temp,i;
while (temp != 0)
{
int remainder = temp % 10;
rev = (rev * 10) + remainder;
temp = temp / 10;
}
if (rev == i)
{
break;
}
}
return i;
}
}
↑ Go To Index
/* Write a program which print given string in reverse order and also remove all spaces of
string */
import java.util.Scanner;
class ReverseString {
public static void main(String[] args){
char arr[]=str.toCharArray();
int l=arr.length-1;
int count=0;
int k=0;
for(int i=0;i<=l;i++)
{
if(arr[i]!=' ')
{
arr1[k]=arr[i];
}
else
{
k--;
count++;
}
k++;
}
import java.util.Scanner;
class RemoveConjigativeChar{
public static void main(String[] args){
int count=0;
int k=0;
int i;
for(i=0; i< arr.length-1; i++)
{
arr1[k]=arr[i];
if(arr[i]==arr[i+1])
{
i++;
count++;
}
k++;
}
arr1[k]=arr[i];
for(int j=0;j<=(arr.length-1)-count;j++)
{
System.out.print(arr1[j]);
}
}
}
↑ Go To Index
import java.util.Scanner;
public class ReflectionMatrix{
public static void main(String[] args){
if(choice==1 || choice==2)
{
for(int row=r-1; row >=0; row--)
{
for(int col=0; col< c; col++)
{
System.out.print(arr[row][col]+" ");
}
System.out.println();
}
}
else if(choice==3 || choice==4)
{
for(int row=0; row< r; row++)
{
for(int col=c-1; col >=0; col--)
{
System.out.print(arr[row][col]+" ");
}
System.out.println();
}
}
else
{
System.out.println("Invalid input, Please choose another choice");
}
}
}
import java.util.Scanner;
class MagicMatrix {
public static void main(String[] args){
boolean flag=false;
for(int i=0;i< size;i++)
{
if(rsum[i]==csum[i] && rsum[i]==d1 &&
rsum[i]==d2 && csum[i]==d1 && csum[i]==d2 && d1==d2)
{
flag=true;
}
else
{
flag=false;
break;
}
}
if(flag)
{
System.out.println("Matrix is Magic Matrix");
}
else
{
System.out.println("Matrix is not Magic Matrix");
}
}
}
/* Write a program which print first letter of words in UpperCase and all remaining letters in
lowerCase in a given string */
import java.util.Scanner;
class FirstAlphaInUpperCase{
public static void main(String[] args){
for(int i=0;i<=arr.length-1;i++)
{
if(i==0)
{
if(arr[i]>=97 && arr[i]<=122)
{
arr[i]=(char) (arr[i]-32);
}
}
else if(arr[i]==' ')
{
i++;
if(arr[i]>=97 && arr[i]<=122)
{
arr[i]=(char) (arr[i]-32);
}
}
else
{
if(!(arr[i]>=97 && arr[i]<=122))
{
arr[i]=(char) (arr[i]+32);
}
}
}
for(int i=0;i<=arr.length-1;i++)
{
System.out.print(arr[i]);
}
}
}
/* Write a program which count all the letters of a given string */
import java.util.Scanner;
class CountAlpha {
public static void main(String[] args){
int count=1;
int i;
for(int j=0;j< arr.length;j++)
{
if(arr[j]!=' ')
{
for(i=j+1;i<arr.length;i++)
{
if(arr[j]==arr[i])
{
count++;
arr[i]=' ';
}
}
System.out.println(arr[j]+"="+count);
count=1;
}
}
}
}
↑ Go To Index
/* Write a program which replace all vowel with next cosonant of a given string */
import java.util.Scanner;
char arr[]=str.toCharArray();
arr[ind]=(char) (arr[ind]+1);
}
}
import java.util.Scanner;
int fact=1;
int factLogic(int num){
if(num>1){
fact *=num;
num--;
factLogic(num);
}
return fact;
}
System.out.println(new Factorial().factLogic(num));
}
}
/* Write a program to check given number is armstrong number or not*/
import java.util.Scanner;
int n = no % 10;
qube = n * n * n;
armstrongNum += qube;
no = no / 10;
qube = 1;
}
if (num == armstrongNum)
System.out.println(num + " is a armstrong No.");
else
System.out.println(num + " is not a armstrong No.");
↑ Go To Index
import java.util.Scanner;
int n = no % 10;
for (int i = 1; i <= n; i++) {
fact *= i;
}
strongNum += fact;
no = no / 10;
fact = 1;
}
if (num == strongNum)
System.out.println(num + " is a strong No.");
else
System.out.println(num + " is not a strong No.");
import java.util.Scanner;
int factorAddition = 0;
if (num % i == 0) {
factorAddition += i;
}
}
if (factorAddition == num)
System.out.println(num + " is a perfact No.");
else
System.out.println(num + " is not a perfact No.");
}
}
/* Write a Java Program to swap two numbers with using the third variable.*/
/* Write a Java Program to swap two numbers without using the third variable.*/
Manikanta Pulagam