100% found this document useful (1 vote)
448 views37 pages

Core Java Logical Questions

The document contains summaries of 30 Java logical programs. The programs cover a range of topics including string manipulation, number conversion, array processing, prime number identification, and matrix operations. Examples of programs include converting a string to a number without using parseInt(), reversing a string using recursion, checking if two words are anagrams, finding distinct elements between two arrays, and determining if a number is a circular prime.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
448 views37 pages

Core Java Logical Questions

The document contains summaries of 30 Java logical programs. The programs cover a range of topics including string manipulation, number conversion, array processing, prime number identification, and matrix operations. Examples of programs include converting a string to a number without using parseInt(), reversing a string using recursion, checking if two words are anagrams, finding distinct elements between two arrays, and determining if a number is a circular prime.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 37

Core Java Logical Programs

1. Write a program to convert String to Number without using parseInt()?


2. Write a program to reverse a String using recursive function and without using reverse()?
3. Write a program to Convert Full Name to Short Name?
4. Write a program to print alternative prime numbers between 1 to 100?
5. Write a program to find given two words are Anagrams or not?
6. Write a program to find distinct elements from two Arrays?
7. Write a program to check given number is circular prime or not?
8. Write a program to find common words given two Strings?
9. Write a program to Reverse String by words?
10. Write a program to Reverse String by words and also reverse their characters?
11. Write a program to Reverse String words characters?
12. Write a program to Arrange the given word according to alphabetical order ignoring the
lower and upper case?
13. Write a program which converts a Binary number to Decimal number?
14. Write a program which converts a Decimal number to Binary number?
15. Write a program which print difference between Maximum and Minimum numbers of a
Array?
16. Write a program which find out Second Smallest and Second Largest values of given Array?
17. Write a program which find out Nearest Palindrome of given number, if number is not
palindrome?
18. Write a program which print given string in reverse order and also remove all spaces of
string?
19. Write a program which remove conjugative character from a word?
20. Write a program to find the reflection of a given Matrix?
21. Write a program to check that given Matrix is Magic Matrix or Not?
22. Write a program which print first letter of words in UpperCase and all remaining letters in
lower Case in a given string?
23. Write a programs which count all the letters of a given string?
24. Write a program which replaces all vowels with next cosonant of a given string?
25. Write a program to print factorial of a number?
26. Write a program to check given number is Armstrong number or not?
27. Write a program to check given number is strong or not?
28. Write a program to check given number is perfact number or not?
29. Write a Java Program to swap two numbers with using the third variable.
30. Write a Java Program to swap two numbers without using the third variable.
31. Write a Java Program to find whether a string or number is palindrome or not.
32. Write a Java Program for Fibonacci series
33. Write a Java Program to find the duplicate characters in a string
34. Write a Java Program to find the second highest number in an array

/* Write a program to convert String to Number without using parseInt() */

