Professional Documents
Culture Documents
Accord Info Matrix: Programming Questions
Accord Info Matrix: Programming Questions
Accord Info Matrix: Programming Questions
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);
}
}
}
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);
if(chr==ch){
count++;
}
}
System.out.println("No of occurance : "+count);
}
}
3
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
}
}
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];
4
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{
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]+", ");
}
5
}
}
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;
}
}
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.
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];
}
}
}
}
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];
}
7
if(array[x]<smallest){
secondSmallest=smallest;
smallest=array[x];
}
else{
if(secondSmallest>array[x]){
secondSmallest=array[x];
}
}
}
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) {
int a=sc.nextInt();
for(int x=a;x>=1;x--){
if(printSymbol.equals(symb1)){
printSymbol=symb2;
}
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;
9
int index2=0;
for(int x=0;x<10;x++){
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.
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 ;
10
}
}
else{
System.out.print("Enter a valid number!!");
}
}
* * * * *
* * *
* * *
* * * * *
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();
}}}
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
check(array,first,mid-1,key);
}
else if(array[mid]<key){
check(array,mid+1,last,key);
}
}
}
16. Program for display the fibonacci series using recursive function
import java.util.Scanner;
public class Test {
13
int c=a+b;
System.out.println(c);
a=b;
b=c;
limit--;
fibonacci(limit);
}
}
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;
}
}
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;
}
}
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";
15
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());
}
}
}
}
}
System.out.println("reversed: "+rev);
}
}
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+" ";
}
} } }}
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++){
18
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");
}
}
}