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

ISC COMPUTERPROJECT

SET 1

ANIK SARKAR

THE HERITAGE SCHOOL


PROGRAM 1:
WAP to create a function compute() to check if a number is prime or not.

ALGORITHM:

Program Name: Program1

main function:

Step 1: Start

Step 2: Create variable ‘n’ as integer.

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

Step 2: Parameter x receives a number from main().

Step 3: Create i, c=0 as integer.

Step 4: For i = 1 to n, step 1

Step 5: If n%i==0, then

c++;

End If

End for i

Step 6: If c==2, then

Print the number is prime.

else

Print the number is not prime.

End If

Step 7: Stop

1
import java.util.*;

class Program1

int c=0; int n=0;

int compute(int n)//function to check prime

for(int i=1;i<=n;i++)

if(n%i==0)//checking number of factors

c++;//counter

return c;

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

Program1 ob=new Program1();

if(ob.compute(n)==2)//if "c" is equal to 2 then the number is prime

System.out.println("It is Prime");

else if(ob.compute(n)>2)//if "c" is more than 2 then the number is not prime

System.out.println("It is not Prime");

2
OUTPUT:

VARIABLE DESCRIPTION: 
main function:

Variable Datatype Desription


s

n int Stores a number from the user.

Compute function:

Variables Datatyp Desription


e

i int Loop Variable.


c int Counts the number of factors in a number.

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 Check if a number is prime or not.


1

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.

comput Checks if a number is prime or not.


e
3
PROGRAM 2:
WAP to accept a sentence and convert each word into piglatin form.

ALGORITHM:

Program name: Program2

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

st1, st2, word, sen to empty; create ch,ch1 as character variables.            

Step 3: Accept a word from the user and store it in variable s.

Step 4: Add a blank space at the end of word, s=s+” “

Step 5: Find the length of word in l.

Step 6: For i = 0 to l-1, step 1

Step 7: Extract character from the sentence at specified position.

Step 8: If the extracted character is not a space, then

  Form the word in variable ‘word’.

Else

Step 9: Find the length of new word in l1.

Step 10: For j = 0 to l1-1, step 1

Step 11: Extract each character form the word and store in ch1.

Step 12: IF ch1 is a vowel, then

    Break out of loop j.

    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

Step 16: Display the piglatin sentence.

Step 17: Stop

4
import java.io.*;

class Program2

    public static void main()throws IOException

    {

        InputStreamReader read=new InputStreamReader(System.in);

        BufferedReader in=new BufferedReader(read);

        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!=' ')

            {

                word=word+ch; // Extracting words from the sentence

            }

            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);

                sen=sen+st2+st1+"ay"+" "; // Joining the piglatin words to form a sentence.

                st1=st2=word=""; //Clearing variables so that the next word can be formed.

            }

        }

        System.out.println("\n The new sentence is : "+sen);

    }

OUTPUT:

VARIABLE DESCRIPTION: 
main function:

Variable Datatype Desription


s

i Int Loop variable


j Int Loop variable

l Int Stores length of the sentence


l1 Int Stores length of each word from the sentence 

s String Accepts a sentence from the user


st1 String Substring to extract first half before first vowel

st2 String Substring to extract second half after the first vowel

word String Stores each word from the sentence


sen String Stores the new piglatin sentence

ch Char Characters from the original sentence


ch1 Char Characters from each word

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.

Program2 Converts a sentence into piglatin form.

METHOD LISTING:
Method Desription

Integer.parseIn Converts data from String to integer.


t

readLine Extracts one line of text from the input stream.


Println To print on the monitor.

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:

Program Name: Program3

main function:

Step 1: Start

Step 2: Create variable ‘a’ as integer.

Step 3: Accept a number as lower limit in ‘a’.

Step 4: Create variable ‘b’ as integer.

Step 5: Accept a number as upper limit in ‘b’.

Step 6: Create an object ‘ob’

Step 7: Call function armstrong() and send the number ‘n’ as parameter.

Step 8: Stop

Armstrong function:

Step 1: Start

Step 2: Create variable ‘num’, ‘x’, ‘s’ as integer

Step 3: Running a for loop from lower limit to upper limit.

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’.

Step 6: Checking if the sum is equal to the number(‘i’).

If sum equal to the number then printing that it’s a Armstrong number.

Step 7: Stop

import java.util.*;

class Program3

int a=0; int b=0;

8
void armstrong(int a,int b)//function to check armstrong number or not

int num=0; int s=0; int x=0;

for(int i=a;i<=b;i++)//loop with lower and upper limit

s=0;

num=i;

while(num>0)//loop to extract digits and add them after cubing

