Sunteți pe pagina 1din 19

1

Accord Info Matrix

Programming Questions

1. Write a program to bring the following output. User will give an input
range of two number (should be greater than zero). Find the sum of odd
numbers between the two numbers and find the sum of all number
divisible by 6

import java.util.*;
public class Sum{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int oddSum=0;
int sixDiv=0;
int a= sc.nextInt();
if(a<=0){
System.out.print("Entered number should be greater that Zero");
System.exit(0);
}
int b= sc.nextInt();
if(b<=0){
System.out.print("Entered number should be greater than Zero");
System.exit(0); }
else if(b<=a){
System.out.print("Number should be greater than First number");
System.exit(0);
}
else{
for(int x=a;x<=b;x++){
if(x%2!=0){
oddSum+=x;
}
else if(x%6==0){
sixDiv+=x;
}
}
System.out.println("Odd sum: "+oddSum);
System.out.println("six: "+sixDiv);
}
}
}
2

2. Find the maximum consecutive 1s in an array of 1s and 0s

public class Consec{


public static void main(String[] args) {
int array []={1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1};
int maxCount=0;
int count=0;
for(int x=0;x<array.length;x++){
if(array[x]==1){
count++;
}
else{
if(count>maxCount){
maxCount=count;
count=0;
}
}
}
if(count>maxCount){
maxCount=count;
}
System.out.println("Maximimum count: "+maxCount);
}

3. Write a program to find the no. of occurrences of a dynamic alphabetic


character in a given string. Both the word and the character should be
input from the user

import java.util.*;
public class Tet {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String word=sc.nextLine();
System.out.println("Enter a sing character to search: ");
char ch =sc.nextLine().charAt(0);
int count=0;
for(int x=0;x<word.length();x++){
int chr=word.charAt(x);
3

if(chr==ch){
count++;
}
}
System.out.println("No of occurance : "+count);
}
}

4. Two words are called as anagrams if one word can be formed by


shuffling the letters of another word. Eg. 'listen' and 'silent' are anagrams
but 'cab' and 'bag' are not anagrams.
Problem: Given list of English words. write a function to group all
anagrams together

Ex : ["abc", "def", "cab","money","bca","yomen"]


output: ["abc" ,"bca","cab"] ["money","yomen"]

public class Anagram {


public static void main(String[] args) {
String[] list = {"MONEY", "YOMEN", "POT", "TOP", "CAT", "ACT"};
for (int x = 0; x < list.length; x++) {
String anagrams=list[x];
for(int y=x+1;y<list.length;y++){
if(list[x].length()==list[y].length()){
char[] array1 = sort(list[x].toLowerCase().toCharArray());
char[] array2 = sort(list[y].toLowerCase().toCharArray());
boolean isAnagram=true;
for(int z=0;z<array1.length;z++){
if(array1[z]!=array2[z]){
isAnagram=false;
break;
}
}
if(isAnagram){
anagrams=anagrams+","+list[y];
}
}
}
if(anagrams.split(",").length>1){
System.out.println(anagrams);
4

}
}
static char[] sort(char[] array) {
for (int x = 0; x < array.length; x++) {
for (int y = x + 1; y < array.length; y++) {
if (array[x] > array[y]) {
char temp = array[x];
array[x] = array[y];
array[y] = temp;
}
} }
return array;

}
}

5. In array of 100 numbers find the unique and duplicate numbers, store
those in two different arrays.
import java.util.*;
public class Tet {
public static void main(String[] args) {
String duplictes="" ;
String nondup="";
int[] numbers ={1,2,3,5,3,7,9,2,6,4,8};
for(int x=0;x<numbers.length;x++){
int count=0;
for(int y=0;y<numbers.length;y++){
if(numbers[x]==numbers[y] & x!=y){
count++;
break;
}
}
if(count==0){

nondup=nondup+numbers[x]+";";
}
else{
5

if(duplictes.indexOf(Integer.toString(numbers[x]))== -1)
duplictes=duplictes+numbers[x]+";";
}

}
String[] duplicatesArray = duplictes.split(";");
String[] noDuplicateArray=nondup.split(";");

for(int x=0;x<noDuplicateArray.length;x++){
System.out.print(noDuplicateArray[x]+", ");
}
System.out.println();
for(int x=0;x<duplicatesArray.length;x++){
System.out.print(duplicatesArray[x]+", ");
}
}
}

6. Write a program to get a number from the user and split the number and
sum the value of each digit. If resulting number is more than a single digit,
split it again to find the sum. This process should he repeated till the sum
of the digits becomes a single digit.
Ex: 95483 => 9+5+4+3+8 =>29 =>2+9=11 => 1+1 = 2

import java.util.*;
public class DigitsSum{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a value: ");
int n=sc.nextInt();
int sum=0;

while(n>0){
int r = n%10;
sum+=r;
n=n/10;
if(sum>9){
n=sum;
sum=0;
6

}
}
System.out.println(sum);
}
}