public class ConversionStringToNumberWithoutUsingparseInt {

public static void main(String[] args) {

String str = "12345";


int num = 0;
int len = str.length();
int ind = 0;

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*/

public class ReverseStringUsingRecursiveFunction {


String revStr = "";

String reverseString(String str){

String revStr = reverseLogic(str, str.length()-1);


return revStr;
}

String reverseLogic(String str, int ind){

if(ind>=0){

revStr += str.charAt(ind);
ind--;
reverseLogic(str, ind);
}
return revStr;
}

public static void main(String[] args) {

Scanner in =new Scanner(System.in);


ReverseStringUsingRecursiveFunction rs=new ReverseStringUsingRecursiveFunction();
System.out.println("Enter the String");
String revString=rs.reverseString(in.nextLine());
System.out.println("Reverse String: "+revString);
}
}

↑ Go To Index

/* Write a program to convert full name to short name


Ex- I/p: abdul kalam ajad
O/p: A. K. Ajad */

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

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

if (arr[i] == ' ') {

shortName +=". ";


min = i + 1;

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

/* Write a program to print alternative prime numbers between 1 to 100 ?


O/p: 2 5 11 17 23 31 41 47 59 67 73 83 97 */

public class PrintAlternativePrimeNo {

public static void main(String[] args) {

int flag=1;

for(int ind=1; ind<=100; ind++){


int count=0;

for(int ind1=2; ind1<=ind; ind1++){

if(ind%ind1==0){

count++;
}
}
if(count==1){

if(flag%2!=0){
System.out.print(ind+" ");
}
flag++;
}
}
}
}

↑ Go To Index

/* Write a program to find given two words are Anagrams or not


Ex- Input - Enter the first word - anaarn
Enter the Second word - rnaaan
Output - Both are Anagrams */

import java.util.Scanner;
public class AnagramsWords{
public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter first Word");
String s1=in.nextLine();

char arr1[]=s1.toCharArray();

System.out.println("Enter Second Word");


String s2=in.nextLine();

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

/* Write a program to find distinct elements from two Arrays


Ex- Input - a[5]={1,2,5,4,7,8}
Input - b[4]={3,2,7,9,5}
Output - 1 4 8 3 9*/

import java.util.Scanner;
public class FindDistinctElements{
public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter how many No. will be present in first Array");
int n1=in.nextInt();
int a[]=new int[n1];
System.out.println("Enter the first Array Elements");
for(int i=0; i<=n1-1; i++)
{
int n=in.nextInt();
a[i]=n;
}

System.out.println("Enter how many No. will be present in second Array");


int n2=in.nextInt();
int b[]=new int[n2];
System.out.println("Enter first Array Elements");
for(int i=0; i<=n2-1; i++)
{
int n=in.nextInt();
b[i]=n;
}

for(int i=0; i<=n1-1; i++)


{
int count=0;
for(int j=0; j<=n2-1; j++)
{
count=0;
if(a[i]==b[j])
{
count=0;
break;
}
else
{
count=1;
}
}
if(count==1)
{
System.out.print(a[i]+" ");
}
}
for(int i=0; i<=n2-1; i++)
{
int count=0;
for(int j=0; j<=n1-1; j++)
{
count=0;
if(b[i]==a[j])
{
count=0;
break;
}
else
{
count=1;
}
}
if(count==1)
{
System.out.print(b[i]+" ");
}
}
}
}

↑ Go To Index

/* Write a program to check given number is circular prime or not


Ex- Input - 113 (bcoz here all iteration of 113(113, 131, 311) are prime)
Output - No. is circular prime
*/

import java.util.Scanner;
public class CircularPrime{
public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter the No.");
int num=in.nextInt();

int len=0;
int pow=1;
int d=num;
while(d>0)
{
d=d/10;
len++;
pow=pow*10;
}
pow=pow/10;

int arr[]=new int[len];


int temp;
for(int i=0; i<=len-1; i++)
{
temp=(num%pow*10)+(num/pow);
arr[i]=temp;
num=temp;
}
int c=0;
for(int i=0; i<=len-1; i++)
{
int val=arr[i];
int count=0;
for(int j=1; j<=val/2; j++)
{
if(val%j==0)
{
count++;
}
}

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

/* Write a program to find common words given two strings


Ex- Input - Enter the first String: i love my india
Enter the second String: i love you
Output - i
love */
import java.util.Scanner;

public class FindCommonWords {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter the First String: ");
String str1 = in.nextLine();

System.out.println("Enter the Second String: ");


String str2 = in.nextLine();

char arr1[] = str1.trim().toLowerCase().toCharArray();


char arr2[] = str2.trim().toLowerCase().toCharArray();

int count1 = 0, count2 = 0;

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

if(arr1[i]==' ')
{
if(arr1[i+1]!=' '){

count1++;
}
}
}

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

if(arr2[i]==' ')
{
if(arr2[i+1]!=' '){

count2++;
}
}
}

String string1[]=new String[count1+1];


String string2[]=new String[count2+1];

String str = "";


int index = 0;
for (int i = 0; i < arr1.length; i++) {

if(arr1[i]==' ' || arr1.length-1==i)


{
if(arr1.length-1==i){

str+=arr1[i];
}

string1[index] = str;
str="";
index++;
}
else{
str += arr1[i];
}
}

index=0;
for (int i = 0; i < arr2.length; i++) {

if(arr2[i]==' ' || arr2.length-1==i)


{
if(arr2.length-1==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");
}
}

/* Write a program to Reverse String by words


Ex- Input - i love my india
Output - india my love i*/

import java.util.Scanner;
public class StringReverseByWords {
public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter a String");
String s=in.nextLine();

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++;
}

for(int j=i+1; count >0; j++ )


{
System.out.print(arr[j]);
count--;
}
System.out.print(" ");
}
else
{
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) {

Scanner in=new Scanner(System.in);


System.out.println("Enter a String");
String s=in.nextLine();

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++;

for(int j=wl; count>0; j--)


{
System.out.print(arr[j]);
count--;
}
wl=i-1;
System.out.print(" ");

}
else
{
count++;

}
}
}
}

/* Write a program to Reverse String words characters


Ex- Input- i love my india
Output- i evol ym aidni*/

import java.util.Scanner;
public class StringWordsReverse{
public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter a String");
String s=in.nextLine();

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) {

Scanner in=new Scanner(System.in);


System.out.println("Enter a Character");
String s=in.nextLine();

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

for(int i=0; i<=arr.length-1; i++)


{
System.out.print(arr[i]);
}
}
}
/* Write a program which convert a Binary number to Decimal number
I/p: 1001
O/p: 9 */

