You are on page 1of 11

NAME-Muskan Agarwal

REG. no.-16BEE0143

Question-1 Exercise-2
Given �n� integers, write an algorithm and the subsequent Java code to print all
numbers that are sum-equivalent to the first number. Two numbers �m� and �n� are
said to be sum-equivalent if �m� and �n� have the same number of digits and the sum
of the digits in �m� and �n� are equal. 12381 is sum-equivalent to 10545. Here both
the numbers are five digit numbers. Sum of the digits in 12381 is 1+2+3+8+1=15.
Similarly the sum of the digits in 10545 is 15.Write Java code to check whether two
numbers are sum-equivalent
or not. If none of the numbers are sum-equivalent then print �No sum-equivalent�.

package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

class count_and_sum{
public int count(int a){
int count=0;
while(a>0){
a=a/10;
count++;
}
return count;
}
public int sum(int a){
int sum=0;
while(a>0){
sum=sum+(a%10);
a=a/10;
}
return sum;
}
}

public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Please enter the number of digits you are goingto
input");
int num=input.nextInt();
System.out.println("Please enter the n numbers you wantedto check");
int[] num_array=new int[num];
for(int i=0;i<num;i++){
num_array[i]=input.nextInt();
}
System.out.println(num_array[0]);
count_and_sum cas=new count_and_sum();
int ref_count=cas.count(num_array[0]);
int ref_sum=cas.sum(num_array[0]);
int sum_count=0;
for(int j=1;j<num;j++){
if(cas.count(num_array[j])!=ref_count){
break;
}
else{
if(cas.sum(num_array[j])!=ref_sum){
break;
}
else{
sum_count++;
System.out.println(num_array[j]);
}
}
}

if(sum_count==0){
System.out.println("None of the numbers are sum equivalent");
}

Question-2 Exercise-2
Consider a nxn board game with four types of coins red, green, blue and
yellow.Given the state of the board with coins in all cells, develop an algorithm
and write a Java program to check if the same coins are
placed in �L� shape on the board. The number of cells in the vertical and
horizontal line of �L� shape should be same and should be greater than 1. Red coins
are represented by �r�, blue coins are represented
by �b�, green coins are represented by �g� and yellow coins are represented by �y�.

Question-3 Exercise-2
Given a number �n�, write an algorithm and the subsequent Java program to count the
number of two digit prime numbers in it when adjacent digits are taken. For
example, if the value of �n� is 114 then the two digit numbers that can be formed
by taking adjacent digits are 11 and 14. 11 is prime but 14 is not. Therefore print
1.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

class adjacent_prime{

public int adjacent_count(int a){


int count=0;
while(a>0){
a=a/10;
count++;
}
return count;
}

public void adjacent_prime(int a,int count,int[] array_adjacent){


int[] array_numbers=new int[count];

for(int i=count-1;i>=0;i--){
array_numbers[i]=a%10;
a=a/10;
}

for(int i=0;i < count-1;i++){


array_adjacent[i]=((10*array_numbers[i])+(array_numbers[i+1]));
}
}

public int prime(int a){


int flag=1;
for(int i=a-1;i>1;i--){
if(a%i==0){
flag = 0;
break;
}
}
return flag;
}
}

public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Please enter the number ");
int num=input.nextInt();
adjacent_prime ap=new adjacent_prime();
int count=(ap.adjacent_count(num))-1;
int[] adjacent=new int[count];
ap.adjacent_prime(num, count+1,adjacent);
System.out.println("Adjacent numbers are ");
for(int i=0;i<count;i++){
System.out.println(adjacent[i]);
}
int flag=0;
for(int i=0;i<count;i++){
flag=flag+ap.prime(adjacent[i]);
}
System.out.println("Total Prime Adjacent numbers are ");
System.out.println(flag);
}

Question-4 Exercise 2
Given �n�, the number of rows, design an algorithm and write Java code to draw a
pattern. If n is �5�, the pattern looks as shown below:

package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Please enter the number of rows ");
int num=input.nextInt();

for(int i=0;i<num;i++){
for(int j=0;j<=i;j++){
System.out.print("**");
}
System.out.println("");
}
}

Question-5 Exercise-2
A word is called as a good word if all the letters of the word are distinct. That
is, all
the letters of the word are different from each other letter. Else, the word is
called as
a bad word.
Write an algorithm and the subsequent Java code to check if the given word is good
or bad.: e.g. START, GOOD, BETTER are bad: WRONG is good! Make the comparison
to be case insensitive.

Question-6 Exercise 2
Write an algorithm and the subsequent Java program to check whether the given
matrix is sparse or not. A matrix is said to be a �Sparse� if the number of zero
entries in the matrix, is greater than or equal to the number of non-zero entries.
Otherwise it is �Not sparse�. Check for boundary conditions and print 'Invalid
input'
when not satisfied.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Please enter the number of rows ");
int rows=input.nextInt();
System.out.println("Please enter the number of columns");
int cols=input.nextInt();
if(rows <0 || cols<0){
System.out.println("Invalid input");
}
int[][] matrix=new int[rows][cols];
int zeros=0;

for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
matrix[i][j]=input.nextInt();
if(matrix[i][j]==0){
zeros=zeros+1;
}
}
}

if(zeros>((rows*cols)-zeros)){
System.out.println("Sparse");
}
else{
System.out.println("Not Sparse");
}

Question-7 Exercise-2

Given 'n' points in an X-Y plane , write an algorithm and the subsequent Java code
to
determine the pair of points that are closer. Distance between two points (x1, y1)
and
(x2, y2) is determined using the formula.
When there are multiple points with the same minimum distance print all points.

Question-2 Exercise-3
Read an array of names and display the sorted names to the user

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

public class JavaApplication1 {


public static void main(String[] args) {
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);

}
}
Question-3 Exercise 3
� Reverse every word in a string and display the result to the user.(If Input is
satish teaches Java then output should be hsitas sehcaet avaJ)

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Please enter the String");
String a=input.nextLine();
int flag = 0;
String reverse ="";

for(int i=a.length()-1;i>0;i--){
reverse= reverse + a.charAt(i);

}
System.out.println("The reversed string is:");
System.out.println(reverse);

Question-4 Exercise 3
� Count the number of times every word occurs in a given sentence

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

public class JavaApplication1 {

/**
* @param args the command line arguments
*/

static final int MAX_CHAR = 256;

static void getOccuringChar(String str)


{
int count[] = new int[MAX_CHAR];

int len = str.length();

for (int i = 0; i < len; i++)


count[str.charAt(i)]++;

char ch[] = new char[str.length()];


for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {

if (str.charAt(i) == ch[j])
find++;
}

if (find == 1)
System.out.println("Number of Occurrence of " + str.charAt(i) + "
is:" + count[str.charAt(i)]);
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("wRITE THE INPUT STRING");
String str=sc.nextLine();
getOccuringChar(str);
}
}

Question-5 Exercise-3
� Count the number of vowels that is present in a string
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

/**
*
* @author Muskan Agarwal
*/

import java.util.*;

public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Please enter the String");
String a=input.nextLine();
int flag = 0;

for(int i=0;i<a.length();i++){
char t=a.charAt(i);
if( (t =='a')||(t =='e')||(t== 'i')||(t=='o')||(t=='u')){
flag++;

}
if(flag>0){
System.out.println("There number of vowel in the string are:");
System.out.println(flag);

}
else{
System.out.println("There is no vowel in the string");
}
}

You might also like