0% found this document useful (0 votes)
16 views39 pages

Computer Project Manthan

The document contains programs related to matrices and strings. It includes programs to calculate sum and product of matrices, find transpose of a matrix, check if a matrix is symmetric, search a number in matrix. For strings, it has programs to check palindrome, count words, characters and digits.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
16 views39 pages

Computer Project Manthan

The document contains programs related to matrices and strings. It includes programs to calculate sum and product of matrices, find transpose of a matrix, check if a matrix is symmetric, search a number in matrix. For strings, it has programs to check palindrome, count words, characters and digits.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 39

DOON CAMBRIDGE

SCHOOL
248001- Dehradun, Uttarakhand

PROJECT OF
COMPUTER SCIENCE
(2024-25)

Submitted by:- Submitted to:-


MANTHAN DHIMAN Mrs. KHILA BHANDARI

XII SCIENCE (COMPUTER TEACHER)

1
ACKNOWLEDGEMENT

I would like to extend my sincere thanks to Mrs. Khila Bhandari my Computer teacher, for
her valuable guidance and support throughout the duration of this project. Her insights and
feedback have been immensely helpful and have greatly contributed to the completion of this
work.
Special thanks go to my family, who have provided me with the necessary resources and a
conducive environment to work on this project. Their constant encouragement and belief in
my abilities have been a source of motivation.
Lastly, I thank all those who directly or indirectly contributed to the success of this project .

~Manthan Dhiman

INDEX
2
S.NO TITLE PAGE REMARK
1. Program to calculate the sum of two matrices 4
2. Program to find the transpose of the given matrix 7
3. Program to check if the matrix is symmetric or not 10
4. Program to search a number in the given matrix 13
5. Program to calculate the product of given matrices 16
6. Program to find given string is palindrome or not 19
7. Program to count the number of words in the given
string 21

8. Program to count the repetition of an alphabet in a 23


string
9. Program to print the words with even length from the 25
given string
10. Program to count the total number of uppercase, 27
lowercase, special characters and digits in a given
string
11. Program to print all the palindrome words in the given 29
sentence
12. Program to check entered number is prime or not 31
13. Program to check the entered number is neon or not 33
14. Program to convert the entered Decimal number to its 35
binary equivalent
15. Program to calculate factorial of a given number 37

PROGRAMS
3
Ques 1. Write a program to calculate the sum of two given matrices: -

Algorithm: -
1. Start
2. Declare class
3. Declare scanner class
4. Taking the size of matrix as input to variable ‘l’ & ’w’
5. Declaring variables for loop i.e. ‘i’ & ‘j’
6. Declaring 2D array ‘a’ & ’b’
7. Taking matrix 1 elements as input and displaying them
8. Taking matrix 2 elements as input and displaying them
9. Calculating the sum in a matrix ‘sum’
10. Displaying the sum
11. Stop

// program to calculate the sum of two given matrices


import java.util.*;
class MatrixSum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of matrices:");
int l=sc.nextInt();
int w=sc.nextInt();
int i,j;
int a[][]=new int[l][w];
int b[][]=new int[l][w];
int sum[][]=new int[l][w];

System.out.println("Enter matrix 1 elements:");


for(i=0;i<l;i++)

