Академический Документы
Профессиональный Документы
Культура Документы
SET 1
ANIK SARKAR
ALGORITHM:
main function:
Step 1: Start
Step 3: Accept a number from the user to check for prime in ‘n’.
Step 4: Call function compute() and send the number ‘n’ as parameter.
Step 5: Stop
compute function:
Step 1: Start
c++;
End If
End for i
else
End If
Step 7: Stop
1
import java.util.*;
class Program1
for(int i=1;i<=n;i++)
c++;//counter
return c;
System.out.println("Enter a number");
int n=sc.nextInt();
System.out.println("It is Prime");
else if(ob.compute(n)>2)//if "c" is more than 2 then the number is not prime
2
OUTPUT:
VARIABLE DESCRIPTION:
main function:
Compute function:
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
METHOD LISTING:
Method Desription
nextInt Extract and converts data from the input stream into integer.
println To print on the monitor.
main Accepts a number from the user and calls compute method.
ALGORITHM:
main function:
Step 1: Start
Step 2: Create variables l, i, l1, j as integer; create s, st1, st2, word, sen as String variables and clear
Else
Step 11: Extract each character form the word and store in ch1.
End If
End For j
Step 13: Extract the first half of the word, before the vowel, by using “st1=word.substring(0,j);”
Step 14: Extract the remaining half of the word using “st2=word.substring(j);”
Step 15: Form the piglatin sentence in variable sen using “sen=sen+st2+st1+"ay"+" ";”
End If
End For i
4
import java.io.*;
class Program2
{
int l,i,l1,j;
String s,st1="",st2="",word="",sen="";
char ch,ch1;
System.out.println("Enter a sentence");
s=in.readLine();
s=s+" ";
l=s.length();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch!=' ')
{
}
else
{
l1=word.length();
for(j=0;j<l1;j++)
{
ch1=word.charAt(j);
if(ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||ch1=='u'||ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
{
break;
}
}
5
st1=word.substring(0,j); //forming the piglatin word
st2=word.substring(j);
}
}
}
OUTPUT:
VARIABLE DESCRIPTION:
main function:
st2 String Substring to extract second half after the first vowel
6
CLASS LISTING:
Class Desription
InputStreamReade Brings the input stream in your program and converts data in bytes to characters.
r
BufferedReader Extracts data from input stream and makes reading of data more efficient.
System To access the default input and output systems i.e. keyboard and the monitor.
METHOD LISTING:
Method Desription
Main Accepts a sentence from user and convert it into piglatin form
7
PROGARM 3:
WAP to create a function armstrong() to print all Armstrong Numbers from a to b, both a and b inclusive.
ALGORITHM:
main function:
Step 1: Start
Step 7: Call function armstrong() and send the number ‘n’ as parameter.
Step 8: Stop
Armstrong function:
Step 1: Start
Step 4: Inside for loop designating the value of ‘s’ as 0 and ‘num’ as ‘i’.
Step 5: Running a while loop to extract digits and compute the sum of cube of the digits in ‘s’.
If sum equal to the number then printing that it’s a Armstrong number.
Step 7: Stop
import java.util.*;
class Program3
8
void armstrong(int a,int b)//function to check armstrong number or not
s=0;
num=i;
x=num%10;
s=s+(x*x*x);
num=num/10;
System.out.println(i);
int a=sc.nextInt();
int b=sc.nextInt();
9
OUTPUT:
VARIABLE DESCRIPTION:
main function:
Armstrong function:
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
10
Program Checks and displays if a number is armstrong or not.
3
METHOD LISTING:
Method Desription
nextInt Extract and converts data from the input stream into integer.
println To print on the monitor.
main Accepts two numbers from the user and calls armstrong method.
11
PROGARM 4:
WAP to check if a word is unique word or not. A word where none of the characters are repeated is a unique
word. Example of unique words are mango, pen, cream. A few non-unique words are people, apple,
volcano.
ALGORITHM:
main function:
Step 1: Start
Step 6: If the letters are repeated then a counter variable ‘c’ is incremented.
Step 7: After checking the value of ‘c’ must be equal to the value of ‘l’ if the letters are not repeated.
Step 8: If the value of ‘c’ is equal to ‘0’ then printing that it is a unique word else if the value of ‘c’ is more than ‘0’
then printing that it is not a unique word.
Step 8: Stop.
import java.util.*;
class Program4
int c=0;
System.out.println("Enter a word");
String str=sc.nextLine();
12
int l=str.length();//determining length of word
for(int i=0;i<l;i++)
for(int j=i+1;j<l;j++)
if(str.charAt(i)==str.charAt(j))//comparing letters
c++;//counter
System.out.println("Unique word");
OUTPUT:
13
VARIABLE DESCRIPTION:
main function:
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
METHOD LISTING:
Method Desription
nextLin Extract and converts data from the input stream into String.
e
main Accepts a word from user and checks if it is a unique word or not.
14
PROGARM 5:
WAP to create an array of size ‘n’. From the array (where values are repeated), print the frequency of each
ALGORITHM:
main function:
Step 1: Start
Step 5: After checking an element making the element at that position zero to prevent repetition of elements.
Step 6: If the element in that position is not zero then printing the frequency.
Step 7: Stop.
import java.util.*;
class Program5
int c=0;
int n=sc.nextInt();
15
for(int i=0;i<n;i++)
for(int i=0;i<n;i++)
c=1;
for(int j=i+1;j<n;j++)
if(arr[i]==arr[j])//comparing elements
c++;//incrementing counter
if(arr[i]!=0)
OUTPUT:
16
VARIABLE DESCRIPTION:
main function:
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
METHOD LISTING:
Metho Desription
d
nextInt Extract and converts data from the input stream into Integer.
main Accepts an array from user and finds frequency of each character.
17
PROGARM 6:
Find the nth largest term from an array. Value in the array may be repeated more than once. If n is 1 print largest
from array. If n is 2 print second largest. If n is 3 print third largest. Also keep a check if nth largest is possible or
not.
ALGORITHM:
main function:
Step 1: Start
Step 4: Creating and integer variable ‘a’ nth largest term to be found.
Step 6: Then if the nth largest term to be found is more that array size then it prints that element not present else
if it is within the array size then the nth largest term is printed.
Step 7: Stop.
import java.util.*;
class Program6
int temp=0;
int n=sc.nextInt();
for(int i=0;i<n-1;i++)
for(int j=0;j<n-i-1;j++)
if(arr[j]<arr[j+1])//bubble sort
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
int a=sc.nextInt()-1;
if(a<=n)
else if(a>n)
OUTPUT:
19
VARIABLE DESCRIPTION:
main function:
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
Program Checks and displays the nth largest term as per the users wish.
6
METHOD LISTING:
Metho Desription
d
nextInt Extract and converts data from the input stream into Integer.
println To print on the monitor.
main Accepts a position from user ‘n’ and prints the nth largest term from the array.
20
PROGARM 7:
A bank intends to design a program to display the denomination of an input amount, up to 5 digits. The
available denominations with the bank are of rupees 2000 , 500 , 100 , 50 , 20 , 10 , 5 , 2 , and 1.
Design a program to accept the amount from the user and display the break-up in descending order of
denomination. (i.e. preference should be given to the highest denomination available) along with the total
ALGORITHM:
main function:
Step 1: Start
Step 4: Taking the input from user for the required amount and storing it is variable ‘n’.
Step 5: Finding the number of notes by dividing the amount by the notes one by one and then reducing the
amount accordingly.
Step 7: Stop.
import java.util.*;
class Program7
21
int arr[]={2000,500,100,50,20,10,5,2,1};
int n=sc.nextInt();
System.out.println("DENOMINATIONS");
for(int i=0;i<9;i++)
c=n/arr[i];
z=c*arr[i];
OUTPUT:
22
VARIABLE DESCRIPTION:
main function:
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
Program Checks and displays the number of notes required to pay a certain amount.
7
METHOD LISTING:
Metho Desription
d
nextInt Extract and converts data from the input stream into Integer.
23
println To print on the monitor.
main Accepts an amount from user and finds out the number of notes needed to pay that amount.
PROGARM 8:
Write a program to implement binary search in an array of characters.
ALGORITHM:
main function:
Step 1: Start
Step 4: Then creating variables ‘lb’ equal to 0 and ‘ub’ equal to the size of the array.
Step 5: Creating a variable mid which is equal to the sum of ‘lb’ and ’ub’ divided by 2.
Step 7: Then searching the element using binary search technique (if element is more than the element at position
‘mid’ then decreasing the value of ‘ub’ by one else if element is less than the element at position ‘mid’ then
increasing the value of ‘lb’ by one else if the element is present at position ‘mid’ then printing the element is
present in position ‘mid+1’, at the end again calculating the value of ‘mid’ equal to the sum of ‘lb’ and ’ub’ divided
by 2.
Step 8: Stop.
import java.util.*;
class Program8
24
System.out.println("Enter size of array");
int n=sc.nextInt();
for(int i=0;i<n;i++)
char ele=sc.next().charAt(0);
int lb=0;
int ub=n;
int mid=(lb+ub)/2;
while(ub>lb)//binary search
if(arr[mid]>(int)ele)
ub=ub-1;
else if(arr[mid]<(int)ele)
lb=lb+1;
else
break;
mid=(lb+ub)/2;
OUTPUT:
25
VARIABLE DESCRIPTION:
main function:
mid Int Stores the mid value which is equal to the sum of ‘lb’ and ‘ub’ divided by 2.
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
METHOD LISTING:
26
Method Desription
nextInt Extract and converts data from the input stream into Integer.
next().charAt(0 Extract and converts data from the input stream into Character.
)
main Accepts a character from user and prints its position in the array.
PROGARM 9:
Write a program to perform selection sort on an integer array passed as a parameter in a function sort(int).
ALGORITHM:
main function:
Step 1: Start
Step 4: Then calling function ‘sort’ to sort the array using selection sort technique.
Step 6: Stop.
Sort function:
Step 1: Start
Step 2: Running 2 for loops to execute selection sort in the required array. (This is done by finding the lowest
element in the given array and putting it in the first position and so on.)
Step 3: Stop.
import java.util.*;
class Program9
{
27
static int n=0;
if (arr[j]<arr[min])
min=j;
int temp=arr[min];// Swap the found minimum element with the first element
arr[min]=arr[i];
arr[i]=temp;
n=sc.nextInt();
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
ob.sort(arr);
System.out.println("Sorted array");
for(int i=0;i<n;i++)
28
{
System.out.print(arr[i]+" ");
OUTPUT:
VARIABLE DESCRIPTION:
main function:
Sort function
min int Stores the index of the minimum element in the array.
29
temp Int Used for swapping positions of elements in the array.
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
METHOD LISTING:
Metho Desription
d
nextInt Extract and converts data from the input stream into Integer.
println To print on the monitor.
main Accepts an array and calls function ‘sort’ to execute selection sort on the array.
30
PROGARM 10:
Write a program to accept numbers in an array of size ‘n’ and replace each prime number with the
ALGORITHM:
main function:
Step 1: Start
Step 4: If the number is prime then sending it to the ‘nextPrime’ function and replacing its value with the next
prime number after that.
Step 5: If the number at index ‘I’ is not prime then keeping it as it is.
Step 6: Then printing the new array after replacing the prime numbers with the next prime numbers.
Step 7: Stop.
nextPrime function:
Step 1: Start
Step 2: Running 2 for loops to find out the next prime number by counting the number of factors.
Step 3: If the number of factors is two then breaking out of the loops and returning the number with 2 factors.
Step 3: Stop.
31
import java.util.*;
class Program10
int copy=0;
copy=i;
int c=0;
for(int j=1;j<=i;j++)
if(i%j==0)
c++;//counter
return copy;
int b=0;
32
System.out.println("Enter the elements");
for(int i=0;i<n;i++)
b=0;
arr[i]=sc.nextInt();
if(arr[i]%j==0)
b++;
if(b==2)//if the number is prime then replacing it with the next prime number
int p=ob.nextPrime(arr[i]);
arr[i]=p;
for(int i=0;i<n;i++)
OUTPUT:
33
VARIABLE DESCRIPTION:
main function:
nextPrime function
34
j Int Loop variable.
CLASS LISTING:
Class Desription
Scanner Brings the input stream in your program so that user can give input to the program.
System To access the default input and output systems i.e. keyboard and the monitor.
Program1 Replaces the prime numbers in an array with the immediate next prime number.
0
METHOD LISTING:
Method Desription
nextInt Extract and converts data from the input stream into Integer.
main Accepts an array and calls function ‘nextPrime’ to find the next prime number after a certain prime
number.
nextPrim Finds the immediate next prime number after a certain prime number.
e
35