0% found this document useful (0 votes)
20 views

Sanvi Comp Year File Adobe

Uploaded by

sanvismd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Sanvi Comp Year File Adobe

Uploaded by

sanvismd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

COMPUTER

SCIENCE YEAR
FILE
SESSION 2024-25

Internal examiner External examiner

Name: Sanvi Agarwal


Study centre: XII B
ARRAY PROGRAMS
Q1.Write a program to find out if a given matrix is sparse or not.
(Sparse matrix contains zero elements above a certain threshold. This threshold is given by
(n*m)/2, where m is the number of rows and n is the number of columns. Hence, if a matrix
contains more than nm/2 number of zeroes, it is a sparse matrix otherwise not.)

Algorithm
1. Start
2. Take the input for the array elements
3. Display the original array
4. Count the number of zeroes in the matrix
5. Check whether the matrix is a sparse matrix or not using the condition r*c/2
6. Display the array
7. Stop

Program:
import java.util.*;
public class sparse{
int co,r,c,a[][];
sparse(int rr,int cc){
r=rr;
c=cc;
a=new int[r][c];
}

void input(){
Scanner in=new Scanner(System.in);
System.out.println("Enter array elements");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
{ a[i]
[j]=in.nextInt();
}
}
}

void display(){
Scanner in=new Scanner(System.in);
System.out.println("The elements are:");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
{ System.out.print(a[i]
[j]);
}
System.out.println();
}
}

void checksparse()
{ for(int i=0;i<r;i+
+){
for(int j=0;j<c;j++){
if(a[i][j]==0)
{
co=co+1;
}
}
}
if(co>(r*c)/2){
System.out.println("The matrix is a sparse matrix");
}
else{
System.out.println("The matrix is not a sparse matrix");
}
}

void display1(){
Scanner in=new Scanner(System.in);
System.out.println("The elements are:");
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
{ System.out.print(a[i]
[j]);
}
System.out.println();
}
}

public static void main(){


Scanner in=new Scanner(System.in);
System.out.println("Enter the no of rows");
int r=in.nextInt();
System.out.println("Enter the no of coloumns");
int c=in.nextInt();
sparse ob=new sparse(r,c);
ob.input();
ob.display();
ob.checksparse();
ob.display1();
}
}

Data table:

Variable Data type Description

r Integer To store number of rows

c Integer To store number of columns

co Integer To store the count of number of


zeroes

a Integer Used as an array variable

Output:
Q2. You are given a sequence of N integers which are called as pseudo arithmetic sequences
(sequences that are in arithmetic progression)
Sequence of n integers: 2,5,6,8,9,12
We observe that 2+12=5+9=6+8=14
The sum of the above sequences can be calculated as 14*3=42
For a sequence containing an odd number of elements the rule is to double the middle element
A class pseudo arithmetic determines whether a given sequence is pseudo arithmetic sequence or
not.
The details of the class are given below
Class name: Pseudo arithmetic
Data members/instance variables

n: To store the size of the sequence


a[]=integer array to store the sequence of numbers
Ans,flag: to store the status
sum: to store the sum of the sequence of numbers
r:to store the sum of the two numbers

Member Functions:

Pseudoarithmetic(): Default constructor

Void accept(int nn): to assign nn to n and create an integer array.Fill in the elements of the array

Boolean check(): returns true if the sequence is a pseudo arithmetic sequence otherwise returns
false

Specify the class psudoarithmetic giving the details of the constructor(),void accept(int) and boolean
check(). Also define a main function to create an object and call the member function accordingly
to enable the task

Algorithm
1. Start
2. Take input for the number of elements in the array(n)
3. Take numbers into the array and calculate the sum of all elements and add the middle
element twice if n is odd.
4. Calculate sum of first and last element
5. Run a loop to calculate the sum of second and second last digit, third and third last digit and so on
and check if it is equal to the sum of first and last element
6. Calculate number of pairs and add 1 if n is odd
7. Check if total sum of elements is equal to sum of first and last element*no. of pairs
8. Print yes if true, else false.
9. Stop
Program:
import java.util.*;
public class pseudoarithmetic{
int n,sum,r,a[];
boolean flag,ans;
Scanner in=new Scanner(System.in);
pseudoarithmetic(){
ans=false;
flag=false;
sum=0;
r=0;
}

void accept(int nn){


n=nn;
a=new int[n];
System.out.println("Enter the numbers for the array");
for(int i=0;i<n;i++){
a[i]=in.nextInt();
}
}

boolean check(){
int l;
int c;
r=a[0]+a[n-1]
;
l=n-1;
int p=n/2;
for(int i=0;i<n;i++){
sum=sum+a[i];
}

for(int i=1;i<=n/2;i++){

if(r==a[i]+a[l-1])
ans=true;
else
ans=false;
l--;
}

if(n%2!=0)
{
sum=sum+a[n/2];
p=p+1;

}
if(ans=true &&
r*p==sum) flag=true;
else
flag=false;

return flag;
}

public static void main(){


Scanner in=new Scanner(System.in);
pseudoarithmetic ob=new pseudoarithmetic();
System.out.println("Enter the value of n");
int k=in.nextInt();
ob.accept(k);
if( ob.check()==true)
System.out.println("It is pseudo arithmetic");
else
System.out.println("It is not pseudo arithmetic");

}
}