4
{
for(j=0;j<w;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix 1 elements:");
for(i=0;i<l;i++){
for(j=0;j<w;j++){
System.out.print(a[i][j]+" \t ");
}
System.out.println();
}
System.out.println("Enter matrix 2 elements:");
for(i=0;i<l;i++){
for(j=0;j<w;j++){
b[i][j]=sc.nextInt();
}
}

System.out.println("Matrix 2 elements:");
for(i=0;i<l;i++){
for(j=0;j<w;j++){
System.out.print(b[i][j]+" \t ");
}
System.out.println();
}
System.out.println("SUM of matrix:");
for(i=0;i<l;i++){
for(j=0;j<w;j++){
sum[i][j]=a[i][j]+b[i][j];
}
5
}
for(i=0;i<l;i++){
for(j=0;j<w;j++){
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
}
}

Output:-

Ques 2. Write a program to find the transpose of a given matrix:-

6
Algorithm:-
1. Start
2. Declare class
3. Declare Scanner class
4. Taking the size of matrix as input to variable ‘l’ & ‘b’
5. Declaring matrix ‘a’
6. Declaring Transpose matrix ‘T’
7. Taking matrix elements as input and displaying them
8. Calculating the transpose and storing it in matrix ‘T’
9. Displaying the transpose matrix
10. Stop

// program to find the transpose of a given matrix


import java.util.*;
class Transpose
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of matrix:");
int l=sc.nextInt();
int b=sc.nextInt();
int a[][]= new int[l][b];
int T[][]= new int[l][b];
System.out.println("Enter matrix elements:");
for(int i=0;i<l;i++){
for(int j=0;j<b;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix elements:");

for(int i=0;i<l;i++){
for(int j=0;j<b;j++){
7
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(int i=0;i<l;i++){
for(int j=0;j<b;j++){
T[j][i]=a[i][j];
}
}
System.out.println("Transpose:");
for(int i=0;i<l;i++){
for(int j=0;j<b;j++){
System.out.print(T[i][j]+"\t");
}
System.out.println();
}
}
}

Output:-

8
Ques 3. Write a program to check the given matrix is symmetric or not:-

9
Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Taking the size of matrix as input to variable ‘l’ & ’b’
5. Declaring 2D array ‘a’ & ’b’
6. Taking matrix elements as input and displaying them
7. Checking if the matrix is a square matrix or not
8. If matrix is not square then it is not symmetric
9. If matrix is square then check the elements
10. Display the result accordingly
11. Stop

// program to check the given matrix is symmetric or not


import java.util.*;
class SymmetricMatrix
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of matrix: ");
int l = sc.nextInt();
int b = sc.nextInt();
int matrix[][] = new int[l][b];
System.out.println("Enter the elements :");
for (int i=0;i<l;i++){
for (int j=0;j<b;j++){
matrix[i][j] = sc.nextInt();
}
}
System.out.println("Matrix :");
for (int i=0;i<l;i++){
for (int j=0;j<b;j++){
10
System.out.print(matrix[i][j]+"\t");
}
System.out.println();
}
if(l != b){
System.out.println("Matrix is not Symmetric");
}
else{
int k=1;;
for (int i=0;i<l;i++){
for (int j=0;j<b;j++){
if(matrix[i][j]!=matrix[j][i]){
k=0;
break;
}
}
}
if(k==1){
System.out.println("Matrix is Symmetric");
}
else{
System.out.println("Matrix is not Symmetric");
}
}
}
}

Output:-
11
12
Ques 4. Write a program to find a number in the given matrix:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Taking the size of matrix as input to variable ‘l’ & ’b’
5. Declaring a matrix ‘a’
6. Taking matrix elements as input and displaying them
7. Checking if the matrix is a square matrix or not
8. Displaying the transpose matrix
9. Stop

// program to find a number in the given matrix


import java.util.*;
class MatrixSearch
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of matrix:");
int l=sc.nextInt();
int b=sc.nextInt();
int a[][]= new int[l][b];
int k=0, n;
System.out.println("Enter matrix elements:");
for(int i=0;i<l;i++){
for(int j=0;j<b;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter number to be searched:");
n=sc.nextInt()

13
System.out.println("Matrix elements:");
for(int i=0;i<l;i++){
for(int j=0;j<b;j++){
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(int i=0;i<l;i++){
for(int j=0;j<b;j++){
if(a[i][j]==n){
k=1;
}
}
}
System.out.println("******************************");
if(k==1){
System.out.println("Number Found");
}
else{
System.out.println("Number not found");
}
System.out.println("******************************");
}
}

14
Output: -

15
Ques 5. Write a program to calculate the product of two matrices given by the user:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Taking the size of matrix as input to variable ‘l’
5. Declaring a matrix ‘a’ & ‘b’
6. Taking matrix elements as input and displaying them
7. Calculating the product of matrices
8. Displaying the result
9. Stop

//program to calculate the product of given matrices


import java.util.*;
class matrixproduct
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of matrices:");
int l=sc.nextInt();
int i,j;
int a[][]=new int[l][l];
int b[][]=new int[l][l];
int product[][]=new int[l][l];
System.out.println("Enter "+l+"X"+l+" matrix 1 elements:");
for(i=0;i<l;i++){
for(j=0;j<l;j++){
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix 1 elements:");

16
for(i=0;i<l;i++){
for(j=0;j<l;j++){

System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Enter "+l+"X"+l+" matrix 2 elements:");
for(i=0;i<l;i++){
for(j=0;j<l;j++){
b[i][j]=sc.nextInt();
}
}
System.out.println("Matrix 2 elements:");
for(i=0;i<l;i++){
for(j=0;j<l;j++){
System.out.print(b[i][j]+" ");
}
System.out.println();
}
System.out.println("Multiplication of matrix:");
for(i=0;i<l;i++){
for(j=0;j<l;j++){
product[i][j]=0;
for(int k=0;k<l;k++){
product[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(product[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
}
}
17
}
Output:-

18
Ques 6. Write a program to find the entered string is palindrome or not:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Declare string variables to store string and its reverse
5. Taking the original string as input and storing it in ‘str’ then displaying it
6. Finding the reverse of the string and storing it in ‘revstr’
7. Checking if the string is palindrome or not
8. Displaying the result accordingly
9. Stop

//Program to find the string is palindrome or not


import java.util.*;
class PalindromeString
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str, revstr;
revstr="";
System.out.println("Enter any lowercase/uppercase string");
str=sc.next();
System.out.println("Original String: "+str);
for(int i=str.length()-1;i>=0;i--){
revstr=revstr + str.charAt(i);
}
System.out.println("Reversed String: "+revstr);
System.out.println("*******************************");
if(revstr.equals(str))
{
System.out.println("Palindrome String");
}

19
else{

System.out.println("Not a Palindrome String");


}
System.out.println("*******************************");
}
}

Output:-

20
Ques 7. Write a program to count the number of words in the given string:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Declare string variables to store string
5. Taking sting as Input
6. Logic to count the words in the sentence
7. Displaying the result accordingly
8. Stop

//program to count the words in a sentence


import java.util.*;
class WordCount
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s;
int k,l;
k=0;
char c;
System.out.println("Enter a string");
s=sc.nextLine();
l=s.length();
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c==' '||c=='.')
{
k=k+1;
}
}
21
System.out.println("*******************");
System.out.println("Total words: "+k);
System.out.println("*******************");
}
}

Output:-

22
Ques 8. Write a program to count the repetition of an alphabet in a string:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Declare string variables to store string
5. Taking sting as Input
6. Take the alphabet to count as input
7. Count the number of times alphabet is repeating
8. Displaying the result accordingly
9. Stop

//program to count how many times the given alphabet is repeating


import java.util.*;
class RepeatingAlphabet{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s;
char c,a;
int i,k,l;
k=0;
System.out.println("Enter the string:");
s=sc.nextLine();
System.out.println("Enter the alphabet to count:");
a=sc.next().charAt(0);
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c==a)
{
k++;

23
}
}
System.out.println("Number of times "+a+" is present: "+k);
}
}

Output:-

24
Ques 9. Write a program to separate words with even length from a given string:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Declare string variables to store string
5. Taking sting as Input
6. Converting string into a string type array
7. Checking if the word has even length or not
8. Displaying the words with even length
9. Stop

//program to print the words with even length from the given string
import java.util.*;
class EvenLength
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s;
int l;
System.out.println("Enter the String:");
s=sc.nextLine();
l=s.length();
System.out.println("Words with even length:");
String str[]=s.split(" ");
for(int i=0;i<str.length;i++){
if(str[i].length()%2==0){
System.out.println(str[i]);
}
}
}
}