7. Write a program to get dynamic list of words from the user and store it to
an array. Select a word from the array by user input a random index let us
call this word as parent. Find another word in the array that has maximum
no of characters similar to the parent word.

public class MyProject {


public static void main(String[] args) {
String [] array = new String[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 elements ");
for(int x=0;x<array.length;x++){
System.out.print(x+" >>");
array[x]=sc.nextLine();
}
System.out.println("Select a word from the list using index");
int index=sc.nextInt();
String parentWord=array[index];
int matched=0;
String childWord="";
for(int x=0;x<array.length;x++){
if(index!=x || array[x].equals(parentWord)==false){
for(int y=0;y<parentWord.length();y++){
int count=0;
for(int z=0;z<array[x].length();z++){

if(parentWord.charAt(y)==array[x].charAt(z)){
count++;
}
}
if(count>matched){
matched=count;
childWord=array[x];

}
7

}
}
}
System.out.println("Word with maximum similar character :
"+childWord);
}
}

8. Write a program to create an array of random numbers find the second


biggest and second smallest number.
public class Tet {
public static void main(String[] args) {
int [] array={950,8,27,1,8,281,930,2,270,800};
int biggest=array[0];
int secondBiggest= 0;

int secondSmallest=0;
int smallest= array[0];
int x;
for( x=1;x<array.length;x++){
if(array[x]>biggest){
secondBiggest=biggest;
biggest=array[x];
}
else{
if(secondBiggest<array[x]){
secondBiggest=array[x];
}
}

if(array[x]<smallest){
secondSmallest=smallest;
smallest=array[x];
}
else{
if(secondSmallest>array[x]){
secondSmallest=array[x];
}

}
8

System.out.println("Biggest "+biggest);
System.out.println("Second Biggest "+secondBiggest);

System.out.println("Smallest "+smallest);
System.out.println("Second Smallest "+secondSmallest);
}
}

9. Program to print the following pattern. Two alternating symbols are


used and need to get the symbols from the user. Maximum size is another
input, where the first line in the pattern should contain that number if
characters. and the next row is one character less the previous row and
with alternating symbols. print till the number becomes zero
Pattern:
######
$$$$$
####
$$$
##
$

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

Scanner sc = new Scanner(System.in);


System.out.println("Enter symbol 1");
String symb1=sc.nextLine();

System.out.println("Enter symbol 2");


String symb2=sc.nextLine();
String printSymbol=symb1;
System.out.println("Enter the rows count");
int a=sc.nextInt();
for(int x=a;x>=1;x--){
if(printSymbol.equals(symb1)){
printSymbol=symb2;
}
9

else{
printSymbol=symb1;
}
for(int y=1;y<=x;y++){
System.out.print(printSymbol+" ");
}
System.out.println();
}
}
}

10. Write a program to get an input number 'x' from the user. and to get a
set of 10 numbers . The numbers in an array less than 'x' should be stored
in to an array and number that are greater than the x should be stored into
another array
public class Test2 {
public static void main(String[] args) {
System.out.println("Enter a number: ");
int [] greater=null;
int [] lesser=null;
Scanner sc = new Scanner(System.in);
int rf=sc.nextInt();
int lesscount=0;
int greatercount=0;
int[] numbers= new int[10];
System.out.println("Enter the 10 numbers");
for(int x=0;x<10;x++){
numbers[x]=sc.nextInt();
if(numbers[x]<rf){
lesscount++;
}
if(numbers[x]>rf){
greatercount++;
}
}
if(lesscount>0){
lesser= new int[lesscount];
}
if(greatercount>0){
greater=new int[greatercount];
}
int index1=0;
int index2=0;
for(int x=0;x<10;x++){
10

if(numbers[x]>rf){
greater[index1]=numbers[x];
index1++;
}
else if(numbers[x]<rf){
lesser[index2]=numbers[x];
index2++;
}
}

System.out.println("Bigger numbers");
for(int x=0;x<greater.length;x++){
System.out.println(greater[x]);
}
System.out.println("Smaller numbers");

for(int x=0;x<lesser.length;x++){
System.out.println(lesser[x]);
}
}}

11. Write a program to get a random number from the user between 1001
and 8765432. Check if the number is divisible by any one or any
combination of digits within the number.

public class Test {


public static void main(String[] ars){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number between 1001 and
8765432");
int x=sc.nextInt();
if(x>=1001 && x<=8765432){
String str = Integer.toString(x);
for(int index=0;index<str.length();index++){

for(int index2=0;index2<str.length();index2++){
String
number=str.charAt(index)+""+str.charAt(index2);
System.out.println(number);
int num=Integer.parseInt(number);
if(x%num==0){
System.out.println("Divisible by:
"+num);
break ;
}
11

}
}
else{
System.out.print("Enter a valid number!!");

}
}

12. Print the following pattern

* * * * *
* * *
* * *
* * * * *
public class Test2 {
public static void main(String[] args) {
for(int x=1;x<=4;x++){
for(int y=1;y<=4;y++){
if(x==y){
System.out.print("*");
}

if(x==1||x==4){
System.out.print("*");
}
else if(y==1||y==4){
System.out.print("*");
}
else{
System.out.print(" ");
}
} System.out.println();
}}}
12

13. Write a program to implement insertion sort.

import java.util.Scanner;
public class Test {
public static void main(String[] ars){
int[] arr={1,8,5,3,2,9,7};
int n = arr.length;
for (int i = 1; i < n; i++)
{
int key = arr[i];
int j = i-1;
while ( (j > -1) && ( arr [j] > key ) )
{
arr [j+1] = arr [j];
j--;
}
arr[j+1] = key;

}
for(int x=0;x<arr.length;x++){
System.out.println(arr[x]);
}
}}

14. Write a program to implement Binary search in a sorted array of values


using Recursive

public class BinarySearch {


public static void main(String[] args) {
int[] array = {2,6,8,19,26,33,34,55,66};
new BinarySearch().check(array,0,array.length,8);
}
void check(int[] array,int first,int last,int key){
if(first>last){
System.out.println("Cannot find");
return;
}
int mid = (first+last)/2;
if(array[mid]==key){
System.out.println("Value found at index: "+mid);
13

}
else if(array[mid]>key){
check(array,first,mid-1,key);
}
else if(array[mid]<key){
check(array,mid+1,last,key);
}

}
}

15. Program to find factorial of a number using recursive function


import java.util.Scanner;
public class Test {
public static void main(String[] ars){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a value ");
int a = sc.nextInt();
int fac=Number.factorial(a);
System.out.println(fac);
}
}
class Number{
static int factorial(int a){
if(a==1){
return 1;
}
return a*factorial(a-1);
}
}
14

16. Program for display the fibonacci series using recursive function

import java.util.Scanner;
public class Test {
public static void main(String[] ars){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a value ");
int a = sc.nextInt();
Number.fibonacci(a);
}
}
class Number{
static int a=0;
static int b=1;
static{
System.out.println(a);
System.out.println(b);
}
static void fibonacci(int limit){
if(limit<3){
return ;
}

int c=a+b;
System.out.println(c);
a=b;
b=c;
limit--;
fibonacci(limit);

}
}
15

17. Reverse a number using recursive function


public class Reverse {
public static void main(String[] args) {
int number=256;
System.out.println(Number.reverse(number));
}
}
class Number{
static int ret=0;
static int reverse(int number){
if(number<=0){
return 0;
}
else{
ret=ret*10+number%10;
reverse(number/10);
}
return ret;
}
}

18. Program to reverse a string without using for loop


public class ReverseSrring {
public static void main(String[] args) {
String str="Welcome";
String res=Task.reverseString(str,str.length());
System.out.println(res);
}
}

class Task{
static String result="";
static String reverseString(String str,int len){
if(len==0){

return "";
}
else{
result=result+str.charAt(len-1);
reverseString(str, len-1);
}

return result;
}
}
16

19. Write a program to create a string using the first characters of the first
letters of each word in the give string.
Ex:
Input ⇒This is a word Output ⇒TIAW
public class StringTask {
public static void main(String[] args) {
String str = "This is a word";
int index = 0;
String result = "";
result += str.charAt(index);
while (true) {
index++;
if (index == str.length() - 1) {
break;
} else {
if (str.charAt(index) == ' ') {
result += str.charAt(index + 1);
}
} }
System.out.println(result.toUpperCase());
}
}

20. Program to compute the series 1/1! + 1/2! +1/3!+1/4! …… 1/n!


public class SumSeries {
public static void main(String[] args) {
int n=2;
float result=0;
for(int x=1;x<=n;x++){
int fact=1;
for(int y=1;y<=x;y++){
fact=fact*y;
}
result= result+ (1.0f/fact);
}
System.out.println("Result "+result);

}
}
17

21. Program to convert the Binary value to Decimal value


public class BinaryToDecimal{

public static void main(String[] args) {


int binaryValue = 1010;
int position = 0;
int sum = 0;
while (binaryValue > 0) {
int r = binaryValue % 10;
sum = sum + r*twoPow(position);
binary=binary/10;
position++;
}
System.out.print(sum);
}

static int twoPow(int n) {


int ret = 1;
if (n==0) {
ret = 1;
} else {
for (int x = 1; x <= n; x++) {
ret=ret*2;
}
}
return ret;
}
}

22. Program to reverse a given number


class ReverseNumber{
public static void main(String[] args){
int number=256;
int rev=0;
while(number>0){
int r=number%10;
rev=(rev*10)+r;
number=number/10;

}
System.out.println("reversed: "+rev);
}
}
18

23. Write A java program to reduce a String array by removing the


duplicate String
public class RemoveDuplictes {
public static void main(String[] args) {
String[] words
={"Android","IOS","Blackberry","Windows","Windows","Windows","Window
s"};
String result="";
for(int x=0;x<words.length;x++){
for(int y=x+1;y<words.length;y++){
if(words[x].compareTo(words[y])>0){
String temp = words[x];
words[x]=words[y];
words[y]=temp;
}
}
}
String word=words[0];
for(int y=1;y<words.length;y++){
if(word.equals(words[y])==false){
result=result+word+" ";
word=words[y];
}
else{
if(y==words.length-1){
result=result+word+" ";
}
} } }}
19

24. Given 2 Arrays int a[]={1,2,3,4,5,6,7,8,9} int b[]={2,3,4} find that b is sub
array of a or not, if all the elements of b are available in a then b is a sub
array of a
public class Compare {
public static void main(String[] args){
int[] array1 ={1,2,3,4,5,6,7,8,9};
int[] array2={7,8,91};
int flag=0;
for(int x=0;x<array2.length;x++){
flag=0;
for(int y=0;y<array1.length;y++){
if(array2[x]==array1[y]){
flag=1;
break;
}
}
if(flag==0){
break;
}
}
if(flag==0){
System.out.println("not a sub array");
}
else
{
System.out.println("sub array");

}
}
}

S-ar putea să vă placă și