Logical Question for JAVA
Logical Question for JAVA
Next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13,
21, 34, 55 etc. The first two numbers of Fibonacci series are 0 and 1.
-----------------------------------------------------------------------------------
------------------------------------------
2) Prime Number :
Prime number is a number that is greater than 1 and divided by 1 or itself only. In
other words, prime numbers can't be divided by other numbers than itself or 1. For
example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.(Number divisible by
themself Only)
3) Palindrome Number :
import java.util.Scanner;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number :");
int num = sc.nextInt();
int rev;
int sum = 0;
int temp;
temp = num;
while(num>0)
{
rev = num % 10;
sum = sum*10+rev;
num = num / 10;
}
if(temp==sum)
{
System.out.println("Number is Palindrome :");
}
else
{
System.out.println("Number is not a Palindrome :");
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------
4) Factorial :
4! = 4*3*2*1 = 24
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number to Find Factorial :");
int num = sc.nextInt();
int fact = 1;
for(int i=1;i<=num;i++)
{
fact = fact * i;
}
System.out.print("Factorial Of Number is : " + fact);
}
}
import java.util.*;
public class Demo
{
public static int fact(int n)
{
if(n==0)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
class Factorial {
public static int calculateFactorial(int n) {
if (n == 0) {
return 1;
} else {
return n * calculateFactorial(n - 1);
}
}
5) Armstrong Number :
An Armstrong number is a positive m-digit number that is equal to the sum of the
mth powers of their digits.
-----------------------------------------------------------------------------------
------------------------------------------
6) Sum of the Digit :
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number :");
int num = sc.nextInt();
int sum = 0;
int digit;
while(num>0)
{
digit = num % 10;
sum = sum + digit;
num = num/10;
}
System.out.println("Sum of the Numbers are :" + sum);
}
}
-----------------------------------------------------------------------------------
------------------------------------------
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number (A) :");
int a = sc.nextInt();
System.out.println("Enter the Number (B) :");
int b = sc.nextInt();
int z;
8) Matrix Multiplication :
Multiplying row * column
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j] = 0;
for(int k=0;k<3;k++)
{
c[i][j] += a[i][k]*b[k][j];
}
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String :");
String str = sc.next();
int vowels = 0;
int consonants = 0;
for(char c : str.toCharArray())
{
if("aeiouAEIOU".indexOf(c) != -1)
{
vowels++;
}
else if(Character.isLetter(c))
{
consonants++;
}
}
System.out.println("Consonants :" + consonants);
System.out.println("Vowels :" + vowels);
}
}
-----------------------------------------------------------------------------------
------------------------------------------
Que :
Sort an Array :
import java.util.*;
import java.util.Arrays;
public class Demo
{
public static void main(String[] args)
{
int arr[] = {1,4,8,2,4,0,9,45,6754,65,41,9087,32};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
-----------------------------------------------------------------------------------
------------------------------------------
Que :
import java.util.Arrays;
public class Demo
{
public static void main(String[] args)
{
int[] arr1 = {1,2,3};
int[] arr2 = {7,8,9};
System.arraycopy(arr1,0,merge,0,arr1.length);
System.arraycopy(arr2,0,merge,arr1.length,arr2.length);
System.out.println(Arrays.toString(merge));
}
}
-----------------------------------------------------------------------------------
------------------------------------------
Que:
import java.util.Arrays;
public class Demo
{
public static void main(String[] args)
{
int[] arr = {5,9,3,9,67,98,25};
import java.util.Arrays;
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size for Array :");
int size = sc.nextInt();
Que :
Remove Duplicates from an Array :
import java.util.Arrays;
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of Array :");
int size = sc.nextInt();
-----------------------------------------------------------------------------------
------------------------------------------
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size of Array");
int n = sc.nextInt();
System.out.println("Enter the Elements in Array :");
int[] arr = new int[n];
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
//end
int max1=0;
int max2 =0;
for(int i=0;i<arr.length;i++)
{
if(arr[i] > max1)
{
max2 = max1;
max1 = arr[i];
}
else if(arr[i] > max1 && arr[i] < max2)
{
max2 = arr[i];
}
}
System.out.println("First :" + max1);
System.out.println("Second :" + max2);
}
}
-----------------------------------------------------------------------------------
------------------------------------------
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size of Array");
int n = sc.nextInt();
System.out.println("Enter the Elements in Array :");
int[] arr = new int[n];
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
//end
System.out.println("");
for(int i=arr.length-1;i>=0;i--)
{
System.out.print(arr[i] + " ");
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size of Array");
int n = sc.nextInt();
System.out.println("Enter the Elements in Array :");
int[] arr = new int[n];
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
//end
int temp;
for(int i=0;i<arr.length-1;i++)
{
for(int j=0;j<arr.length-i-1;j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i] + " ");
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------
import java.util.*;
public class Demo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String :");
String str = sc.nextLine();
int count=0;
int l = str.length();
for(int i=l-1;i>=0;i--)
{
if(str.charAt(i) != ' ')
{
count++;
}
else if(str.charAt(i) == ' ')
{
break;
}
}
System.out.println(count);
}
}
-----------------------------------------------------------------------------------
------------------------------------------
Que :
9) Alphabet Triangle :
10) Write an Example Program for Pointer :
11) Example Program for Array :
12) Example Program for Structures :
13) Bubble Sort :
14) Convert an Array into Zigzag Pattern :