Data table:

Variable Data type Description

n Integer To store the size of the sequence

a[] Integer integer array to store the


sequence of numbers

Ans,flag Integer to store the status

sum Integer to store the sum of the sequence


of numbers

r Integer to store the sum of the two


numbers

p Integer To find number of pairs


l Integer To store the n-1th position

Output:
Q3. Write a program in Java to store a two dimensional array, and find the sum of the columnal
elements of the matrix

Algorithm
1. Start
2. Input an array from the user
3. Find the elements of the first column and find the sum.
4. Print the sum
5. Then, repeat the same for the other columns.
6. Stop

Program:
import java.util.*;
class column_sum
{
int a[][];
int n,m;
column_sum(int x, int y)
{
n=x;
m=y;
a=new int[n][m];
}

void input()
{
System.out.println("Enter the elements");
Scanner sc=new Scanner(System.in);
for(int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
a[i][j]=sc.nextInt();
}
}

void output()
{
for(int i=0;i<n;i++)
{
for (int j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
void columnsum()
{
int sum=0;
for(int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
sum=sum+a[j][i];
System.out.println("sum of "+ (i+1)+" column :"+sum);
sum=0;
}
}

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of rows");
int x=sc.nextInt();
System.out.println("Enter the no. of columns");
int y=sc.nextInt();
column_sum ob= new column_sum(x,y);
ob.input();
ob.output();
ob.columnsum();
}
}

Data table:

Variabl Data Type Description


e
Integer Used as a parameter to pass the number of array
x elements

Integer Used as a parameter to pass the number of array


y elements

Integer Used to store the array inputted by the user


a[]
Integer Used to store the length and breadth of array
n ,m
Output:

Q4.Write a program in Java to sort numbers using selection sort technique


algorithm:
1. Start
2. Input the array elements till n(number entered by user)
3. Repeat steps 3 -5 until n-1
4. Initialise the variable min with i (min=i) and if the a[j]<a[min] we store j value in min
5. Find the smallest number and interchange it with required position
6. Print the sorted array
7. Stop

Program:
import java.util.*;
public class selectionsort{
int a[];
int n,t;
Scanner in=new Scanner(System.in);
void input(){
System.out.println("Enter the array elements");
for(int i=0;i<n;i++){
a[i]=in.nextInt();
}
}
void selection(int nn){
n=nn;
a=new int[n];
}

void sort(){ int


min,p=0;
for(int i=0;i<n;i++){
min=i;
for(int j=i+1;j<n;j++){
if(a[j]<a[min]){
min=j;
}
}
t=a[i];
a[i]=a[min]
; a[min]=t;
}
}
void output(){
System.out.println(“THE SORTED ARRAY IS”);
for(int j=0;j<n;j++){
System.out.println(a[j]);
}
}
public static void main(){
Scanner in=new Scanner(System.in);
int kk;
System.out.println("Enter the value of n");
kk=in.nextInt();
selectionsort ob=new selectionsort();
ob.selection(kk);
ob.input();
ob.sort();
ob.output();
}
}

Data table:

Variabl Data type Description


e
Integer Used to store array elements
a
Integer Used to take number from the
n user

Integer Used as swapping variable


t
Integer To store the minimum value
min

Output:
Q5.Write a program in java to print the border elements of a matrix

Algorithm:
1: Start
2: Initialize a 2d array to store the matrix
3: Take input of the number of rows and columns from the user for the matrix.
4: Take the input from the user
5: Using nested loops, check if the element lies on the first row, column or last row, column.
6: If yes, print those as boundary elements.
7: Stop

Program
import java.util.*;
public class Boundary
{
int num[][];
int a,b;
Boundary(int x,int y)
{
a=x;
b=y;
num= new int[a][b];
}
void input()
{
Scanner ab=new Scanner(System.in);
System.out.println("Enter the elements ");
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
num[i][j]=ab.nextInt();
}
}
}
void output()
{
System.out.println("The elements of the matrix are: ");
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
System.out.print(num[i][j]+ " ");
}
System.out.println();
}
}
void check()
{
System.out.println("Boundary elements of the matrix: ");
for(int j=0;j<b;j++)
{
System.out.print(num[0][j]+ " ");
}
System.out.println();
for(int i=1;i<a-1;i++)
{
for(int j=0;j<b;j++)
{
if(j==0 || j==b-1)
{
System.out.print(num[i][j]+ " ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
for(int j=0;j<b;j++)
{
System.out.print(num[a-1][j]+ " ");
}
System.out.println();
}
public static void main()
{
Scanner ab=new Scanner(System.in);
System.out.print("Enter the number of rows");
int x=ab.nextInt();
System.out.print("Enter the number of columns");
int y=ab.nextInt();
Boundary obj=new Boundary(x,y);
obj.input();
obj.output();
obj.check();
}}
Data table:

Variabl Data type Description


e
int Used to store number of rows
a
int Used to store a number of
b columns.

Output:
STRING PROGRAMS
Q1. Write a program to input a sentence and create a new sentence by replacing each consonant
with the previous letter.If the previous letter is a vowel then replace it with the next letter and the
other characters must remain the same

Algorithm:
1. Start
2. Take the input for the sentence
3. Run a loop until length and check the characters one by one if it is a vowel add one and
if its consonant then subtract one letter
4. We find the letter using ASCII
5. Print a\new
sentence
6. Stop

Program
import java.util.Scanner;

class consonants
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
String s=sc.nextLine();
int n=s.length();
String f="";
int x;
char y;
for(int i=0; i<n; i++)
{
int ch=s.charAt(i);
x=(int)ch;
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
x=x+1;
else
x=x-
1;
y=(char)x;
f=f+y;
}
System.out.println(f);
}
}
Data table:

Variabl Data type Description


e
string To store the string
s
Integer To find the length
n
Integer Used as a converting variable
x
Character To extract individual characters
y
string To store a new string
f

Output:

Q2. Write a program to extract the second last word of the string.

Algorithm:

1. Start

2. Take input for the sentence.

3. Find the index of the last space in the sentence.

4. Extract the substring from the beginning of the sentence to the last space index - 1.

5. Find the index of the last space in the extracted substring.

6. Extract the substring from the beginning of the extracted substring to the last space index.

7. Print the extracted substring.

8. Stop

Program:

import java.util.*;

public class sec

public static void main()


{

Scanner ab=new Scanner(System.in);

String str=" ";

System.out.println("Enter a sentence: ");

str=ab.nextLine();

int s=str.lastIndexOf(" ");

String g=str.substring(0,s);

int a=g.lastIndexOf(" ");

System.out.println(str.substring(a+1,s));

Data table:

Variabl Data type Description


e
Integer To store the last index of the
s string.

Integer To store the space


a
string To store the string
str
string To store the substring
g

Output:
Q3. Write a program in Java to encode a string where every vowel is encoded to the letter 2 after
the vowel and consonant to the letter 1 after the consonant.

Algorithm:

1. Start
2. Store a word entered by the user
3. Check every letter of the word
4. If the letter is a vowel, type the letter that is two after the vowel
5. If the letter is a consonant, type the letter that is one after the consonant.
6. Print the new word
7. Stop

Program:

import java.util.*;

class encode

void encoding(String word)

String newword = "";

char ch;

for(int i = 0; i<word.length(); i++)

ch = word.charAt(i);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

int cha = (int)(word.charAt(i))+2;

newword = newword + ((char)(cha));

else

int cha = (int)(word.charAt(i))+1;


newword = newword + ((char)(cha));

System.out.println(newword);

public static void main()

Scanner sc = new Scanner(System.in);

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

String word = sc.next();

encode ob = new encode();

ob.encoding(word);

Data table:

Variable Data type Description

word String Used to store the word entered


by the user

newword String Used to store the new word after


encoding

ch Character Used to check every character of


the word

i Integer Loop variable

Output:
Q4. Write a program to reverse the string.

Algorithm:
1. Start
2. Create a variable to store the reversed string.
3. Initialize a loop that iterates through each character of the original string in reverse order.
4. Inside the loop, append each character to the variable created in step 1.
5. Continue the loop until all characters in the original string have been processed.
6. The variable now contains the reversed
string. 7.Stop

Program:
import java.util.*;
public class Word
{
public static void main()
{
Scanner ab= new Scanner(System.in);
System.out.println("Enter a sentence: ");
String s=" "+ab.nextLine()+" ";
int sp1=s.lastIndexOf(" ");
int sp2=0;
String st=" ";
for(int i=s.length()-2;i>=0;i--)
{
char c=s.charAt(i);
if(Character.isWhitespace(c)
)
{ sp2=i
;
String sub=s.substring(sp2,sp1);
st=st+sub+" ";
sp1=sp2;
}
}
System.out.print(st.trim());
}
}

Data table:

Variabl Data type Description


e
String To store the string.
s
Integer To store the last index of space
sp1
Integer Used for reversing
sp2
String Used for the new string.
st
Output:

Q5. Write a program in Java to input a sentence. Change the first letter of each word into upper
case and display the sentence.

Algorithm:
1: Start
2: Take input for the sentence.
3: Initialize an empty string for the modified sentence.
4: Iterate through each character in the sentence.
5: Check if the current character is the first character of the sentence or if the previous character is a
space.
6: If true, change the uppercase version of the current character to the modified sentence.
7: Else, change the current character to the modified sentence.
8: Print the modified sentence.
9: Stop

Program:
import java.util.Scanner;
public class First
{
public static void main()
{
Scanner ab = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String str = ab.nextLine();
String w= "";
for (int i = 0; i < str.length(); i++)
{
if (i == 0 || str.charAt(i - 1) == ' ') {
w=w+ Character.toUpperCase(str.charAt(i));
}
else {
w=w+ str.charAt(i);
}
}
System.out.println(w);
}
}
Data table:

Variabl Data type Description


e
String To store the sentence
str user inputs.

String Used to print the answer.


w

Output:

You might also like