import java.util.Scanner;
class BinaryToDecimal{
public static void main(String[] args){

Scanner in=new Scanner(System.in);


System.out.println("Enter the binary no.");
int num=in.nextInt();

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

/* Write a program which convert a Decimal number to Binary number


I/p: 9
O/p: 1001 */
Method-1 (Using String)

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

Method-2 (Using Array)

import java.util.Scanner;
class DecimalToBinaryUsingIntValue {
public static void main(String[] args){

Scanner in=new Scanner(System.in);


System.out.println("Enter the Decimal value");
int num=in.nextInt();

int arr[]=new int[8];


int cursor=0;

while(num!=0)
{
if(num%2==0)
{
num=num/2;
arr[cursor]=0;
}
else
{
num=num/2;
arr[cursor]=1;
}
cursor++;
}

for(int index=arr.length-1; index>=0; index--)


{
System.out.print(arr[index]);
}

}
}

↑ 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){

Scanner in =new Scanner(System.in);

System.out.println("Enter the size of Array");


int size=in.nextInt();
int arr[]=new int[size];

System.out.println("Enter the elements of Array");


for(int i=0;i< size;i++)
{
int n=in.nextInt();
arr[i]=n;
}

int min=arr[0];
int max=arr[size-1];

for(int i=0;i< size;i++)


{
if(min>=arr[i])
{
min=arr[i];
}

if(max<=arr[i])
{
max=arr[i];
}
}

int diff=max-min;

System.out.println("Difference b/w Max and Min values = "+ diff);


}
}

↑ Go To Index

/* Write a program which find out Second Smallest and Second Largest values of given Array */

Method-1
public class SecondLargestAndSecondLowest {

public static void main(String[] args) {

int[] a = { 2, 5, 1, 6, 7, 3, 8, 7 };
int max = a[0];
int min = a[0];

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

if (max < a[i])


max = a[i];
if (min > a[i])
min = a[i];
}

System.out.println("First Largest= "+ max + " First Lowest" + min);

int max2 = min;


int min2 = max;
for (int i = 0; i < a.length; i++) {

if (max2 < a[i] && a[i] != max)


max2 = a[i];
if (min2 > a[i] && a[i] != min)
min2 = a[i];
}

System.out.println("Second Largest= "+ max2 + " Second Lowest" + min2);


}
}

Method-2
import java.util.Scanner;
public class SecondLargestSecondSmallest {
public static void main(String[] args){

Scanner in=new Scanner(System.in);


System.out.println("Enter the size of Array");
int size=in.nextInt();
int arr[]=new int[size];

System.out.println("Enter the elements of Array");


for(int index=0; index<=arr.length-1; index++)
{
int num=in.nextInt();
arr[index]=num;
}

for(int index1=0; index1<=arr.length-1; index1++)


{
for(int index2=0; index2<=arr.length-1; index2++)
{
if(arr[index1]< arr[index2])
{
int temp=arr[index1];
arr[index1]=arr[index2];
arr[index2]=temp;
}
}
}

for(int index=0; index<=arr.length-1; index++)


{
if(arr[index]!=arr[index+1])
{
System.out.println("Second Smallest No. = "+arr[index+1]);
break;
}
}

for(int index=arr.length-1; index>=0; index--)


{
if(arr[index]!=arr[index-1])
{
System.out.println("Second Largest No. = "+arr[arr.length-2]);
break;
}
}
}
}

↑ Go To Index

/* Write a program which find out Nearest Palindrome of given number, if number is not
palindrome

Ex- if number is 123 then nearest palindrome will be 121.


if number is 127 then nearest palindrome will be 131.
if number is 125 then nearest palindrome will be 121 and 131 because both have same
distance from given number.
if number is already palindrome then will be print given number self. */