25
Output:-

26
Ques 10. Write a program to count the total number of uppercase, lowercase, special
characters and digits in a given string:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Declare string variables to store string
5. Taking sting as Input
6. Count the number of all types of characters and store their values
7. Display the result
8. Stop

// program to count the total number of uppercase, lowercase, special characters and digits in
a given string
import java.util.*;
class Count
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s;
int u,l,sp,d,i;
u=l=sp=d=0;
char c;
System.out.println("Enter the string:");
s=sc.nextLine();
for(i=0;i<s.length();i++){
c=s.charAt(i);
if(Character.isUpperCase(c)){
u++;
}
else if(Character.isLowerCase(c)){
l++;

27
}
else if(Character.isDigit(c)){
d++;
}
else{
sp++;
}
}
System.out.println("**************************");
System.out.println("UPPERCASE: "+"\t"+u);
System.out.println("LOWERCASE: "+"\t"+l);
System.out.println("DIGITS: "+"\t"+d);
System.out.println("SPECIAL CHARACTER: "+"\t"+sp);
}
}

Output:-

28
Ques 11. Write a program to print all the palindrome words in the given sentence:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Declare string variables to store string
5. Taking sting as Input
6. Converting sentence to array of string
7. Logic to find which word is palindrome
8. Printing the words which are palindrome
9. Stop

// program to print all the palindrome words in the given sentence


import java.util.*;
class PalindromeWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s;
System.out.println("Enter the sentence");
s=sc.nextLine();
String[] words = s.split(" ");
System.out.println("Palindrome words in the sentence:");
for (int i = 0; i < words.length; i++) {
String word = words[i];
boolean isPalindrome = true;
for (int j = 0; j < word.length() / 2; j++) {
if (word.charAt(j) != word.charAt(word.length() - 1 - j)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
System.out.println(word);
29
}
}
}
}

Output:-

30
Ques 12. Write a program to check entered number is prime or not:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Taking the number as input
5. Checking if the number is prime or not
6. Displaying the result
7. Stop

//program to check entered number is prime or not


import java.util.*;
class CheckPrimeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int k=1;
if (num < 2) {
k=0;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
k=0;
break;
}
}
}

if (k==1) {
System.out.println(num + " is a prime number.");
} else {

31
System.out.println(num + " is not a prime number.");
}
}
}

Output:-

32
Ques 13. Write a program to check the entered number is neon or not:-
(A neon number is a number where the sum of digits of square of the number is equal to the number.)

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Taking the number as input
5. Checking if the number is neon or not
6. Displaying the result accordingly
7. Stop

//program to check entered number is neon or not


import java.util.*;
class NeonNo
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number :");
int num = sc.nextInt();
int square = num * num;
int sum = 0;
while(square > 0){
sum += square % 10;
square = square / 10;
}
if(sum == num){
System.out.println(num + " is a Neon number.");
}
else{
System.out.println(num + " is not a Neon number.");
}
}

33
}

Output:-

34
Ques 14. Write a program to convert the entered Decimal number to its binary
equivalent:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Taking the number as input
5. Converting the number to its binary equivalent
6. Displaying the result
7. Stop

//program to convert a decimal number to binary


import java.util.Scanner;
class DecimalToBinary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int decimal = sc.nextInt();
String binary = "";
while (decimal > 0) {
int remainder = decimal % 2;
binary = remainder + binary;
decimal = decimal / 2;
}
System.out.println("Binary equivalent: " + binary);
}
}

35
Output:-

36
Ques 15. Write a program to calculate the factorial of a given number:-

Algorithm:-
1. Start
2. Declare class
3. Declare scanner class
4. Taking the number as input
5. Calculating the factorial
6. Displaying the result
7. Stop

//program to calculate the factorial of a given number


import java.util.*;
class Factorial{
int fact(int n){
if(n==0)
return 1;
else
return(n*fact(n-1));
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int k=sc.nextInt();
Factorial ob=new Factorial();
int result=ob.fact(k);
System.out.println("FACTORIAL: "+result);
}
}

37
Output:-

38
Bibliography

For making this project a success, I have taken help from:-


 www.google.com
 www.geeksforgeeks
 www.wikepidia.com
 APC Understanding Computer Science Class XII(textbook)

~Thank You

39

You might also like