Computer Science Assignment
Name : Aditya Krishna Das
Class : XI
Roll No. : 2
Question.
A palindrome is a word that may be read the same way in either direction.Accept a sentence in
uppercase which is terminated by either .,? or !. Each word of the sentence is separated by a single blank
space.
a)Count the number of palindromic words in the sentence.
b) Display the palindromic words in the sentence.
Algorithm :
Step 1:Start
Step 2:take str3
Step 3:set l=str3.length()
Step 4:set c=str.charAt(l-1)
Step 5: if c=='.'||c=='?'||c=='!' then
Step 6:set a=str1.length()
Step 7:set str2=str1.charAt(j)+str2
Step 8:Repeat step 7 a times.
Step 9:if str1.compareTo(str2)=0 then continue else go to step 14
Step 10: count=count+1
Step 11: print str1
Step 12:set str1=""
Step 13:set str2=""
Step 14:else
Step 15: set str1=str1+k
Step 16:if count !=0
Step 17:print count
Step 18:else
Step 19:print invalid statement
Step 20:Stop
Source Code :
import java.io.*;
class palindrome
public static void main()throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the sentence.");
String str=br.readLine();
String str1="";
int l=str.length();
str=str.toUpperCase();
char c=str.charAt(l-1);
if(c=='.'||c=='?'||c=='!')
String str2="";
int count=0;
for(int i=0;i<l;i++)
char k=str.charAt(i);
if(k==' '||i==(l-1))
{
int a=str1.length();
for(int j=0;j<a;j++)
str2=str1.charAt(j)+str2;
if(str1.compareTo(str2)==0)
count++;
System.out.print(str1+" ");
str1="";
str2="";
else
str1=str1+k;
System.out.println( );
if(count!=0)
System.out.println("Number of palindromic words is "+count);
else
System.out.println("No palindromic words.");
else
System.out.println("Invalid statement.");
}
Output :
Variable Description :
Serial no. Variable name Data type Description
1. str3 String Contains the string
given as input by the
user.
2. str String Contains str3 in
uppercase.
3. Str1 String Contains the words of
the sentence.
4. Str2 String Contains str1 in the
reverse case.
5. l Int Contains the length of
the string.
6. c char Contains the last
character of str.
7. i Int Used as a variable in for
loop.
8 count Int Counts the number of
palindromic words in
the sentence.
9. k Int Contains the characters
of str.
10. a Int Stores the length of
each word of the string.
Question.
An ISBN is a ten digit code which uniquely identifies a book .Each of the first nine digits of the code can
take a value between 0 and 9. Sometimes it is necessary to make the last digit equal to 10. This is done
by writing the last digit of the code as ‘X’.To verify an ISBN,calculate 10*d1+…+1*d10 is divisible by 11.
Algorithm :
Step 1:take str
Step 2:set l=str.length()
Step 3:if l!=10 then continue else goto step 6.
Step 4:print invalid
Step 5:else
Step 6:set ch=str.charAt(i)
Step 7:set p=Character.isDigit(ch)
Step 8: if p==true or i==(l-1)
Step 9:if i==(l-1) and ch=='X'
Step 10:set s=s+10
Step 11:else if i!=(l-1) and ch=='X'
Step 12: set c=c+1
Step 13:print invalid and stop
Step 14:else
Step 15:set s=s+((int)(ch)-48)*(10-i);
Step 16:else
Step 17:print invalid and stop.
Step 18: if c!=1 else go to step 22.
Step 19:if s%11!=0
Step 20:print leaves remainder. Invalid
Step 21:else
Step 22:print valid
Source Code :
import java.io.*;
class ISBN
public static void main()throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the code");
String str=br.readLine();
int l=str.length();
int c=0;
long s=0;
if(l!=10)
System.out.println("Invalid.");
c++;
else
for(int i=0;i<l;i++)
{
char ch=str.charAt(i);
boolean p=Character.isDigit(ch);
if(p==true||i==(l-1))
if(i==(l-1)&&ch=='X')
s=s+10*1;
else if(i!=(l-1)&&ch=='X')
c++;
System.out.println("Invalid.");
break;
else
s=s+(int)(ch-48)*(10-i);
else
c++;
System.out.println("Invalid.");
break;
if(c!=1)
{
if(s%11!=0)
System.out.println("Leaves remainder.Invalid.");
else
System.out.println("Valid.");
Output :
Variable Description :
Serial no. Variable name Data type Description
1. str String Stores the string given as
input by the user.
2. l int Stores the length of the
string.
3. c int Stores 1 or 0accordingly
as string is valid or
invalid.
4. ch char Stores the character of
str.
5. p boolean Checks if each character
is a digit.
6. s int Stores the sum.
Question.
Write a program to declare a square matrix A[][], of order m x m; where m is the no. of rows and no. of
columns such that 2<m<20. Allow the user to input integers into this matrix. Display appropriate error
message for an invalid output. Perform the following tasks :
(a) Display the input matrix.
(b) Create a mirror image matrix.
(c) Display the mirror image matrix.
Algorithm :
Step 1:Start
Step 2:set c=0;
Step 3:take m
Step 4:if m>2 and m<20 else go to step 12.
Step 5:declare array A of size m*m
Step 6:declare array mirr of size m*m
Step 7: take elements of A
Step 8:set mirr[i][m-j-1]=A[i][j]
Step 9:print original matrix
Step 10:print the mirror matrix
Step 11:else
Step 12:print invalid input
Source Code :
import java.util.*;
class mirrormatrix
{
public static void main()
Scanner in=new Scanner(System.in);
int c=0;
System.out.println("Enter the size");
int m=in.nextInt();
if(m>2&&m<20)
int Arr[][]=new int[m][m];
int mirr[][]=new int[m][m] ;
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
Arr[i][j]=in.nextInt();
mirr[i][m-j-1]=Arr[i][j];
System.out.println("Original matrix.");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
System.out.print(Arr[i][j]+" ");
}
System.out.println( );
System.out.println("Mirror matrix.");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
System.out.print(mirr[i][j]+" ");
System.out.println( );
else
System.out.println("Invalid input.");
Output :
Variable Description :
Serial no. Variable name Data type Description
1. m int Stores the size of the
array.
2. i int Used as a variable in for
loop.
3. j int Used as a variable in for
loop.
Question.
Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100. Find
the smallest integer that is greater than M and whose digits add up to N. For example, if M=100 and
N=11, then the smallest integer greater than 100 whose digits add up to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest required number
whose sum of all its digits is equal to N. Also, print the total number of digits present in the required
number. The program should check for the validity of the inputs and display an appropriate message for
an invalid input.
Algorithm :
Step 1:start
Step 2:take m
Step 3:if m>2 and m<20 then continue else go to step 18
Step 4: declare an array A of size m*m
Step 5:print the elements of the array
Step 6:set c=0
step 7:if a[i][j]!=a[j][i]
step 8:c=c+1
Step 9:Repeat step 7 and 8 till i<=m and j<=m
Step 10:if c>=1
Step 11:print non symmetric matrix and stop
Step 12:set s1=0
Step 13:set s2=0
Step 14:initialize aloop with l=0
Step 15:set s1=s1+A[l][l]
Step 16:loop..
Step 17:set s2=s2+a[f][m-f-1]
Step 18:else
Step 19:print invalid
Source Code :
import java.util.*;
class magicSum
public static void main()
Scanner in=new Scanner(System.in);
System.out.println("Enter m and n.");
int m=in.nextInt();
int n=in.nextInt();
int x=0;
int d=0;
int c=0;
int s=0;
if(m>=100&&m<=10000)
if(n<=100)
for(int i=(m+1);;i++)
x=i;
do
c++;
d=i%10;
s=s+d;
i/=10;
while(i!=0);
if(s==n)
System.out.println(x);
System.out.println(c);
break;
else
s=0;
c=0;
i=x;
else
System.out.println("Invalid.");
else
System.out.println("Invalid.");
}
}
Output :
Variable Description :
Serial no. Variable name Data type Description
1. m int To store the input of the
user.
2. n int To store the input of the
user.
3. i int Used as a variable in a
for loop.
4. x int To store the value of i.
5. c int To store the number of
digits in the required
number.
6. d int To store the digits if the
required number.
7. s int To calculate the sum of
digits of the number.
Question.
Write a program to declare a square matrix A[][] of order m x m, where m is the number of rows and
columns, such that 2 < m < 10. Accept the value of m as user input. Display an appropriate message for
invalid input. Allow the user to input integers into the matrix. Perform the following tasks :
(a) Display the original matrix.
(b) Check if the matrix is symmetric or not. A square matrix is symmetric if the element of the i th row and
jth column is equal to the element of jth row and ith column.
Algorithm :
Step 1:Start
Step 2:take m
Step 3:take n
Step 4:if m<100 or m>10000 then continue else go to step 22
Step 5:if n<=100
Step 6:set x=i
Step 7:set c=c+1
Step 8:set d=i%10
Step 9:set s=s+d
Step 10:set i=i/10
Step 11: repeat step 6 to 10 till i>0
Step 12:if s=n
Step 13: print x
Step 14:print c
Step 15:Stop
Step 16:else
Step 17:set s=0
Step 18:set c=0
Step 19:set i=x
Step 20:repeat this process till s=n
Step 21:else
Step 22:print invalid
Step 23:print invalid
Source Code :
import java.util.*;
class symmetric
public static void main()
Scanner in=new Scanner(System.in);
System.out.println("Enter the size.");
int m=in.nextInt();
if(m>2&&m<10)
int a[][]=new int[m][m];
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
a[i][j]=in.nextInt();
}
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
System.out.print(a[i][j] +" ");
System.out.println( );
int c=0;
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
if(a[i][j]!=a[j][i])
c++;
if(c>=1)
System.out.println("Not symmetric.");
break;
if(c==0)
System.out.println("Symmetric.");
int s1=0;
int s2=0;
for(int i=0;i<m;i++)
s1=s1+a[i][i];
for(int j=0;j<m;j++)
s2=s2+a[j][m-j-1];
System.out.println("The sum of left and right diagonal are " + s1+" and "+s2+" respectively.");
else
System.out.println("Invalid.");
Output :
Variable Description :
Serial no. Variable name Data type Description
1. m int To store the size of the
array.
2. i int Used as a variable in for
loop.
3. j int Used as a variable in for
loop.
4. c int Used as a counter.
5. k int Used as a variable in for
loop.
6. S1 int Used to calculate the
sum of the left diagonal.
7. S2 int Used to calculate the
sum of the right
diagonal.
Question.
A composite magic number is a positive integer which is composite as well as magic number. Composite
number is a number that has more than 2 factors and magic number is a number in which the individual
sum of digits is equal to 1.
Accept two positive integers m and n, where m<n as user input. Display the number of composite magic
integers, that are in the range [m, n]. Output them along with the frequency.
Algorithm :
Step 1:Start
Step 2:Set c=0
Step 3:if j%i=0
Step 4:Set c=c+1
Step 5: Repeat step 3 till i<=j/2
Step 6:if c=1 return 0
Step 7: else return 1
Step 8:set p=0
Step 9:set p=x
Step 10:set d=x%10
Step 11:set s=s+d
Step12:set x=x/10
Step 13:repeat step 10 to 12 till x>0
Step 14:Repeat step 8 to 13 till s>9
Step 15:if s==1 return 1
Step 16:else return 0
Step 17:take m
Step 18:take n
Step 19:set f=0
Step 20: if m>=n print invalid
Step 21:else print the magic numbers are
Step 22:create object obj
Step 23:set l=obj.prime(j)
Step 24:set w=obj.sum(j)
Step 25:if w==1 && l==0 then print j
Step 26: set f=f+1
Step 27:Repeat step 23 to 26 ,m times
Step 28:print f
Step 29:Stop
Source Code :
import java.util.*;
class magicsum
int prime(int j)
int c=0;
for(int i=2;i<=j/2;i++)
if(j%i==0)
c++;
break;
}
}
if(c==1)
return 0;
else
return 1;
int sum(int x)
int p=0;
int s=0;
int d=0;
do
s=0;
p=x;
do
d=x%10;
s+=d;
x/=10;
while(x!=0);
x=s;
while(s>9);
if(s==1)
return 1;
else
return 0;
public static void main()
Scanner in=new Scanner(System.in);
System.out.println("Enter the number.");
int m=in.nextInt();
int n=in.nextInt();
int f=0;
magicsum obj=new magicsum();
if(m>=n)
System.out.println("Invalid.");
else
System.out.println("The magic numbers are:");
for(int k=m;k<=n;k++)
int l=obj.prime(k);
int w=obj.sum(k);
if(w==1&&l==0)
{
System.out.println(k+" ");
f++;
System.out.print("\"The frequency is ");
System.out.print(f);
Output :
Variable Description :
Serial no. Variable name Data type Description
1. i int Used as a variable in a
for loop.
2. j int Stores the number
which is to checked that
whether it is composite.
3. c int Used as a counter.
4. x int Stores the number
whose sum of digits is
to be found.
5. s int Calculates the sum of
digits of the number.
6. d int Stores the digits of the
number.
7. p int Stores x temporarily.
8. m int Stores the input of the
user.
9. n int Calculates the sum of
digits of the number.
10. k int Used as a variable in for
loop.
11. w int Stores a value
accordingly the given
number is composite or
not.
12. l int Stores a value
accordingly the given
number is magic
number or not.
Q. Write a program to accept a sentence which may be terminated by ‘.’ or ‘?’ or ‘,’ . Any other character
may be ignored. The words may be separated by more than one blank space and are in upper case.
Perform the following operations:
(a) Accept the sentence and reduce all the extra blank space in between two words to a single space.
(b) Accept a word from the user which is a part of the sentence along with its position number and
delete the word and display the sentence.
Algorithm :
Step 1:Start
Step 2:print “give input”
Step 3:take s
Step 4:set l=s.length()
Step 5:set str1=””
Step 6:set c=0
Step 7:set ch=s.charAt(l-1)
Step 8:if(ch==’.’||ch==’,’||ch==’?’) then continue else go to step 23
Step 9: set ch=s.charAt(i)
Step 10: if ch==’ ‘ set c=c+1
Step 11:else set c=0
Step 12: if c<=1 set str1=str1+ch
Step 13:print str1
Step 14:print “give input”
Step 15:take word
Step 16: take p
Step 17:set l=str1.length
Step 18:set a=0
Step 19:set str2=””
Step 20:set ch=str1.charAt(j)
Step 21:if ch==’ ‘ then set a=a+1
Step 22: print str2
Step 23:else print “invalid input”
Step 24:Stop
Source Code :
import java.util.*;
class sent
public static void main()
Scanner in=new Scanner(System.in);
System.out.println("Give input.");
String s=in.nextLine();
int l=s.length();
String str1="";
int c=0;
char ch;
ch=s.charAt(l-1);
if(ch=='.' || ch==','|| ch=='?')
for(int i=0;i<l;i++)
ch=s.charAt(i);
if(ch==' ')
c++;
else
c=0;
if(c<=1)
str1=str1+ch;
System.out.println(str1);
System.out.println("Give input.");
String word=in.next();
int p=in.nextInt();
l=str1.length();
int a=0;
String str2="";
for(int j=0;j<l;j++)
ch=str1.charAt(j);
if(ch==' ')
a++;
if(a!=(p-1)||ch==','||ch=='.'||ch=='?')
str2=str2+ch;
System.out.println(str2);
else
System.out.println("Invalid input");
}
Output :
Variable Description :
Serial no. Variable name Data type Description
1. s String Stores the string given
as input by the user.
2. l int Stores the length of s.
3. str1 String Used to display the
string after removing
extra spaces.
4. c int Used as a counter.
5. ch char Stores the character of
the s.
6. word String Stores the word to be
removed.
7. p int Stores the position of
the word to be
removed .
8. str2 String Displays the string by
removing the word.
9. a int Counts number of
spaces.
10. j int Used as a variable in for
loop.