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);
}
}
}
2
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);
}
}
}
}
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.
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);
}
}
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);
}
}
import java.util.*;
public class Tet {
public static void main(String[] args) {
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.
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!!");
}
}
* * * * *
* * *
* * *
* * * * *
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
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]);
}
}}
}
else if(array[mid]>key){
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 {
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
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());
}
}
}
}
17
}
System.out.println("reversed: "+rev);
}
}
18
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");
}
}
}