Computer Project!!
Computer Project!!
‘M’ must be greater than 3 and less than 10. Allow the user to input positive
integers into this matrix. Perform the following tasks on the matrix:
1. Sort the non-boundary elements in ascending order using any standard
sorting technique and rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal
elements of the rearranged matrix with their sum.
Test your program with the sample data and some random data:
INPUT :M = 4
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
REARRANGED MATRIX
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
DIAGONAL ELEMENTS:
9 5
3 6
8 13
7 8
ALGORITHM:
Step 1: Start.
Step 2: Input M.
Step10: Stop.
PROGRAM:
import java.util.*;
class Q1
int M = sc.nextInt();
else
int i, j, c, t;
System.out.println("Enter " + (M*M) + " elements");
a[i][j] = sc.nextInt();
}//loop j
}//loop i
c = 0;
b[c++] = a[i][j];
}//loop j
System.out.println();
}//loop i
t = b[i];
b[i] = b[j];
b[j] = t;
}//loop j
}//loop i
c = 0;
a[i][j] = b[c++];
}//loop j
}//loop i
}//loop j
System.out.println();
}//loop i
else
System.out.print(" ");
}//loop j
System.out.println();
}//loop i
}//end of main
}//end of class
VARIABLE DECLARATION:
Test your program with the following data and some random data:
Example 1
INPUT:
N = 726
OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
Example 2
INPUT:
N = 140
OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6*1=6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6
Example 3: N= 4296
INVALID INPUT
ALGORITHM:
Step 1: Start.
Step 8:Stop.
PROGRAM:
import java.util.*;
public class Q2
{//class starts
int n = in.nextInt();
System.out.println("INVALID INPUT");
return;
int cartonSizes[] = {48, 24, 12, 6};//set the size of the cartoons
int total = 0;
int t = n;
t = t % cartonSizes[i];
total += cartonCount;
if (cartonCount != 0) {
/*
*/
if (t != 0)
total++;
else
}
System.out.println("Total number of boxes = " + n);
}//class end
OUTPUT:
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
q3) Write a program to declare a matrix a[][] of order (m × n) where 'm' is the
number of rows and 'n' is the number of columns such that the values of both
'm' and 'n' must be greater than 2 and less than 10. Allow the user to input
integers into this matrix. Perform the following tasks on the matrix:
Test your program for the following data and some random data:
Example 1
INPUT:
M=4
N=3
AFTER ENTERING MATRIX ELEMENTS
OUTPUT:
ORIGINAL MATRIX
11 -2 3
5 16 7
9 0 4
-2 3 11
5 7 16
0 4 9
1 3 8
Example 2
INPUT:
M=3
N=3
OUTPUT:
ORIGINAL MATRIX:
22 5 19
5 7 16
0 4 9
MATRIX AFTER SORTING ROWS
5 9 22
7 12 36
6 9 13
Example 3
INPUT:
M = 11
N=5
OUTPUT:
MATRIX SIZE OUT OF RANGE.
ALGORITHM:
Step 1: Start.
Step 8: Stop.
PROGRAM:
import java.util.*;
public class Q3
{//class starts
return;
a[i][j] = in.nextInt();
System.out.println();
int t = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = t;
}
}
System.out.println();
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
Test your program for the following data and some random data:
Example 1
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
1 3 7
1 3 1
1 1 3
Example 2
INPUT:
N = 13
OUTPUT:
MATRIX SIZE OUT OF RANGE
Example 3
INPUT:
N=5
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6
OUTPUT:
SORTED ARRAY: 2 5 6 10 23
FILLED MATRIX:
2 5 6 10 23
2 5 6 10 2
2 5 2 5 6
2 2 5 6 10
ALGORITHM:
Step 1: Start.
Step 9: Stop.
PROGRAM:
import java.util.*;
class Q4
{//class starts
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter");
int N=sc.nextInt();
if(N<2||N>10)//set the range
{
System.out.println("MATRIX SIZE OUT OF RANGE");
}
else
{
int a[]=new int[1000];
int b[][]=new int[100][100];
System.out.println("ENTER ELEMENT OF SINGLE DIMENSIONAL ARRAY:");
for(int i=0;i<N;i++)
{
a[i]=sc.nextInt();//input elements
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N-i-1;j++)//sort the array
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
///for printing sorted array....
System.out.println("SORTED ARRAY :");
for(int i=0;i<N;i++)
{
System.out.println(a[i]);
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(j<N-i)//fill the array
b[i][j]=a[j];
else
b[i][j]=a[j-(N-i)];
}
}
System.out.println("FILLED MATRIX");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(b[i][j]);//display the array
}
System.out.println();
}
}
}
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
Test your program for the following data and some random data:
Example 1:
INPUT:
M=1
N=3
ENTER ELEMENTS FOR ROW 1: 1 4
Example 2:
INPUT:
M=3
N=4
ENTER ELEMENTS FOR ROW 1: 1 1 3 7
ENTER ELEMENTS FOR ROW 2: 2 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT:
1 1 3 7 607
2 1 0 6 1094
0 2 4 5 165
Example 3:
INPUT:
M=3
N=3
ENTER ELEMENTS FOR ROW 1: 2 4 8
OUTPUT:
INVALID INPUT
Example 4:
INPUT:
M=4
N=6
OUTPUT:
OUT OF RANGE
ALGORITHM:
Step 1: Start.
Step 9: Stop
PROGRAM:
import java.util.*;
public class Q5
{//class starts
if (m <= 0 || m >= 10 || n <= 2 || n >= 6)//Set the range for rows and
columns
System.out.println("OUT OF RANGE");
return;
System.out.println("INVALID INPUT");
return;
}
int decNum = 0;
System.out.print("\t\t" + decNum);
System.out.println();
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
(a) Accept the sentence and reduce all the extra blank space between two
words to
a single blank space.
(b) Accept a word from the user which is part of the sentence along with its
position number and delete the word and display the sentence.
Test your program with the sample data and some random data:
Example 1
INPUT:
WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6
Example 2
Example 3
INPUT:
OUTPUT:
INVALID INPUT.
ALGORITHM:
Step 1: Start.
Step 9: Stop.
PROGRAM:
import java.util.*;
public class Q6
{//class starts
int i=0,count=1,len=0,pos=0;
String sen="",newsen="",wd="",wd1="";
System.out.println("INVALID INPUT.");
return;
sen = sen.toUpperCase();
for(i=0;i< len;i++)
ch=sen.charAt(i);
if(ch==' '||ch=='.'||ch=='?'||ch=='!')
/*do nothing*/
else
wd1="";
count++;
else
wd1=wd1+ch;
}
}
System.out.println("NEW SENTENCE:"+newsen);
}//class end
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
Example 1:
OUTPUT:
Intelligence Plus Character Is Education
Example 2
OUTPUT:
God is Great
Example 3
INPUT: All the best!
ALGORITHM:
Step 1: Start.
PROGRAM:
import java.util.*;
class Q7
{//class starts
public static void main(String args[])
{
String p="";
int v=0,c=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String n=sc.nextLine();//Enter the string
n=n+" ";
System.out.println("Word \t vowel \tconsonant");
int l=n.length();
if(n.charAt(l-2) != '.' && n.charAt(l-2) != '?')
{
System.out.println("Invalid Input. End a sentence with either '.' or '?'");
}
else
{
for(int i=0;i<l;i++)
{
char ch=n.charAt(i);
if(ch!=' ')
p=p+ch;
else
{
int k=p.length();
for(int j=0;j<k;j++)
{
char ch1=p.charAt(j);
if(ch1=='.')
break;
if(ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||ch1=='u')//check the vowels
{
v++;//count the vowels
}
else
c++;//count the consonant
}
System.out.println(p+"\t"+v+"\t"+c);//print the output
p="";
v=0;
c=0;
}
}
}
}
}//class ends
OUTPUT:
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
Test your program with the sample data and some random data:
Example 1
INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Example 2
INPUT:
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE
TODAY.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
Example 3
INPUT:
LOOK BEFORE YOU LEAP.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4
INPUT:
HOW ARE YOU@
OUTPUT:
INVALID INPUT
ALGORITHM:
Step 1: Start.
Step 11:Stop.
PROGRAM:
import java.util.*;
public class Q8
{//class starts
int n,i,t;
Char ch;
System.out.println("INVALID INPUT");
return;
else
n=s.countToken( );
t=0;
for(int i=0;i<n;i++)
s1=s.nextToken();
last=s1.charAt(0);
ch=s1.charAt(s1.length( )-1);
if(“AEIOU”.indexOf(last)>=0&&(“AEIOU”.indexOf(ch)>=0))//give the
conditions for first and last character of each word
s2=s2+’ ‘+s1;
else
s3=s3+’ ‘+s1;
s4=s2+’ ‘+s3;
System.out.println(s4);
}
}
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
(‘rotate by 13 places’).
remaining unchanged.
A/a B/b C/c D/d E/e F/f G/g H/h I/i J/j K/k L/l M/m
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/n O/o P/p Q/q R/r S/s T/t U/u V/v W/ X/x Y/y Z/z
w
Test your program with the sample data and some random data:
Example 1:
Example 2:
Example 3:
INPUT: You
OUTPUT : INVALID LENGTH
ALGORITHM:
Step 1: Start
if((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))
if((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))
Step 9: Stop.
PROGRAM:
import java.util.*;
class Q9
System.out.println("INVALID LENGTH");
return;
String c = "";
char ch = s.charAt(i);
if((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))//give the condition
c += (char)(ch + 13);
else if((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))
c += (char)(ch - 13);
else
c += ch;
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
OUTPUT:
R M D K
o a e i
y r n
a s R g
l o s
s
e
Example 3
INPUT:
N = 10
OUTPUT:
INVALID INPUT
ALGORITHM:
Step 1: Start.
Step 9: Stop.
PROGRAM:
import java.util.*;
{//class starts
System.out.println("INVALID INPUT");
return;
int highLen = 0;
highLen = teams[i].length();
if (i >= len) {
else {
}
}
System.out.println();
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
The input contains two positive integers m and n where m>=100 and n<= 3000.
Display number of prime palindrome integers in the specified range along with
their values in the format specified below:
Test your program with the sample data and some random data:
Example 1:
INPUT:
M=100
N=1000
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Example 2:
INPUT:
M=100
N=5000
ALGORITHM:
Step 1: Start.
for(i=m;i<=n;i++)
Step 7: If the number is both prime as well as palindrome print the number
print the number.
PROGRAM:
import java.util.*;
class Q11
freq = 0;
for(i=m;i<=n;i++)
t = i;
c = 0;
if(t%j == 0)
c++;
if(c == 2)
r = 0;
while(t>0)
{
a = t%10;//getting remainder
r = r*10 + a;
t = t/10;
if(r == i){
System.out.print(i+" ");
freq++;
else
System.out.println("OUT OF RANGE");
}//end of main
}//end of class
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
Q12) An ISBN (International Standard Book Number) is a ten digit code which
uniquely identifies a book.
The first nine digits represent the Group, Publisher and Title of the book and
the last digit is used to check whether ISBN is correct or not.
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 ten; this is done by
writing the last digit of the code as X.
To verify an ISBN, calculate 10 times the first digit, plus 9 times the second
digit, plus 8 times the third and so on until we add 1 time the last digit. If the
final number leaves no remainder when divided by 11, the code is a valid ISBN.
For Example:
1. 0201103311 = 10*0 + 9*2 + 8*0 + 7*1 + 6*1 + 5*0 + 4*3 + 3*3 + 2*1 + 1*1 =
55
2. 007462542X = 10*0 + 9*0 + 8*7 + 7*4 + 6*6 + 5*2 + 4*5 + 3*4 + 2*2 + 1*10
= 176
Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.
3. 0112112425 = 10*0 + 9*1 + 8*1 + 7*2 + 6*1 + 5*1 + 4*1 + 3*4 + 2*2 + 1*5 =
71
Since 71 leaves no remainder when divided by 11, hence it is not a valid ISBN.
Design a program to accept a ten digit code from the user. For an invalid input,
display an appropriate message. Verify the code for its validity in the format
specified below:
Test your program with the sample data and some random data:
Example 1
INPUT CODE: 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE
Example 2
Example 3
ALGORITHM:
Step 1: Start
if(ch.equalsIgnoreCase("X"))
Step 9: Stop.
PROGRAM:
import java.util.*;
class Q12
{//class starts
int l=s.length();
else
String ch;
int a=0, sum=0, k=10;
ch=Character.toString(s.charAt(i));
a=10;
else
a=Integer.parseInt(ch);
k--;//counter decrease
else
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE
5. a int
Example:
ALGORITHM:
Step 1: Start.
if(ch==’?’||ch==’.’||ch==’!’)&&st.length<=20)
Step 9: Compare the string and set the flag variable accordingly.
PROGRAM:
import java.util.*;
class Q13
{//class starts
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,l,j;
System.out.println("Enter Sentence");
String str=sc.nextLine();//Enter the string
l=str.length();
char ch=str.charAt(l-1);
if(ch==’?’||ch==’.’||ch==’!’&&(st.length)<=20)
{
System.out.println(“Valid String”);
int f=0;
for(i=0;i<st.length;i++)//set i loop
for(j=0;j<st.length-1;j++)//set j loop
if(st[j].compareTo(st[j+1])<0)//compare word
f=0;
else
if(f==0)
{
System.out.println("Snowball String");//display the message
}
else
{
System.out.println("Not a Snowball String");
}
else
System.out.println(“Invalid String”);
}//class ends
VARIABLE DECLARATION:
SL.NO. NAME TYPE PURPOSE