Sanvi Comp Year File Adobe
Sanvi Comp Year File Adobe
SCIENCE YEAR
FILE
SESSION 2024-25
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();
}
}
Data table:
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
Member Functions:
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;
}
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;
}
}
}
Data table:
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;
}
}
Data table:
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];
}
Data table:
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:
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:
Output:
Q2. Write a program to extract the second last word of the string.
Algorithm:
1. Start
4. Extract the substring from the beginning of the sentence to the last space index - 1.
6. Extract the substring from the beginning of the extracted substring to the last space index.
8. Stop
Program:
import java.util.*;
str=ab.nextLine();
String g=str.substring(0,s);
System.out.println(str.substring(a+1,s));
Data table:
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
char ch;
ch = word.charAt(i);
else
System.out.println(newword);
System.out.println("Enter a word");
ob.encoding(word);
Data table:
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:
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:
Output: