0% found this document useful (0 votes)
20 views

Logical Question for JAVA

Uploaded by

harshmavlikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Logical Question for JAVA

Uploaded by

harshmavlikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

1) Fibonacci Series :-

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.

public class Demo


{
public static void main(String[] args)
{
int a=0;
int b=1;
int c;
int count=10;

System.out.print(a + " " + b);


for(int i=2;i<count;i++)
{
c = a + b ;
System.out.print(" " + c);
a = b;
b = c;
}
}
}

-----------------------------------------------------------------------------------
------------------------------------------

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)

public class Demo


{
public static void main(String[] args)
{
int ct = 0, n = 0, i = 1, j = 1;
while(n < 25)
{
j = 1;
ct = 0;
while(j<=i)
{
if(i % j == 0)
ct++;
j++;
}
if(ct == 2)
{
System.out.print(" " + i);
n++;
}
i++;
}
}
}
-----------------------------------------------------------------------------------
------------------------------------------

3) Palindrome Number :

A palindrome number is a number that is same after reverse.

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

public static void main(String[] args)


{
int num = 4;
int result = fact(num);
System.out.println("factorial of number :" + result);
}
}

class Factorial {
public static int calculateFactorial(int n) {
if (n == 0) {
return 1;
} else {
return n * calculateFactorial(n - 1);
}
}

public static void main(String[] args) {


int num = 10;
int result = calculateFactorial(num);
System.out.println("Factorial of " + num + " = " + result);
}
}
-----------------------------------------------------------------------------------
------------------------------------------

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

7) Swap Two Numbers :


Swapp the number between two Numbers

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;

System.out.println("Before Swapping :" + a + " " + b);


z = a;
a = b;
b = z;
System.out.println("After Swapping :" + a + " " + b);
}
}
-----------------------------------------------------------------------------------
------------------------------------------

8) Matrix Multiplication :
Multiplying row * column

public class Demo


{
public static void main(String[] args)
{
int a[][] = {{1,1,1},{2,2,2},{3,3,3}};
int b[][] = {{1,1,1},{2,2,2},{3,3,3}};
int c[][] = new int[3][3];

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

Que : Count vowels and Consonants

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 :

Merge Two Arrays :

import java.util.Arrays;
public class Demo
{
public static void main(String[] args)
{
int[] arr1 = {1,2,3};
int[] arr2 = {7,8,9};

int[] merge = new int[arr1.length + arr2.length];

System.arraycopy(arr1,0,merge,0,arr1.length);
System.arraycopy(arr2,0,merge,arr1.length,arr2.length);

System.out.println(Arrays.toString(merge));

}
}
-----------------------------------------------------------------------------------
------------------------------------------

Que:

Find the Largest Element in an Array:

import java.util.Arrays;
public class Demo
{
public static void main(String[] args)
{
int[] arr = {5,9,3,9,67,98,25};

int largest = arr[0];


for(int num : arr)
{
if(num>largest)
{
largest = num ;
}
}
System.out.println(largest);
}
}

new User Define :

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

int[] arr = new int[size];

System.out.println("Enter the Elements in Array :");


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

int largest = arr[0];

for(int num : arr)


{
if(num>largest)
{
largest = num ;
}
}
System.out.println("Largest element in an Arrays : " + largest);
}
}
-----------------------------------------------------------------------------------
------------------------------------------

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

int[] arr = new int[size];

System.out.println("Enter the Elements of Arrays :");


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

HashSet<Integer>set = new HashSet<>();


for(int num : arr)
{
set.add(num);
}
System.out.println(set);
}
}

-----------------------------------------------------------------------------------
------------------------------------------

Que : Find the Second Largest Number :

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

Que : Reverse the Elemets in an Array

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

}
}
-----------------------------------------------------------------------------------
------------------------------------------

Que : Sorting an Array with Logic :

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

}
}
-----------------------------------------------------------------------------------
------------------------------------------

Que : length of last word

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 :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy