Вы находитесь на странице: 1из 3

ASSIGNMLN1 1

SIVAkUMAkM
C120100218667


Question 1
Search for a name
Write a program to accept an array oI names and a name and check whether the
name is present in the array. Return the count oI occurrence. Use the Iollowing
array as input
'Dave, 'Ann, 'George, 'Sam, 'Ted, 'Gag, 'Saj, 'Agati, 'Mary, 'Sam,
'Ayan, 'Dev, 'Kity, 'Meery, 'Smith, 'Johnson, 'Bill, 'Williams, 'Jones,
'Brown, 'Davis, 'Miller, 'Wilson, 'Moore, 'Taylor, 'Anderson, 'Thomas,
'Jackson}
Program :
lmporL [avalo*
lmporL [avauLll*
publlc class CounLer

sLaLlc SLrlng Arr_names uave Ann Ceorge Sam 1ed Cag Sa[ AgaLl Mary Sam Ayan uev klLy Meery
SmlLh !ohnson 8lll Wllllams !ones 8rown uavls Mlller Wllson Moore 1aylor Anderson 1homas !ackson
publlc sLaLlc vold maln(SLrlng args)Lhrows LxcepLlon

SLrlng1okenlzer s1new SLrlng1okenlzer(Arr_names)
Scanner br new Scanner(SysLemln)
SysLemouLprlnLln(LnLer a name Lo be searched)
SLrlng namebrnexLLlne()
lnL counL0
whlle(s1hasMore1okens())

lf(nameequalslgnoreCase(s1nexL1oken()))

counL++


SysLemouLprlnLln(1PL nAML +name+ PAS ALA8Lu +counL+ 1lMLS)



Question 2
reatest common divisor
Calculate the greatest common divisor oI two positive numbers a and b.
gcd(a,b) is recursively deIined as
gcd(a,b) a iI a b
gcd(a,b) gcd(a-b, b) iI a ~b
gcd(a,b) gcd(a, b-a) iI b ~ a

Program:

import java.io.*;
import java.util.Scanner;
class gcdIinder

public static void main(String args||)

Scanner sr new Scanner(System.in);
System.out.println("Enter The number a");
int asr.nextInt();
System.out.println("Enter The number b");
int bsr.nextInt();
int gcd;
iI(ab)
gcda;
else iI(a~b)
gcdIindgcd(a-b,b);
else
gcdIindgcd(b,b-a);
System.out.println("The greatest common divisor oI numbers " a " and " b "

is " gcd);
}
public static int Iindgcd(int c,int d)

iI (d 0)
return c;
return Iindgcd(d, c d);
}
}

Вам также может понравиться