Question 1 Part 1
Write a program to accept an array of names and a name and check whether the
name is present in the array. Return the count of occurrence. Use the following
array as input
{Dave, Ann, George, Sam, Ted, Gag, Saj, Agati, Mary, Sam,
Jackson}
Code:
import java.io.*;
class program1
String[] names={Dave, Ann, George, Sam, Ted, Gag, Saj, Agati, Mary, Sam,
Jackson};
InputStreamReaderInp=new InputStreamReader(System.in);
String name=Br.readLine();
int count=0;
for(inti=0;i<names.length;i++)
if(names[i].equals(name))
count++;
if(count!=0)
else
Output :
Sam
Answer: The code below takes an array of 10 integer values as an argument,prints its values,increments it by 10 and again prints the incremented value
int[] numArray = new int[10]; // allocating memory to 10 integer elements in an array named 'numArray'
public static void incrementElements (int[] integerArray) //defining the 'incrementElements()' method with one integer type array 'integerArray' as the parameter
intarraylen = integerArray.length; // initializing an integer variable 'arraylen' with the length, i.e. the no of elements in array
for (inti = 0; i<arraylen; i ++) //definition of 'for' loop to print the current values of array
System.out.println(integerArray[i]);
for (inti = 0; i<arraylen; i ++) //definition of 'for' loop to increment the current values of array {
for (inti=0; i<arraylen; i ++) //definition of 'for' loop to print the updated values of array
System.out.println(integerArray[i]);
gcd(a,b) = a if a =b
Solution :
import java.io.*; class findgcd { intgcd(inta,int b) { if(a==b) return a; else if(a>b) return(gcd(a-b,b)); else return(gcd(a,b-a));
} }
class gcdtest1 { public static void main(String arr[]) throws IOException { BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a::"); int a=Integer.parseInt(br.readLine());
} }
Enter a::
36
Enter b::
24
int[] a; //declaring a new array 'a' of integer type to store the numbers
//defining a new parameterised method to initialize the array with 'max' as size of the array {
a = new int[max];
public void insert(int value) //defining the insert method to insert values in the array {
int out, in; // declaring two integer variables 'out' & 'in'
if( a[in] > a[in+1] ) //conditional statement to compare the adjacent values
swap(in, in+1); //swaping the two values by calling the 'swap()' function
public void swap(int one, int two) //defining 'swap' function to perform swapping of elements
a[one] = a[two];
a[two] = temp;