import java.util.Scanner;
class NearestPalindrom{
public static void main(String ch[]){

Scanner in=new Scanner(System.in);


System.out.println("Enter the no.");
int n=in.nextInt();

Logic logic=new Logic();

/* 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. */

if((n%10) >5 || (n%10==0))


{
int no=logic.nextPalindrom(n);
System.out.println(no);
}
else if((n%10)< 5)
{
int no=logic.previousPalindrom(n);
System.out.println(no);
}
else
{
int no1=logic.previousPalindrom(n);
int no2=logic.nextPalindrom(n);
System.out.println(no1);
System.out.println(no2);
}
}
}

class Logic{
public int nextPalindrom(int num){
int rev, temp,i;

for (i = num; i >= num; i++)


{
temp = i;
rev = 0;

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;

for (i = num; i <= num; i--)


{
temp = i;
rev = 0;

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){

Scanner in=new Scanner(System.in);


System.out.println("Enter the String");
String str=in.nextLine();

char arr[]=str.toCharArray();

int l=arr.length-1;

char arr1[]=new char[l];

int count=0;
int k=0;

for(int i=0;i<=l;i++)
{
if(arr[i]!=' ')
{
arr1[k]=arr[i];
}
else
{
k--;
count++;
}
k++;
}

for(int j=l-count; j>=0;j--)


{
System.out.print(arr1[j]);
}
System.out.print("\n");
}
}

/* Write a program which remove conjugative character from a word */

import java.util.Scanner;
class RemoveConjigativeChar{
public static void main(String[] args){

Scanner in=new Scanner(System.in);


System.out.println("Enter the String");
String s=in.nextLine();
char arr[]=s.toCharArray();
char arr1[]=new char[arr.length];

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

/* Write a program to find the reflection of a given Matrix */

import java.util.Scanner;
public class ReflectionMatrix{
public static void main(String[] args){

Scanner in=new Scanner(System.in);


System.out.println("No. of rows = ");
int r=in.nextInt();
System.out.println("No. of columns = ");
int c=in.nextInt();

int arr[][]=new int[r][c];


System.out.println("Enter the Array elements");
for(int row=0; row< r;row++)
{
for(int col=0; col< c; col++)
{
int n=in.nextInt();
arr[row][col]=n;
}
}

System.out.println("Your input Matrix is =");


for(int row=0; row< r; row++)
{
for(int col=0; col< c;col++)
{
System.out.print(arr[row][col]+" ");
}
System.out.println();
}

System.out.println("\n 1. Choose 1 for Top Reflection \n 2. Choose 2 for Bottom


Reflection \n"
3. Choose 3 for Left Reflection \n 4. Choose 4 for Right Reflection \n");

System.out.println("Choose Any one and see reflected value");


int choice=in.nextInt();

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

/* Write a program to check that given Matrix is Magic Matrix or Not */


/* Magic Matric - A magic matic is an NxN Matrix(Square matrix) in which every row, column,
and diagonal add up to
the same number.
Ex- 2 7 6 = 15
9 5 1 = 15
4 3 8 = 15
//|| || ||\\
15 15 15 15 15

import java.util.Scanner;
class MagicMatrix {
public static void main(String[] args){

Scanner in =new Scanner(System.in);


System.out.println("Enter the size of Square Matrix");
int size=in.nextInt();
int arr[][]=new int[size][size];

System.out.println("Enter the elements of Array");


for(int i=0;i< size;i++)
{
for(int j=0;j< size;j++)
{
int n=in.nextInt();
arr[i][j]=n;
}
}

System.out.println("Your Matrix is = ");


for(int i=0;i< size;i++)
{
for(int j=0;j< size;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}

int rsum[]=new int[size];


int csum[]=new int[size];
int d1=0,d2=0;

for(int i=0;i< size;i++)


{
int r=0,c=0;
for(int j=0;j< size;j++)
{
r=r+arr[i][j];
c=c+arr[j][i];
if(i==j)
{
d1=d1+arr[i][j];
}
if((i+j)==(size-1))
{
d2=d2+arr[i][j];
}
}
rsum[i]=r;
csum[i]=c;
}

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){

Scanner in=new Scanner(System.in);


System.out.println("Enter the String");
String s=in.nextLine();
char arr[]=s.toCharArray();

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){

Scanner in=new Scanner(System.in);


System.out.println("Enter the word");
String w=in.nextLine();
char arr[]=w.toCharArray();

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;

public class VowelReplaceWithNextConsonant {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter the String");
String str=in.nextLine();

char arr[]=str.toCharArray();

for(int ind=0; ind<=arr.length-1; ind++){

if(arr[ind]=='a' || arr[ind]=='e' || arr[ind]=='i' || arr[ind]=='o' || arr[ind]=='u'){

arr[ind]=(char) (arr[ind]+1);
}
}

String string = new String(arr);


System.out.println(string);
}
}

/* Write a program to find factorial of a given number */

import java.util.Scanner;

public class Factorial {

int fact=1;
int factLogic(int num){

if(num>1){
fact *=num;
num--;
factLogic(num);
}
return fact;
}

public static void main(String[] args) {

Scanner in=new Scanner(System.in);


System.out.println("Enter The No.: ");
int num=in.nextInt();

System.out.println(new Factorial().factLogic(num));
}

}
/* Write a program to check given number is armstrong number or not*/

import java.util.Scanner;

public class ArmstrongNumber {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);


System.out.println("Enter The No.: ");
int num = in.nextInt();
int no = num;
int qube = 1;
int armstrongNum = 0;

while (no > 0) {

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

/* Write a program to check given number is strong number or not*/

import java.util.Scanner;

public class StrongNumber {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);


System.out.println("Enter The No.: ");
int num = in.nextInt();
int no = num;
int fact = 1;
int strongNum = 0;

while (no > 0) {

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

/* Write a program to check given number is perfactnumber or not*/

import java.util.Scanner;

public class PerfectNumber {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);


System.out.println("Enter The No.: ");
int num = in.nextInt();

int factorAddition = 0;

for (int i = 1; i < num; i++) {

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.*/

public class SwapTwoNumbers {


public static void main(String[] args) {
// TODO Auto-generated method stub
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping" + x + y);
temp = x;
x = y;
y = temp;
System.out.println("After Swapping" + x + y);
}
}

/* Write a Java Program to swap two numbers without using the third variable.*/

Public class SwapTwoNumberWithoutThirdVariable


{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping without third variable\nx = "+x+"\ny = "+y);
}
}
/* Write a Java Program to find whether a string or number is palindrome or not.*/

public class Palindrome {


public static void main (String[] args) {
String original, reverse = "";
Scanner in = new Scanner(System.in);
int length;
System.out.println("Enter the number or String");
original = in.nextLine();
length = original.length();
for (int i =length -1; i>=0; i--) {
reverse = reverse + original.charAt(i);
}
System.out.println("reverse is:" +reverse);
if(original.equals(reverse))
System.out.println("The number is palindrome");
else
System.out.println("The number is not a palindrome");
}
}
/* Write a Java Program for Fibonacci series.*/

public class Fibonacci {


public static void main(String[] args) {
int num, a = 0,b=0, c =1;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of times");
num = in.nextInt();
System.out.println("Fibonacci Series of the number is:");
for (int i=0; i<=num; i++) {
a = b;
b = c;
c = a+b;
System.out.println(a + ""); //if you want to print on the same line, use print()
}
}
}
/* Write a Java Program to find the duplicate characters in a string.*/

public class DuplicateCharacters {


public static void main(String[] args) {
// TODO Auto-generated method stub
String str = new String("Sakkett");
int count = 0;
char[] chars = str.toCharArray();
System.out.println("Duplicate characters are:");
for (int i=0; i<str.length();i++) {
for(int j=i+1; j<str.length();j++) {
if (chars[i] == chars[j]) {
System.out.println(chars[j]);
count++;
break;
}
}
}
}
}

/* Write a Java Program to find the second highest number in an array.*/

public class SecondHighestNumberInArray {


public static void main(String[] args)
{
int arr[] = { 14, 46, 47, 94, 94, 52, 86, 36, 94, 89 };
int largest = arr[0];
int secondLargest = arr[0];
System.out.println("The given array is:");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + "\t");
}
for (int i = 0; i < arr.length; i++)
{
if (arr[i] > largest)
{
secondLargest = largest;
largest = arr[i];
}
else if (arr[i] > secondLargest && arr[i] != largest)
{
secondLargest = arr[i];
}
}
System.out.println("\nSecond largest number is:" + secondLargest);
}
}

All the Best

Manikanta Pulagam

You might also like