Professional Documents
Culture Documents
Bouncy Number
A bouncy number is any non-negative integer that is neither increasing nor decreasing.
An increasing integer is any integer in which when going from left to right, the current
digit is greater than or equal to the previous digit.
In contrast, a decreasing integer is any integer in which when going from left to right,
the current digit is less than or equal to the previous digit.
A bouncy number’s digits neither increase nor decrease. Rather, they “bounce” between
increasing and decreasing.
Write a program to accept a positive number and check whether it is a Bouncy number
or not.
Algorithm:
Step 1: Start
Step 4: Check the digits are in increasing order or not. If false then go to step 5 else go to
step 6
Step 6: Check if the digits are in decreasing order or not. If false then go to step 7 else go
to 8
Step 7: Change the value of f1=1.
Step 8: Check f=1 and f1=1. If true then go to step 9 else go to step 10
Step 11:Stop
Program:
import java.util.*;
class BouncyNumber
a=a/10; c++;
int f=0,inc=0,d;
a=n;
for(int j=c;j>=1;j--)
{
if(d>=inc)
inc=d; }
else
f=1; break;
a=a%(int)Math.pow(10,j-1);
int dec=9;
a=n;
for(int j=c;j>=1;j--)
if(d<=dec)
dec=d;
} else {
f1=1; break;
}
a=a%(int)Math.pow(10,j-1);
if(f==1&&f1==1)
else
}
Assignment 17:
Calculating Denomination and number of notes
Design a program to accept the amount from the user and display the break – up in
descending order of denomination along with the total number of notes. The available
denominations are 2000, 500, 200, 100, 50, 20, 10. Also print the amount in words
according to the digit.
Example:
Enter Amount:
3450
THREE FOUR FIVE ZERO
Denominations:
2000 x 1 = 2000
500 x 2 = 1000
200 x 2 = 400
50 x 1 = 50
Total Number of Notes : 6
Algorithm:
Step 1: Start
Step 2: Accept an amount amt
Step 3: Check amt is in the given denomination. If true then go to step 4 else go to step
11
Step 4: Create an integer array and store the denominations.
Step 5: Create a string array and store the digits in words.
Step 6: Extract each digit from the left and concatenate the digits in words in a string w.
Step 7: Print the concatenated word w.
Step 8: Divide the amount with each denomination and add the quotient into the
variable total.
Step 9: Print the denominations.
Step 10: Print total number of notes, total
Step 11: End
Program:
import java.util.*;
class Money
System.out.println("Enter Amount:");
if(amt%10!=0)
System.out.println("Invalid Amount.");
else
int d[]={2000,500,200,100,50,20,10};
String
wrd[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT",
"NINE"};
String w="";
int total=0,a=amt,r=0;
for(int i=l;i>0;i--)
r=a/(int)Math.pow(10,i-1);
w=w+wrd[r]+" ";
a=a%(int)Math.pow(10,i-1);
System.out.println(w);
System.out.println("Denomination:");
a=amt;
for(int i=0;i<d.length;i++)
r=a/d[i];
if(r>0)
total=total+r;
a=a%d[i];
}
}
OUTPUT:
Assignment 18:
Arranging words containing vowel in beginning
(b) Create a new string with the words that begins and end with vowels followed by
other words.
(c) Display the new sentence along with the original sentence.
Example 1:
ORIGNAL SENTENCE:
REARRANGED SENTENCE:
Example 2:
Invalid String.
Algorithm:
Step 1: Start
Step 2: Accept a sentence st.
Step 3: Convert it into uppercase.
Step 5: Check whether the string ends with ‘?’ or ‘!’ or ‘.’. If not then go to step 6 else
go to step 7
Step 6: Print “Invalid String”. Go to step 18
Step 10: Check whether the first and last character of the wrd is a vowel or
Step 11: Concatenate vow, wrd and a space and go to step 13.
Program:
import java.util.*;
class Vowels
{
System.out.println("Enter a sentence:");
int l=st.length();
System.out.println("Invalid String.");
else
int wl=0;
for(int i=0;i<l;i++)
ch=st.charAt(i);
wrd=wrd+ch;
else
wl=wrd.length();
if((ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
&&(ch2=='A'||ch2=='E'||ch2=='I'||ch2=='O'||ch2=='U'))
vow=vow+wrd+" "; // joins the word begin and end with vowels
else
wrd="";
nst=vow+cons;
System.out.println(st);
System.out.println(nst);
}
Variable Description
Output:
Assignment 19:
Displaying Date
Design a program to accept a day number (between 1 and 366), year(in 4 digits) from
the user to generate and display the corresponding date. Display an error message if the
value of the day number and year are not within the limit or not according to the
condition specified.
Test your program with the following data and some random data:
INPUT:
YEAR: 2018
Algorithm:
Step 1: Start
Step 2: Accept number of days d.
Step 3: Accept year y.
Step 4: Check d>=1 and d<=366 and y>=1000 and y<= 10000. If true go to step
5 else go to step 14
Step 5: Create and initialize an array, mdays[] with number of days in each
month.
Step 6: Create and initialize an array, months [] with the month names.
Step 7: Check y is a Leap year or not. If true go to step 8.
Step 8: Increase the value of mdays[2] by 1. Go to step 9
Step 9: Declare and initialize i=1
Step 10: Repeat the steps11 to12 until d<= mdays[i]
Step 11: Reduce d=d-mdays[i]
Step 12: Update i =i+1
Step 13: Print d, mdays[i], y. Go to step 15
Step 14: Print “Invalid Date”
Step 15: Stop
Program:
import java.util.*;
System.out.println("DAY NUMBER:");
int d=sc.nextInt();
System.out.println("ENTER YEAR");
int y=sc.nextInt();
if((d>=1&&d<=366)&&(y>=1000&&y<=9999))
int mdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
"APRIL","MAY","JUNE","JULY",
"AUGUST","SEPTEMBER",
"OCTOBER","NOVEMBER","DECEMBER"};
int i=1;
while(d>mdays[i])
i++;
else
System.out.println("Invalid date");
}
Variable Description
Output:
Assignment 20:
Rearranging Matrix
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.
1. Sort the elements of the single-dimensional array in ascending order using any
standard sorting technique and display the sorted elements.
2. Fill the square matrix b[][] in the given format:
1 2 5 8
1 2 5 1
1 21 2
11 2 5
Algorithm:
Program:
import java.util.*;
int N=sc.nextInt();
if(N<3 || N>9)
System.out.println("Out of range");
else
for(int i=0;i<N;i++)
System.out.println("Enter a number:");
B[i]= sc.nextInt();
//sorting
int t=0;
for(int i=0;i<N;i++)
for(int j=0;j<N-1-i;j++)
if(B[j+1]<B[j])
t=B[j];
B[j]=B[j+1];
B[j+1]= t;
}
//printing the sorted array
for(int i=0;i<N;i++)
System.out.print(B[i]+" ");
for(int i=0,n=N-1;i<N;i++,n--)
A[i][j]=B[j];
A[i][k]=B[p];
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
System.out.println();
Variable Description:
Sample Input:
8 15 9 18
9 10 7 6
10 8 11 13
12 16 17 19
Sample Output:
18 9 15 8
6 7 10 9
13 11 8 10
19 17 16 12
Algorithm:
Step 1: Start
Step 3: Check whether M>2, M<=10, N>2 and N<=10. If true then go to step 4 else go to
step 11.
Program:
import java.util.*;
class Mirror
int M=sc.nextInt();
int N=sc.nextInt();
if(M<=2||N<=2||M>10||N>10)
System.out.println("OUT OF RANGE");
else
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.println("Enter a number:");
A[i][j]=sc.nextInt();
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
int t=0;
for(int c=0;c<N;c++)
{
for(int i=0;i<M;i++)
for(int j=N-1;j>c;j--)
t=A[i][j];
A[i][j]=A[i][j-1];
A[i][j-1]=t;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
Variable Description:
Output:
Assignment 22:
Smith Number
A Smith number is a composite number, whose sum of the digits is equal to the sum of
its prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.
Write a program in Java to enter a number and check whether it is a Smith number or
not.
Algorithm:
Step 1: Start
Step 3: Check whether it not a composite number . If true then go to step 4 else go to
step 5.
Program:
import java.util.*;
int n;
System.out.println("Enter number:");
n = sc.nextInt();
int sumDigits(int x)
int s=0;
while(x>0)
s=s+x%10;
x=x/10;
return s;
boolean isPrime(int a)
{
int c=0;
for(int i=1;i<=a;i++)
if(a%i==0)
c++;
if (c==2)
return true;
else
return false;
void check()
int f=0;
for(int i=2;i<n;i++)
if(n%i==0)
f=1;
break;
}
}
if(f==0)
else
int dsum=sumDigits(n);
int sp=0,j=2;
int a=n;
while(a>1)
if(a%j==0&&isPrime(j)==true)
System.out.print(j+" ");
sp=sp+sumDigits(j);
a=a/j;
continue;
j++;
System.out.println("\nSum of digits:"+sp);
if(dsum==sp)
System.out.println(n+" is a Smith Number.");
else
obj.accept();
obj.check();
Variable Description:
Assignment 23:
Consecutive Letters
Write a program to accept a string and display the words along with the frequency of the
words which have at least a pair of consecutive letters.
Sample Input:
Sample Output:
Algorithm:
Step 1: Start
Step 7: Check whether wrd contains consecutive pair or not. If true then go to step 8 else
go
to step 10.
import java.util.*;
class Cons
System.out.println("Enter a word:");
String st=sc.nextLine();
st=st.toUpperCase();
st=st+" ";
System.out.println(st);
int l=st.length();
int wordcount=0;
String wrd="";
for(int i=0;i<l;i++)
ch=st.charAt(i);
if(ch!=' ')
{
wrd=wrd+ch;
else
ch1=wrd.charAt(j);
ch2=wrd.charAt(j+1);
if((int)ch2-(int)ch1==1)
System.out.println(wrd);
wordcount++;
break;
wrd="";
}
Variable Description:
Output:
Assignment 24:
Largest and Smallest element in each row
Write a program to create a matrix A[][] of order (MxN) and store the numbers in it.
(Both M and N should be greater than 2 and less than 10).Find the largest and smallest
element in each row.
Sample Input:
1 2 3
4 5 6
7 8 9
Sample Output:
1 3 1
2 6 4
3 9 7
Algorithm:
Step 1: Start
Step 4: Check whether M<3, M>9, N<3 or N>9. If true then go to step 5 else go to step
6.
Step 12: Check whether A[i][j]>max. If true then go to step13 else go to step 14.
Step 14: Check whether A[i][j]<min. If true then go to step15 else go to step 15.
Program:
import java.util.*;
class MinMax
if(M<3||N<3||M>9||N>9)
System.out.println("OUT OF RANGE");
else
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
A[i][j]=sc.nextInt();
System.out.println("Matrix is:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
int min, max;
System.out.println("Row\tMaximum\tMinimum");
for(int i=0;i<M;i++)
min=A[i][0];
max=A[i][0];
for(int j=1;j<N;j++)
if(A[i][j]>max)
max=A[i][j];
if(A[i][j]<max)
min=A[i][j];
System.out.println((i+1)+"\t"+max+"\t"+min);
}
Variable Description:
OUTPUT:
Assignment 25:
Sorting the boundary elements
Write a program to create a matrix A[][] of order (MxN) and store the numbers in it.
(Both M and N should be greater than 2 and less than 10). Sort the boundary elements
of the matrix. Display the sorted matrix.
Sample Input:
11 8 34
45 21 1
9 18 74
Sample Output:
1 8 9
74 21 11
45 34 18
Algorithm:
Step 1: Start
Step 4: Check whether M<3, M>9, N<3 or N>9. If true then go to step 5 else go to step
6.
Step 12: Store the sorted border elements in the matrix A[][].
Program:
import java.util.*;
class SortOuter
int M=sc.nextInt();
int N=sc.nextInt();
if(M<=2||N<=2||M>10||N>10)
System.out.println("OUT OF RANGE");
else
{
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.println("Enter a number:");
A[i][j]=sc.nextInt();
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
int size=2*(M+N-2);
int k=0;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
if(i==0||j==0||i==M-1||j==N-1)
B[k++]=A[i][j];
//sorting
int t=0;
for(int i=0;i<size;i++)
for(int j=0;j<size-1-i;j++)
if(B[j+1]<B[j])
t=B[j];
B[j]=B[j+1];
B[j+1]=t;
}
k=0;
A[0][j]=B[k++];
A[i][N-1]=B[k++];
A[M-1][j]=B[k++];
A[i][0]=B[k++];
System.out.println("SORTED MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
Variable Description:
Output: