0% found this document useful (0 votes)
47 views33 pages

Computer Science Assignment

The document is a computer science assignment submission from a student named Jeevesh Shukla to Mrs. P. Vasudeva. It contains 6 sections on different programming concepts: 1) Programs on numbers, 2) Programs on 2D arrays, 3) Programs on recursion, 4) Programs on file operations, 5) Programs on strings, and 6) Programs on date and time. Each section includes algorithms and programs for solving sample problems related to the concept. Variable descriptions are also provided for the key variables used in the programs.

Uploaded by

ded
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
47 views33 pages

Computer Science Assignment

The document is a computer science assignment submission from a student named Jeevesh Shukla to Mrs. P. Vasudeva. It contains 6 sections on different programming concepts: 1) Programs on numbers, 2) Programs on 2D arrays, 3) Programs on recursion, 4) Programs on file operations, 5) Programs on strings, and 6) Programs on date and time. Each section includes algorithms and programs for solving sample problems related to the concept. Variable descriptions are also provided for the key variables used in the programs.

Uploaded by

ded
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

Computer Science

Assignment(2020-2021)

Boys’ High School & College

Submitted by: Submitted to:


Jeevesh Shukla Mrs. P. Vasudeva
XI-A

1
Index
A.Programs on numbers…………………...3
B. Programs on 2D array……………………9
C. Programs on recursion…………………..15
D. Programs on operation of files………….21
E. Programs on Strings………………………25
F. Programs on Date and Time…………….29

2
A. Programs on Numbers

1. Write a program to determine whether a number is


kaprekar or not.
Algorithm

Step 1: Start of algorithm.


Step 2: Input the number ‘n’.
Step 3: Calculate the square of the number.
Step 4: Convert the number into String for easier operations.
Step 5: Calculate the mid point of the number, and split the
number into 2 parts.
Step 6: Check if the sum of the two parts is equal to the number.
Step 7: If the sum and the number are both equal then print that
the number is kaprekar, else print that it is not.
Step 8: End of algorithm.

Program

import java.util.Scanner;
class CSA1
{
public static void main(String args[])
{Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to be checked");
int n=sc.nextInt();
int n1=n*n;
String n2=Integer.toString(n1);
int l=n2.length();
int mid=l/2;
int left=Integer.parseInt(n2.substring(0,mid));
int right=Integer.parseInt(n2.substring(mid));

3
if((left+right)==n)
{System.out.println("The number is a kaprekar number.");}
else
{System.out.println("The number is not a kaprekar
number.");
}
}
}

Variable Description

Variable Data Type Use


n int To store the number
input from the user.
n1 int To store the square of
the number ‘n’.
n2 String To store the number as a
String.
l int To store the number
characters of ‘n2’.
mid int To store the middle
value of ‘l’.
left int To store the value of the
left half of ‘n2’.
right int To store the value of the
right half of ‘n2’.

4
2. Write a program to check whether a number is
prime palindrome or not.

Algorithm

Step 1: Start of algorithm.


Step 2: Input the number.
Step 3: Calculate the number of factors of the number.
Step 4: Assign rev the value of n if it has only 2 factors.
Step 5: Reverse the number.
Step 6: Check if the reversed number is equal to the original
number.
Step 7: Print the appropriate output.
Step 8: End of algorithm.

Program

import java.util.Scanner;
class CSA2
{public static void main(String args[])
{Scanner sc=new Scanner(System.in);
int c=0,n,rev=0,s=0,r;
System.out.println("Enter the number");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{if(n%i==0)
{ c++;}}
if(c==2)
{rev=n;}
while(rev>0)
{r=rev%10;
s=s*10+r;
rev=rev/10;}
if(n==s)
{ System.out.println("Prime Palindrome");}
else

5
{System.out.println("Not Prime Palindrome"); }

}
}
Variable Description

Variable Data Type Use


c int Used as a counter.
n int To store the number
rev int Used to store the
value of ‘n’ if.
s int To store the reverse of
the number.
r int To store last digit of
‘rev’

6
3. Write a program in java to accept a
number and check whether it is an
Adam Prime Number or not.

Algorithm

1. Input a number.
2. Calculate the reverse of the number.
3. Calculate the square of the number.
4. Calculate the square of the reversed number.
5. If the square of the reversed number is equal to the
reverse of the square of the input number then it is
an Adam Prime number otherwise it is not an
Adam Prime number.
6. End algorithm.

Program

import java.util.Scanner;
class CSA3
{private static int reverse(int n)
{int rev=0;
while(n!=0)
{int d=n%10;
n=n/10;
rev=rev*10+d;
}return rev;
}

public static void main(String[]args)


{Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to be checked");
int n=sc.nextInt();
int rev=reverse(n);
int s1=n*n;
int s2=rev*rev;

7
if(reverse(s1)==s2)
{System.out.println("It is an Adam Prime number");
}else
{System.out.println("It is not an Adam Prime number");
}
}private static boolean isPrime(int n)
{int c=0;
for(int x=1;x<=n;x++)
{if(n%x==0)
{c++;
}}if(c>=3)
{return false;
}
else
{return true;
}
}}
Variable Description

Main()
Variable Data type Use
n int To store a number
rev int Store reverse of n
s1 int Store square of n
s2 int Store square of rev
isPrime(int n)
Variable Data Type Use
n int To input a number.
c int To count number of factors of n.
x int Loop variable
Reverse(int n())
Variable Data Type Use
n int To store a number
rev int To store reverse of n
d int To store a digit of n

8
B. Programs on 2D Arrays

4. Write a program in Java to input a 2D


array and find its saddle point.

Algorithm

Step 1: Start of algorithm.


Step 2: Traverse the row and find the smallest number. Save its
column number in a variable.
Step 3: Now traverse the same column check if the row ‘s
smallest number is greatest in its column.
Step 4: If yes print the number , if no check for next row.
Step 5: If no such point is there in the array, print no saddle
point.
Step 6: End of algorithm.

Program

import java.util.Scanner;
class CSA4 {
public static void main(String args[]) {
int i, j, num;
int sm, p, large, f = 1;
int a[][] = new int[100][100];
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of matrix:");
num = sc.nextInt();
System.out.print("Enter 2D Array Data :");
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
a[i][j] = sc.nextInt();
}

9
}System.out.print("Elements of The 2D array is :");
for (i = 0; i < num; i++) {
System.out.print("\n");
for (j = 0; j < num; j++) {
System.out.print(" " + a[i][j]);
}}
for (i = 0; i < num; i++) {
p = 0;
sm = a[i][0];
for(int j1=0;j1 < num; j1++) {
if (sm > a[i][j1]) {
sm = a[i][j1];
p=j1;
}}
large = 0;
for (j = 0; j < num; j++) {
if (large < a[j][p]) {
large = a[j][p];
}}
if (sm == large) {
System.out.print("\nValue of Saddle Point :" + sm);
f = 0;
}}
if (f > 0) {
System.out.print("\nNo Saddle Point ");
}
}}
Variable Description
Variable Data Type Use
ar[][] int To input the array
num int To store the size of the
array.
i int Loop variable
j int Loop variable
sm int To store the smallest
number
large int To store the
largest number

10
5. Write a program in java to rotate a
matrix to right.

Algorithm
Step 1. Start.
Step 2. Initialize the array.
Step 3. Set n=3.
Step 4. Print “Original array”.
Step 5. Repeat step 6 until i<arr.length.
Step 6. Print arr[i].
Step 7. Repeat step 8 to step 12 until i<n.
Step 8. Define j, last.
Step 9. Last=arr[arr.length-1].
Step 10. Repeat stepp 11 until j>0;
Step 11. Arr[j]=arr[j-1].
Step 12. Arr[0]=last.
Step 13.Print array after rotation.
Step 14.Repeat step 15 until i<arr.length.
Step 15.Print arr[].
Step 16.End algorithm.
Program

class CSA5 {
public static void main(String[] args) {
int [] arr = new int [] {1, 2, 3, 4, 5};
int n = 3;
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
for(int i = 0; i < n; i++){
int j, last;
last = arr[arr.length-1];
for(j = arr.length-1; j > 0; j--){
arr[j] = arr[j-1];
}
arr[0] = last; }

11
System.out.println();
System.out.println("Array after right rotation: ");
for(int i = 0; i< arr.length; i++){
System.out.print(arr[i] + " ");
}}}

Variable Description

Variable Data type Use


arr[] int To store the array
i int Loop variable
j int Loop variable

12
6. Write a program in java to multiply
two arrays.
Algorithm

Step 1. Start
Step 2. Take the two matrices to be multiplied
Step 3. Check if the two matrices are compatible to be
multiplied
Step 4. Create a new Matrix to store the product of the two
matrices
Step 5. Traverse each element of the two matrices and multiply
them. Store this product in the new matrix at the
corresponding index.
Step 6. Print the final product matrix
Step 7. End algorithm.