x=num%10;

s=s+(x*x*x);

num=num/10;

if(s==i)//checking if the number is equal to the sum of cube of digits

System.out.println(i);

public static void main()//main method

Scanner sc=new Scanner(System.in);

System.out.println("Enter lower limit");

int a=sc.nextInt();

System.out.println("Enter upper limit");

int b=sc.nextInt();

Program3 ob=new Program3();

ob.armstrong(a,b);//calling function armstrong

9
OUTPUT:

VARIABLE DESCRIPTION: 
main function:

Variable Datatyp Desription


s e

a Int Stores a number from the user (lower limit).

b Int Stores a number from the user (upper limit).

Armstrong function:

Variables Datatyp Desription


e
i Int Loop Variable.

x Int Stores the extracted digits


s Int Stores the sum of cube of digits.

num Int Stores the number to be checked

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.

armstron Checks and displays if a number is Armstrong or not.


g

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:

Program Name: Program4

main function:

Step 1: Start

Step 2: Create variable ‘c’ as integer.

Step 3: Accept a sentence and store it in ‘str’.

Step 4: Create variable ‘l’ as integer to store length of the String.

Step 5: Run 2 for loops to check repetition of letters in the String.

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

public static void main()//main method

int c=0;

Scanner sc=new Scanner(System.in);

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

if(c>0)//if 'c' is more than zero then it is not a unique word

System.out.println("Not unique word");

else if(c==0)//if 'c' is equal to zero then it is a unique word

System.out.println("Unique word");

OUTPUT:

13
VARIABLE DESCRIPTION: 
main function:

Variable Datatype Desription


s

c Int Counter variable.

str String Stores a word given by user.

l Int Stores the length of the string ‘str’.

i Int Loop variable.

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.

Program Checks and displays if a word is unique or not.


4

METHOD LISTING:
Method Desription

nextLin Extract and converts data from the input stream into String.
e

println To print on the monitor.

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

number present in the array.

ALGORITHM:

Program Name: Program5

main function:

Step 1: Start

Step 2: Taking size of array and taking the input of elements.

Step 3: Running 2 for loops to check the repetition of elements.

Step 4: Keeping a counter variable to calculate the frequency of elements.

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

public static void main()//main method

int c=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter size of array");

int n=sc.nextInt();

int arr[]=new int[n];

System.out.println("Enter the elements of array");

15
for(int i=0;i<n;i++)

arr[i]=sc.nextInt();//taking input of elements of array

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

arr[j]=0;//ensuring that an element is not repeated

if(arr[i]!=0)

System.out.println(arr[i]+" is present "+(c)+" time(s)");

OUTPUT:

16
VARIABLE DESCRIPTION: 
main function:

Variable Datatype Desription


s
c Int Counter variable.

n Int Stores size of array.

arr[] Int Stores array elements.

i Int Loop variable.

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.

Program Checks and displays the frequency of each element in an array.


5

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 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:

Program Name: Program6

main function:

Step 1: Start

Step 2: Taking size of array and taking the input of elements.

Step 3: Running 2 for loops for executing bubble sort.

Step 4: Creating and integer variable ‘a’ nth largest term to be found.

Step 5: Parsing the variable ‘a’-1 as the position is a-1

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

public static void main()//main method

int temp=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter size of array");

int n=sc.nextInt();

int arr[]=new int[n];

System.out.println("Enter the elements of array");


18
for(int i=0;i<n;i++)

arr[i]=sc.nextInt();//taking input of elements of array

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;

System.out.println("Enter the nth largest term to be found");

int a=sc.nextInt()-1;

if(a<=n)

System.out.println("The term is = "+arr[a]);//printing position

else if(a>n)

System.out.println("There only "+n+" elements present");//default case

OUTPUT:

19
VARIABLE DESCRIPTION: 
main function:

Variable Datatype Desription


s
temp Int Variable used for sorting array elements.

n Int Stores size of array.

arr[] Int Stores array elements.

a Int User inputs the nth largest term to be found.

i Int Loop variable.

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.

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

number of notes. [Note: Only the denomination used, should be displayed].

ALGORITHM:

Program Name: Program7

main function:

Step 1: Start

Step 2: Taking an array with the mentioned amount of notes.

Step 3: Then adding the number of notes one by one.

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 6: Then printing the number of notes required.

Step 7: Stop.

import java.util.*;

class Program7

public static void main()//main method

int c=0;int s=0;int z=0;

Scanner sc=new Scanner(System.in);

21
int arr[]={2000,500,100,50,20,10,5,2,1};

System.out.println("Enter the amount");

int n=sc.nextInt();

