(Q1.
) Pascal’s triangle is a triangular array constructed
by assuming adjacent elements in preceding rows. It
contains the values of binomial coefficient. Every row
begins with ‘1’ and ends with ‘1’; any other element of
arrow is the sum of adjacent elements of the preceding
row. Write a program in Java to compute Pascal’s
triangle of ‘n’ rows where ‘n’ is a natural number
taken as input.
OVERVIEW.
Step1: Start
Step2: Class starts.
Step3: main Method starts.
Step4: The variables are initialized.
Step5: The numbers of rows are taken as input from
the user.
Step6: A loop starts from 0 to ‘n’ for controlling and
printing the elements till ‘n’ number of rows.
Step7: A loop starts for printing the spaces to the left
and right of the triangle.
Step8: The space is printed and num is initialized as 1.
Step9: A loop starts for printing elements of triangle.
Step10: Class ends.
Step11: Stop
import java.util.*;
public class Pascal_Triangle{
public static void main(String args[]){
int num=1, n, s, i, j;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of rows of Pascal's Triangle: ");
n = sc.nextInt();
for(i=0;i<n;i++){
for(s=n;s>i;s--){
System.out.print(" "); num=1;
}
for(j=0;j<=i;j++){
System.out.print(num+" ");
num = num*(i-j)/(j+1);
}
System.out.println();
}
}
}
NAME TYPE FUNCTION
n int
INPUT To accept the no of
rows from the user.
PROCESSING num int To store the
number as 1.
i int To run a loop for
controlling and also
printing the elements
till 'n' no. of rows.
s int To run a loop for
printing the spaces to
the left and right of
the triangle.
j int To run a loop for
printing the numbers
of the triangle.
OUTPUT num int To print the numbers.
(Q2.) Write a program in java to convert a
decimal number into binary using recursion.
OVERVIEW.
Step1: Start
Step2: Class starts
Step3: main Method starts.
Step4: A decimal number is taken as input from the
user as per choice.
Step5: The binary equivalent of the number is printed
by calling from decimal to binary method.
Step6: A recursive method decimal to binary is created.
Step6.1: If n is equal to zero then it returns zero else it
returns the binary equivalent of the number.
Step6.2: The recursive method ends.
Step 7: main Method ends.
Step8: Class ends.
Step 9: Stop
import java.util.*;
public class p2{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int d ;
System.out.println("Enter a decimal
number:");
d = sc.nextInt();
System.out.println("Binary equivalent is
:"+dectobi(d));
}
static int dectobi(int n)
{
if(n==0)
return 0;
else
return (n%2+ 10*dectobi(n/2));
}
}
NAME TYPE FUNCTION
INPUT d int To accept the
decimal number
from the user.
PROCESSING n int To store the binary
equivalent of the
number.
Output n int To print the binary
equivalent of the
number.
(Q3) Write a program to declare a single-dimensional array a[] and a
square matrix b[][] of size N, where N>2 and N<10. Allow the user to input
positive integers into the single dimensional array. Perform the following
tasks on the matrix: Sort the elements of the single-dimensional array in
ascending order using any standard sorting technique and display the
sorted elements.Fill the square matrix b[][] in the following format:
If the array a[]={5,2,8,1} then, after sorting a[]={1,2,5,8} then,
the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
Display the filled matrix in the above format. Test for random data.
OVERVIEW.
Step1: Start
Step2: Class starts
Step3: main Method starts
Step4: The size of the single dimensional array is taken as input
from the user.
Step5: If the size is lesser than or equal to 2 and greater than or
equal to 10, a message will be printed and it will exit the program.
Step6: Two arrays (one1dandanother2d) are initialized.
Step7: The elements of the single dimensional array are taken as
input from the user. Step8: The length of the single dimensional
array is stored in a variable.
Step9: A counter loop is started from 0 to l and an inner loop is
started from 0 to i- 1 to sort the numbers in ascending order.
Step10: The sorted array is then printed.
Step11: A counter loop is started from n-1 to 0 and an inner loop
is started from 0 to i to fill up the double dimensional array with
the numbers.
Step12: The double dimensional array is
then printed.
Step 13: main Method ends.
Step14: Class ends.
Step 15: Stop
import java.util.*;
public class Array{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("ENTER VALUE OF N: ");
int n = sc.nextInt();
if(n<=2||n>=10){
System.out.println("MATRIX SIZE OUT OF
RANGE"); return;
}
int a[] = new int[n];
int b[][] = new int[n][n];
System.out.println("ENTER ELEMENTS OF
SINGLE DIMENSIONAL ARRAY: ");
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
int l=a.length;
for(int i=0;i<l-1;i++){
for(int j=0;j<l-i-1;j++){
if (a[j] > a[j + 1]) {
int t = a[j];
a[j]=a[j+1];
a[j+1] = t;
}
}
}
System.out.println("SORTED ARRAY: ");
for (int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
for(int i=n-1,r=0;i>=0;i--,r++){
for(int j=0;j<=i; j++) {
b[r][j]=a[j];
}
for(int k=n-1;k>i;k--){
b[r][k]=a[k-i-1];
}
}
System.out.println();
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]+" ");
}
System.out.println();
}
}
}
NAME TYPE FUNCTION
INPUT n int Toacceptthesize of
array from the
user.
Processing a[] int To store the
number in 1-darray.
b[][] int To store the
numbers in 2d
array.
l int To store the length
of array.
i int To run a loop from
0 to i-1
j int To run a loop from
0 to l-i-1
t int To store
temporarily the
swapped number.
k int To run a loop
from n-1 to it to
store the numbers.
Output b[][] int To print the
numbersofthe2d
array.
(Q4) Write a program to accept a sentence which may be
terminated by either ".", '?' or '!' only. The words are to be
separated by a single blank space and are in uppercase. Perform
the following tasks:
(a) Check for the validity of the accepted sentence.
(b) Convert the non-Palindromic words of the sentence into
Palindromic words by concatenating the word by its reverse
(excluding the last character).
OVERVIEW.
Step1: Start
Step2: Class starts.
Step3: main Method starts.
Step3.1: A sentence is taken as input from the user.
Step3.2: The method validity is called and it stores the substring till it is terminated by
a space.
Step3.3: A loop is run to store the word and then it is checked by calling the method
palindrome. If the word is not equal to w after reversing then method reverse is
calledandthewordsareconcatenatedorelsethewordisaddedwiththestringand a space.
Step3.4:ThetransformedsentenceisprintedfollowingfromStep3.2orelseitis terminated
and goes to Step 7 by printing an appropriate message.
Step 4: Static method boolean validity starts.
Step4.1: The character is stored from the string.
Step4.2: If the character stored is equal to‘.’, ‘?’ and ‘!’ then sp is initialized as 0.
Step4.3: A loop is run to check if the characters are letters and in uppercase and the
loop continues to another checking and if it is equal to a space, it returns false.
Step4.4: The boolean value is returned.
Step4.5: Static method boolean validity ends.
Step5: Static method String palindrome starts.
Step5.1:Thestringnsisinitializedandaloopisrunfrom0tolengthofthestringto convert the
word into palindrome.
Step5.2: The Palindromic word is then returned.
Step5.3: Static method String palindrome ends.
Step 6: Static method String reverse starts.
Step6.1: The character is stored in c and the index of the character is stored in i.
Step 6.2: The substring of the word is stored and added with the new string.
Step6.3: The new string is returned.
Step6.4: Static method String reverse ends.
Step 7: Stop
import java.util.*;
public class PalindromeStr{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String s=in.nextLine();
if(validity(s)){
s=s.substring(0,s.length()-1)+" ";
int i,l=s.length();
String w="",ns="";
for(i=0;i<l;i++){
if(s.charAt(i)!=' ')
w=w+s.charAt(i);
else{
if(palindrome(w)!=w)
ns=ns+reverse(w)+" ";
else
ns=ns+w+"";
w="";
}
}
System.out.println("Transformed sentence is:
"+ns);
}
else
System.out.println("Invalid input, ALL THE
CHARACTER SHOULD BE IN UPPERCASE AND
SENTENCE SHOULD BE TERMINATED BY
EITHER . OR ? OR !");
}
public static boolean validity(String s){
char c=s.charAt(s.length()-1);
if(c=='.'||c=='?'||c=='!'){
int i,sp=0;
for(i=0;i<s.length()-1;i++){
c=s.charAt(i);
if(Character.isLetter(c) &&
Character.isUpperCase(c))
continue;
else if(c==' '){
if(s.charAt(i+1)==' ')
return false;
}
else
return false;
}
return true;
}
return false;
}
public static String palindrome(String s){
int i;
String ns="";
for(i=0;i<s.length();i++)
ns=s.charAt(i)+ns;
return ns;
}
public static String reverse(String s)
{
char c=s.charAt(s.length()-1); int i=s.indexOf(c);
String ns=s.substring(0,i); s=s+palindrome(ns);
return s;
}
}
NAME TYPE FUNCTION
INPUT s String To accept the
sentencefromthe
user.
PROCESSING l int To store the length of the
string.
s char To store the sub string of
the word.
i int To run a loop from 0 to l.
w String To store the new words.
ns
String To store the new sentence.
c
char To store the character of
the string.
s String To store the Palindromic
word.
OUTPUT ns String To print the transformed
sentence.
(Q5.) Write a program to declare a square matrix of size m where m must
be greater than 3 and lesser than 10. Allow the user to input positive
integers into the matrix and perform the following tasks:
i) Sort the non boundary elements in ascending order using any standard
sorting technique and rearrange them in the matrix.
ii) Calculate the sum of both the diagonals.
iii) Display the original matrix, the rearranged matrix and the diagonal
elements of the rearranged matrix along with their sum.
OVERVIEW.
Step1: Start
Step2: Class starts
Step3: main Method starts.
Step4: The size of the array is taken as input from the user.
Step5: If size is lesser than or equal to 3 or greater than or equal to 10, it
will exit the program displaying a message.
Step6: The array is initialized and the values of the array are taken as input
from the user.
Step7: The array is then printed.
Step8: The new array is created using the new size of u.
Step9: The elements of the new array are stored by the non boundary
elements of the previous array.
Step10: Another loop is started from 0 to c-1 and an inner loop is started
from 0 to c-j-1 to sort the numbers in ascending order.
Step11: The numbers sorted are again put back in the previous
array.
Step 12: The rearranged matrix is then printed.
Step13: A loop is run to find the sum of the diagonals and to print the
diagonal elements.
Step14: The sum of diagonals is printed.
Step15: main Method ends.
Step16: Class ends.
Step 17: Stop.
import java.util.*;
public class boun{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("ENTER THE VALUEOF M");
int m=sc.nextInt();
if((m<=3)||(m>=10)){
System.out.println("WRONG OUTPUT");
System.exit(0);
}
int A[][]=new int[m][m];
System.out.println("ENTER THE VALUES OF MATRIX ");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
A[i][j]=sc.nextInt();
}
}
System.out.println("THE ORIGINAL MATRIX IS");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
System.out.print(A[i][j]+"");
}
System.out.println();
}
int u=(int)(Math.pow(m,2));
int b[]=new int[u];
int c=0;
for(int i=1;i<m-1;i++){
for(int j=1;j<m-1;j++){
b[c]=A[i][j]; ++c;
}
}
int temp=0;
for(int j=0;j<c-1;j++){
for(int k=0;k<c-j-1;k++){
if(b[k]>b[k+1]){
temp= b[k];
b[k]=b[k+1];
b[k+1]=temp;
}
}
}
c=0;
for(int i=1;i<m-1;i++){
for(int j=1;j<m-1;j++){
A[i][j]=b[c]; ++c;
}
}
System.out.println("THE REARRANGED MATRIX IS");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
System.out.print(A[i][j]+" ");
}
System.out.println();
}
int sum=0;
System.out.println("THE DIAGONALS ARE");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if((i==j)||(i+j==m-1)){
System.out.print(A[i][j]+""); sum = sum + A[i][j] ;
}
else
System.out.print(" ");
}
System.out.println();
}
System.out.println("THESUMOFDIAGONALS"+sum);
}
}
NAME TYPE FUNCTION
INPUT m int To accept the size of the
array from
the user.
PROCESSING A[][] int To store the numbers of
the array.
U int To store the length or
size of 1–d array.
B[] To store the sorted
int
elements of the array.
i int To run a loop from 0 to
m.
j int To run a loop from 0 to
m.
s int To store the sum of the
diagonals.
OUTPUT A[][] int To print the rearranged
matrix and the diagonal
elements.
sum int To print the sum of the
diagonal elements.