Computer Science Assignment Name: Aditya Krishna Das Class: XI Roll No.: 2
Computer Science Assignment Name: Aditya Krishna Das Class: XI Roll No.: 2
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.
Algorithm :
Step 1:Start
Step 14:else
Step 18:else
Source Code :
import java.io.*;
class palindrome
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)
else
else
System.out.println("Invalid statement.");
}
Output :
Variable Description :
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 5:else
Step 14:else
Step 16:else
Step 21:else
Step 22:print valid
Source Code :
import java.io.*;
class ISBN
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 :
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 :
Algorithm :
Step 1:Start
Step 3:take m
Step 11:else
Source Code :
import java.util.*;
class mirrormatrix
{
public static void main()
int c=0;
int m=in.nextInt();
if(m>2&&m<20)
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
Algorithm :
Step 1:start
Step 2:take m
step 8:c=c+1
Step 16:loop..
Step 18:else
class magicSum
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
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 :
(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 14:print c
Step 15:Stop
Step 16:else
Step 21:else
Source Code :
import java.util.*;
class symmetric
int m=in.nextInt();
if(m>2&&m<10)
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.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 :
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
Step12:set x=x/10
Step 17:take m
Step 18:take n
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;
int m=in.nextInt();
int n=in.nextInt();
int f=0;
if(m>=n)
System.out.println("Invalid.");
else
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(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 3:take s
Step 24:Stop
Source Code :
import java.util.*;
class sent
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);
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.