System.out.println("DENOMINATIONS");

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

c=n/arr[i];

s=s+c;//finding number of notes

z=c*arr[i];

n=n-z;//reducing the amount accordingly

System.out.println(arr[i]+" \t x "+c+" \t = "+z);

System.out.println("The number of notes = "+s);

OUTPUT:

22
VARIABLE DESCRIPTION: 
main function:

Variable Datatype Desription


s
c Int Variable used to count the number of notes per note.

s int Stores total number of notes required.

z Int Stores the amount to be subtracted after each denomination.

arr[] int Stores the array elements.

n Int Stores the amount given by the user.

i 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.

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:

Program Name: Program8

main function:

Step 1: Start

Step 2: Taking input of the number of elements in the array.

Step 3: Then taking input of the elements of the array.

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 6: Then taking input of the element to be searched.

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

public static void main()//main method

Scanner sc=new Scanner(System.in);

24
System.out.println("Enter size of array");

int n=sc.nextInt();

char arr[]=new char[n];

System.out.println("Enter the elements of array");

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

arr[i]=sc.next().charAt(0);//taking input of elements of array

System.out.println("Enter the element to be found");

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

System.out.println("The element is present at position " +(mid+1));

break;

mid=(lb+ub)/2;

OUTPUT:
25
VARIABLE DESCRIPTION: 
main function:

Variable Datatype Desription


s

n Int Stores number of elements in the array.

lb int Stores the lower bound which is 0.

ub Int Stores the upper bound which is equal to ‘n’.

arr[] char Stores the array elements.

mid Int Stores the mid value which is equal to the sum of ‘lb’ and ‘ub’ divided by 2.

ele char Stores the element to be searched.

i 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.

Program Checks and displays the position of element present in an array.


8

METHOD LISTING:

26
Method Desription

nextInt Extract and converts data from the input stream into Integer.

println To print on the monitor.

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:

Program Name: Program9

main function:

Step 1: Start

Step 2: Creating an object ‘ob’ to call ‘sort’ function.

Step 3: Then taking input of the elements of the array.

Step 4: Then calling function ‘sort’ to sort the array using selection sort technique.

Step 5: Then printing the sorted array.

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;

static void sort(int arr[])

for (int i=0;i<n-1;i++)// One by one move boundary of unsorted subarray

int min=i; // Find the minimum element in unsorted array

for (int j=i+1;j<n;j++)

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;

public static void main()

Program9 ob=new Program9();

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of elements in the array");

n=sc.nextInt();

int arr[]=new int[n];

System.out.println("Enter elements of the array");

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:

Variable Datatype Desription


s

n Int Stores number of elements in the array.

arr[] int Stores the array elements.

i Int Loop variable.

Sort function

Variable Datatype Desription


s

min int Stores the index of the minimum element in the array.

29
temp Int Used for swapping positions of elements in the array.

i int Loop variable

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.

Program Sorts an array using Selection Sort technique.


9

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.

sort Executes selection sort on a given array.

30
PROGARM 10:
Write a program to accept numbers in an array of size ‘n’ and replace each prime number with the

immediate next prime number.

ALGORITHM:

Program Name: Program10

main function:

Step 1: Start

Step 2: Creating an array and taking input of the elements.

Step 3: Then checking if the number at index ‘I’ is prime or not.

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;

int nextPrime(int n)//Checks and returns the next prime number

for(int i=n+1;i>0;i++)//finding the next prime number

copy=i;

int c=0;

for(int j=1;j<=i;j++)

if(i%j==0)

c++;//counter

if(c==2)//checks if the number is prime

break;//if it is prime then breaking out of the loop

return copy;

public static void main()

int b=0;

Program10 ob=new Program10();

Scanner sc=new Scanner(System.in);

System.out.println("Enter number of elements in array");

int n=sc.nextInt();//number of elemts in array

int arr[]=new int[n];

32
System.out.println("Enter the elements");

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

b=0;

arr[i]=sc.nextInt();

for(int j=1;j<=arr[i];j++)//checking if the number at index 'i' is prime or not

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;

System.out.println("The new array is");

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

System.out.print(arr[i]+" ");//printing the desired array

OUTPUT:

33
VARIABLE DESCRIPTION: 
main function:

Variables Datatype Desription

n Int Stores number of elements in the array.

arr[] int Stores the array elements.

i Int Loop variable.

j int Loop variable.

b int Counter variable.

p int Stores the value returned by ‘nextPrime’ function.

nextPrime function

Variable Datatype Desription


s
copy int Stores the next prime number

c Int Counter Variable.

i int Loop variable

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.

println To print on the monitor.

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

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