Program

public class CSA6{


public static void main(String args[]){
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}}

13
Variable Description

Variable Data Type Use


a[][] int To store first array
b[][] int To store second
array
c[][] int To store multiplied
array
i int Loop variable
j int Loop variable
k int Loop variable

14
C.Programs on Recursion
7. Write a program in java to input a
number and print the sum of digits of
the number using Recursion.

Algorithm

1. Start.
2. Input a number.
3. Create a function to calculate the sum of digits.
4. Find last digit of number using modular division by 10.
5. Add the last digit found above to sum variable.
6. Remove last digit from given number by dividing it by
10.
7. Repeat the above three steps till the number becomes 0.
8. End algorithm.

Program

import java.util.Scanner;
class SumNumbers
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
System.out.println(Sum(n));
}
private static int Sum(int n)
{
if(n==0)
{return 0;

15
}else
{
return Sum(n%10)+Sum(n/10);
}
}}

Variable Description
Variable Data Type Use
n int To input a number

16
8. Write a program in java to input a
number and print the sum of the squares
of the digits of the number.

Algorithm

1. Start.
2. Input a number.
3. Declare a recursive function to calculate the sum of the
square of the digits of the number.
4. If n is equal to 0 then print 0 and goto step 6 else goto step
5.
5. Take out the last digit of the number and add its square of
that digit and change the value of the number with its
quotient when divided by 10 and goto step 4.
6. End algorithm.

Program

import java.util.Scanner;
class SumOFTheSquaresOfTheDigits
{ public static int sumSq(int n)
{ if(n==0)
{
return 0;
} else
{ int d=n%10;
return sumSq(n/10)+d*d;
}
}
public static void main(String[]args)
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt(); System.out.println(sumSq(n));
}}

17
Variable Description

sumSq(int n)
Variable Data Type Use
n int To store the number
d int To store the digit of
the number
Main()
Variable Data Type Use
n int To store the number

18
9 Write a program in java to count the
number of digits of a number using
Recursion.

Algorithm
1. Start.
2. Input a number.
3. Create a function to count the number of digits of a
number.
4. Find last digit of number using modular division by 10.
5. Since the number is not 0, hence add 1.
6. Repeat the above two steps until the number is 0.
7. End algorithm.

Program

import java.util.Scanner;
class CountDigits
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
System.out.println(count(n));
}
private static int count(int n)
{
if(n<10)
{
return 1;
}
else
{
return count(n/10)+1;
}

19
}}
Variable Description
Variable Data Type Use
n int To store a number

20
D: Programs on operations on
files
10. Write a program in java to Delete text
from a file.

Algorithm

1. Create PrintWriter object for output.txt


2. Open BufferedReader for input.txt
3. Run a loop for each line of input.txt
3.1 flag = false
3.2 Open BufferedReader for delete.txt
3.3 Run a loop for each line of delete.txt
-> If line of delete.txt is equal to current line of input.txt
-> flag = true
-> break loop
4. Check flag, if false
-> write current line of input.txt to output.txt
5. Flush PrintWriter stream and close resources.

Program
import java.io.*;
public class FileOperation
{
public static void main(String[] args) throws IOException
{
PrintWriter pw = new PrintWriter("output.txt");
BufferedReader br1 = new BufferedReader(new
FileReader("input.txt"));
String line1 = br1.readLine();
while(line1 != null)
{
boolean flag = false;
BufferedReader br2 = new BufferedReader(new

21
FileReader("delete.txt"));
String line2 = br2.readLine();
while(line2 != null)
{
if(line1.equals(line2))
{
flag = true;
break;
}
line2 = br2.readLine();
}
if(!flag)
pw.println(line1);
line1 = br1.readLine();
}
pw.close();
br1.close();
pw.close();
System.out.println("File operation performed
successfully");
}
}

Variable Description
Variable Data Type Use
Line1 String Store the text to be
deleted
Line2 String Store the deleted texts
flag flag Check whether the line
exists in the file

22
11. A text file named “Student.txt”
contains the students’ names (std) and
marks (mks) of number of students.
Write a method to check and display the
names of the students who have secured
more than 75 marks.

Algorithm
1. Start
2. Call FileReader and BufferedReader for Student.txt
3. Extract the name of the student as well as their marks
4. Print the names of the students having marks greater than
75
5. Repeat steps 3 and 4 until no name is left in the file
6. End algorithm.

Program
import java.io.*;
import java.util.*;
class Print
{ public static void check()throws IOException{
FileReader fr = new FileReader("Student.txt");
BufferedReader br = new BufferedReader(fr);
String s = "";
while((s = br.readLine()) != null){
String std = s.substring(0, s.lastIndexOf(‘ ‘));
int mks = Integer.parseInt(s.substring(s.lastIndexOf(' ')
+ 1));
if(marks > 75)
System.out.println(std);
}
br.close();
fr.close();
}
}

23
Variable Description
Variable Data type Use
s String To store the names of
the students
mks int To store the marks of
the students

24
E: Program on Strings

12. Write a program in java to find the


number of tokens present in a String by
using String Tokenizer.

Algorithm
1. Start
2. Input the string
3. Call StringTokenizer in the program
4. Using iteration print and count the tokens
5. End algorithm

Program

import java.util.*;
class Token
{
public static void main(String str)
{
StringTokenizer st=new StringTokenizer(str);
int c=0;
String str1;
System.out.println("The tokens of the String are ");
while(st.hasMoreTokens())
{
str1=st.nextToken();
System.out.println(str1);
c=c+1;
}
System.out.println("Number of tokens is "+c);
}
}

25
Variable Description
Variable Data Type Use
Str1 String To store a single token
c int To count the number
of tokens

26
13. Write a program in java to find the
frequency of a word present in a String
using StringTokenizer.

Algorithm

1. Start
2. Input String and the word
3. Call StringTokenizer in the class
4. Using iteration count the frequency of the word in the
string
5. Print the frequency
6. End algorithm

Program

import java.util.*;
public class Word
{
public static void main(String str, String wd)
{
StringTokenizer st=new StringTokenizer(str);
int c=0;
String str1;
while(st.hasMoreTokens())
{
str1=st.nextToken();
if(wd.equals(str1))
c+=1;
}
System.out.println("Frequency of "+wd+" is "+c);
}
}

27
Variable Description
Variable Data Type Use
str String To store the string
wd String To input the word
c int To count the frequency
Str1 String To count a single word
of str

28
F. Programs on date and time

14. Write a program in java to input a date


and print the next date.

Algorithm

1. Start.
2. Input date in format dd/mm/yyyy.
3. Extract the day, month and year from this.
4. If the day is the last date of the month then goto step 5
else goto step 7.
5. If the month is the 12th month of the year then change day
and month to 1 and increase the year else change day to 1
and increase the month number.
6. Goto step 8.
7. Just increase the date.
8. End algorithm.

Program

import java.util.*;
class NextDate
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter date(dd/mm/yyyy)");
String date=sc.nextLine();
StringTokenizer st=new StringTokenizer(date,"/");
int d=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
int y=Integer.parseInt(st.nextToken());
int ar[]={31,(y%4==0?29:28),31,30,31,30,31,31,30,31,30,31};
if(d==ar[m-1])

29
{
if(m==12)
{
d=1;
m=1;
y++;
} else
{
d=1;
m++;
}
}
else
{
d++;
}
System.out.println(d+"/"+m+"/"+y);
}

Variable Description
Variable Data Type Use
date String To store the input date
d int To store date
m int To store month
y int To store year
ar[] int To store the days in
every month

30
15. Write a program in java to input a date
and print its previous date.

Algorithm
1. Start.
2. Input date in the format dd/mm/yyyy.
3. Extract the day, month and year from this.
4. If the day is the first date of the month then goto step 5
else goto step 7.
5. If the month is 1 then change the day to the last day of the
previous month and change the value of the month to 12
else change the day to the last day of the previous month
and decrease the value of m.
6. Goto step 8.
7. Just dcrease the value of day.
8. End algorithm

Program

import java.util.*;
class PreviousDate
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter date(dd/mm/yyyy)");
String date=sc.nextLine();
StringTokenizer st=new StringTokenizer(date,"/");
int d=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
int y=Integer.parseInt(st.nextToken());
int ar[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(y%4==0)
{
ar[1]=29;
}

31
if(d==1)
{
if(m==1)
{ d=ar[0];
m=12;
y--;
}
else
{
m--;
d=ar[m-1];
}
}
else
{
d--;
}
System.out.println(d+"/"+m+"/"+y);
}
}

Variable Description
Variable Data Type Use
date String To store the input date
d int To store the day
m int To store the month
y int To store the year
ar[] int To store the days in
every month

32
33

You might also like