0% found this document useful (0 votes)
9 views56 pages

Projectpgy (With Comment)

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

Projectpgy (With Comment)

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

Program 1: Write a program in blue j to accept a string in mixed case.

Arrange all the alphabets of the string in such a way that all the lower case
characters are followed by the upper case characters.

import java.util.Scanner;
class casesorter
{
void main()
{
Scanner sc=new Scanner(System.in); /*to input*/
String s,low="",up="";
char c;
int i,l;
System.out.println("Enter your string "); /* to enter string*/
s=sc.nextLine();
l=s.length(); /*to enter length*/
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(Character.isUpperCase(c))
up=up+c;
if(Character.isLowerCase(c))
low=low+c;
}
System.out.println(" the new sting formed is:" +"\t" + low+up);

}
}
OUTPUT 1:-

Enter a string
AAAaaa
aaaAAA
Enter a string
mySTUDent
myentSTUD
Enter a string
MyTEACher
yherMTEAC
Program 2: Write a program in bluej to accept a string,frame a word by taking
the first character of each word and check whether the new word is a palindrome or
not.

import java.util.Scanner;
class checkpalindrome
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
String s,t="",p="";
char c,d,e;
int i,l,a,b;
System.out.println("enter a string"); /* to enter string*/
s=Sc.nextLine();
s=" "+s;
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c==' ') , /*checking if c is equal to' '*/nh
{
d=s.charAt(i+1);
t=t+d;
}
a=t.length();
for(b=0;b<a;b++)
{
e=t.charAt(b);
p=e+p;
}
if(t.equalsIgnoreCase(p))
System.out.println("The new word such formed is palindrome");
else
System.out.println("The new word such formed is not palindrome");
}
}
OUTPUT 2:-

Enter a string
mad a mad a mad
it is a palindrome string
Enter a string
mad a mad a d
it is not a palindrome string
Enter a string
why u why
it is a palindrome string
Program 3 : Write a program in bluej to accept a string, which includes some special
characters like punctuation mark viz. apostrophe(), full stop(.).comma(,),
semicolon(:) etc. and display the string in reversed order without using punctuation
mark.

import java.util.Scanner;
class specialscharacters
{
void main()
{
Scanner sc=new Scanner(System.in); /*to input*/
String s,str="",t="",newst="";
char c,d;
int i,l,k;
System.out.println(" Enter your string"); /* to enter string*/
s=sc.nextLine();
l=s.length(); /* to obtain length*/
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(Character.isLetter(c) || (Character.isWhitespace(c)))
str=str+c;
}
str=str+' ';
k=str.length();
for(i=0;i<k;i++)
{
d=str.charAt(i);
if(Character.isLetter(d))
t=t+d;
if(Character.isWhitespace(d))
{
newst= " "+t+" "+newst+" ";
t="";
}
}
System.out.println( " the reversed new string is : " + newst);
}
}
OUTPUT 3:-

Enter a string
'emotions', controlled, 'controlled' and 'directed to work' show the greatness of swami
Vivekananda.
Vivekananda swami of greatness the show work to directed and controlled
controlled emotions
Program 4 : Write a program in bluej to input natural numbers between 1 to 1000 and
then output in words.

import java.util.*;
class naturalnumbers
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
int n;
System.out.println("Enter a natural number"); /*to enter number*/
n=Sc.nextInt(); /* to extract new integer*/
int r,m=n;
String str="",st="";
if(n>=1 && n<=1000)

{
while(n!=0)
{
r=n%10;
if(r==0)
str="zero";
if(r==1)
str="one";
if(r==2)
str="two";
if(r==3)
str="three";
if(r==4)
str="four";
if(r==5)
str="five";
if(r==6)
str="six";
if(r==7)
str="seven";
if(r==8)
str="eight";
if(r==9)
str="nine";
st=' '+str+st;
n=n/10;
}
System.out.println("Number "+m+" in letters="+st);
}
else
System.out.println("Number "+m+" not in range");

}
}
OUTPUT 4:-

Enter a natural number


29
Number 29 in letters= two nine
Enter a natural number
46
Number 46 in letters= four six
Enter a natural number
1009
Number 1009 not in range
Enter a natural number
999
Number 999 in letters= nine nine nine
Program 5 :- Write a program in bluej to store 10 integers in a single dimension
array,arrange them in descending order. Ask the user to input one more number and
store it at proper position.

import java.util.*;
class descendingarray
{
void main()
{
Scanner scanner = new Scanner(System.in); /*to input*/
int[] arr = new int[11];
int i, j, temp;
System.out.println("Enter 10 integers:"); /*to enter 10 integers*/
for (i = 0; i < 10; i++)
{
arr[i] = scanner.nextInt();
}
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9 - i; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Enter another number:"); /* to display to enter another number*/
int newNumber = scanner.nextInt();
int pos = 10;
for (i = 0; i < 10; i++)
{
if (newNumber > arr[i])
{
pos = i;
break;
}
}

for (i = 10; i > pos; i--)


{
arr[i] = arr[i - 1];
}
arr[pos] = newNumber;
System.out.println("Displaying the array in descending order:");
for (i = 0; i < 11; i++)
{
System.out.println(arr[i]);
}
}
}
OUTPUT 5:-

Enter 10 integers
3
4
5
6
7
90
10
8
2
3
Enter another no.
100
Displaying the arrray in descending order:-
100
90
10
8
7
6
5
4
3
3
2
Program 6 : Write a program in bluej to store n integers (n>0) in a S.D.A.
and store only palindrome numbers in another array. Display the new array.

import java.util.*;
class palindromearray
{
void main(int n)
{
Scanner Sc=new Scanner(System.in); /*to input*/
int i,j=0;
int b[]=new int[n];
int a[]=new int[n];
System.out.println("Enter your numbers"); /*to enter numbers*/
for(i=0;i<n;i++)
{
a[i]=Sc.nextInt();
}
for(i=0;i<n;i++)
{
int r,p=0,m=a[i],temp=a[i];
while(temp!=0)
{
r=temp%10;
temp=temp/10;
p=p*10+r;
}
if(p==m) /* checking if p is equal to m*/
{
b[j]=m;
j++;
}
}
System.out.println("Displaying the array containing only palindrome
number:-");
for(i=0;i<j;i++)
{
System.out.println(b[i]);
}
}
}
OUTPUT 6:-

Enter numbers
101
109
890
787
7
777
9876
11
000
909
Displaying the array containing only palindrome number:-
101
787
7
777
11
0
909
Program 7 : Write a method that takes a number in decimal number system
as argument and returns its binary equivalent.

import java.util.* ;
class DecimalToBinary
{
void main()
{

Scanner sc = new Scanner(System.in); /*to input*/


System.out.println("Enter a decimal number:"); /*to enter decimal numbers*/
int decimal = sc.nextInt(); /* to extract the integer*/
if (decimal < 0)
{
System.out.println("Please enter a non-negative integer.");
return;
}
String binary = "";
if (decimal == 0)
{
binary = "0";
}
else
{

while (decimal > 0)


{
int remainder = decimal % 2;
binary = remainder + binary;
decimal = decimal / 2;
}
}
System.out.println("The binary equivalent is: " + binary);
}
}
OUTPUT 7:-

Enter a number
100
The binary equivalent of the number 100 = 1100100
Enter a number
678
The binary equivalent of the number 678 = 1010100110
Enter a number
9009
The binary equivalent of the number 9009 = 10001100110001
Program 8 : Write a method in bluej that takes a binary equivalent number
as argument and returns its decimal equivalent.

import java.util.*;
class binarytodecimal
{
void main()
{

Scanner sc = new Scanner(System.in); /*to input*/


System.out.println("Enter a binary number:"); /*to enter binary numbers*/
String bn = sc.nextLine(); /* to input string*/
int dn = 0;
int base = 1;

for (int i = bn.length() - 1; i >= 0; i--)


{
char ch = bn.charAt(i);
if (ch == '1')
{
dn += base;
}
else if (ch != '0')
{

System.out.println("Invalid binary code. Please enter a valid binary


number.");
return;
}
base *= 2;
}
System.out.println("The decimal equivalent of the binary " + bn + " = " + dn);
}
}
OUTPUT 8:-

Enter a binary number


100
The decimal equivalent of the binary 100 = 4
Enter a binary number
1010100110
The decimal equivalent of the binary 1010100110 = 678
Enter a binary number
1010100
The decimal equivalent of the binary 1010100 = 84
Program 9 :- A perfect square is an integer which is the square of another integer.
For example, 4, 9,16.. are perfect squares. Design a Class Perfect with the following
description:-

Class name: Perfect.


Data members/instance variables:-
n : stores an integer number.
Member functions:-
Perfect() : default constructor.
Perfect(int) : parameterized constructor to assign a value to 'n'.
void perfect_sq() : to display the first 5 perfect squares larger than
'n'(if n= 15, the next 3 perfect squares are 16, 25, 36).
void sum_of() : to display all combinations of consecutive integers whose sum
is equal to n. Example -The number n =15 can be expressed as:-
12345
456
78
Specify the class Perfect giving details of the constructors, void perfect_sq() and void
sum_of().
Also define the main function to create an object and call methods accordingly to
enable the task.

class perfect
{
int n;
perfect()
{
n=0;
}

perfect(int a)
{
n=a;
}

void perfect_sq()
{
int c=0;
int num=(int)Math.sqrt(n)+1;
System.out.println("The next 5 perfect squares which are greater than the
inputed number "+n+":-");
while(c<5)
{
int perfectsquare=num*num; /* to do square*/
System.out.println(perfectsquare); /*to print the perfect square*/
c++;
num++;
}
}

void sum_of()
{
int a,i;
boolean found=false;
System.out.println("Displaying all the combitions of consecutive integers whose
sum is equal to the inputed number:-");
for(a=1;a<n;a++)
{
int s=0;
String sequence="";
for(i=a;i<n;i++)
{
s=i+s;
sequence=sequence+i+" ";
if(s==n) /*checking if s is equal to n*/
{
System.out.println(sequence);
found =true;
break;
}
else if(s>n)
break;
}
}
}

void main(int a)
{
perfect p1=new perfect(a);
p1.perfect_sq();
p1.sum_of();
}
}
OUTPUT 9:-

The next 5 perfect squares are which are greater than the inputed number 15:-
16
25
36
49
64
Displaying all the combitions of consecutive integers whose sum in equal to the
inputed number:-
12345
456
78
The next 5 perfect squares are which are greater than the inputed number 20:-
25
36
49
64
81
Displaying all the combitions of consecutive integers whose sum in equal to the
inputed number:-
23456
The next 5 perfect squares are which are greater than the inputed number 16:-
25
36
49
64
81
Displaying all the combitions of consecutive integers whose sum in equal to the
inputed number:-
Program 10 : In "Piglatin" a word such as KING is replaced by INGKAY, while
TROUBLE becomes OUBLETRAY and so on. The first vowel of the original word
becomes the start of the translation, any preceding letters being shifted towards the
end and followed by AY. Words that begin with a vowel or which do not contain any
vowel are left unchanged. Design a class Piglatin using the description of the data
members and member functions given below:

Class name: Piglatin


Data members /instance variables:
Txt: to store a word
len: to store the length
Member functions:
Piglatin(): constructor to initialize the data mrmbers
void readstring(): to accept the word input in UPPER CASE void convert (): converts
the
word into its piglatin form and displays the word (changed or unchanged)
void consonant(): counts and displays the number of consonants present in
the given word.

Specify the class Piglatin giving the details of the constructor, void readstring(),
void convert() and void consonant(). Also define the main function to create an
object and call methods accordingly to enable the task.

import java.util.Scanner;
class piglatin
{
String txt;
int len;
piglatin()
{
txt="";
len=0;
}
void readstring()
{
Scanner sc=new Scanner(System.in); /*to input*/
System.out.println("Enter your string in upper case"); /*to display to enter string in upper case*/
txt=sc.nextLine(); /*to input the string*/
txt=txt.toUpperCase();

len=txt.length();
}
void convert()
{
int i,k=0; char c,d=txt.charAt(0);String t="",p="";
for (i=1;i<len;i++)
{
c=txt.charAt(i);
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
k++;
}
if(d=='A' || d=='E' || d=='I' || d=='O' || d=='U')
{

System.out.println(" The first letter of the word is a vowel!");


System.out.println("Unchanged word! : "+ txt);
}
else if(k==0)
{

System.out.println("The word has no vowels!");


System.out.println("Unchanged word: " + txt);
}
else
{

for( i=0;i<len;i++)
{
c=txt.charAt(i);
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
{
p=txt.substring(i);
break;
}
else
t=t+c;
}
System.out.println("The pigltin form of the word is : " + p + t+ "AY");
}
}

void consonant()
{
int z=0;
char c;
int i;
len=txt.length();
for(i=0;i<len;i++)
{
c=txt.charAt(i);
if(c!='A' || c!='E' || c!='I' || c!='O' || c!='U')
z++;
}
System.out.println("The number of consonants in the string are : " + z);
}
void main()
{
piglatin p1=new piglatin();
p1.readstring();
p1.convert();
p1.consonant();
}

}
OUTPUT 10:-
Enter a string in Upper Case
KING
The pig latin form = INGKAQ
Number of consonants = 1
Enter a string in Upper Case
TROUBLE
The pig latin form = OUBLETRAQ
Number of consonants = 3
Enter a string in Upper Case
AMEN
The pig latin form = AMEN
Number of consonants = 2
Enter a string in Upper Case
HELLO
The pig latin form = ELLOHAQ
Number of consonants = 2
Program 11 : Write a program in bluej to input 100 numbers through the keyboard
using scanner class and display only those numbers which are palindrome. your
program must validate for integer numbers.

import java.util.*;
class palindromenumbers
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
int a[]=new int[100];
int b[]=new int[100];
int i,j=0;
System.out.println("enter 100 integers"); /* to enter 100 numbers*/
for(i=0;i<100;i++)
{
a[i]=Sc.nextInt();
}
for(i=0;i<100;i++)
{
int r,p=0,m=a[i],temp=a[i];
while(temp!=0)
{
r=temp%10;
temp=temp/10;
p=p*10+r;
}
if(p==m) /*checking if c is equal to m*/
{
b[j]=m;
j++;
}
}
for(i=0;i<j;i++)
{
System.out.println(b[i]);
}
}
}
OUTPUT 11:-
enter 100 integers
1
2
3
4
5
6
7
8
9
0
1
2
1
3
1
4
5
2
3
53
465
769
856
77
88
99
11
22
3
3334
5
5
6
6
7
576
776
87
665
56
56
4545
555
5655
77
8
89
6766
54
55
555
666
121
232
43435
75
447
38454
4645
87454
9565
73643
8466
0687
9676
6986
607
9676
8647
84
8565
766
5554
765
647
86
968
99
999
88
7777
66565
76767
77
4847
75
8465
8464
585
8746565
4756
5665
6
56456
6456
98
9445
8545
454
45
1
2
3
4
5
6
7
8
9
0
1
2
1
3
1
4
5
2
3
77
88
99
11
22
3
5
5
6
6
7
555
77
8
55
555
666
121
232
99
999
88
7777
76767
77
585
5665
6
454
Program 12 : Write a method in bluej that takes two integers as argument
and returns its HCF using division method.

import java.util.*;
class HCF
{
int HCF(int a,int b)
{
int r,i;
System.out.print("The highest common factor=");
while(b!=0)
{
int temp=b;
b=a%b;
a=temp;
}
return a; /*returning the value of a*/
}
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
int x,y;
System.out.println("Enter two numbers"); /* to display to enter 2 numbers*/
x=Sc.nextInt();
y=Sc.nextInt();
System.out.println(HCF(x,y));
}
}
OUTPUT 12:-
Enter two no.
36
24
The highest common factor=12
Enter two no.
45
30
The highest common factor=15
Enter two no.
350
100
The highest common factor=50
Program 13 : Write a program to input the date dd/mm/yyyy format and check
whether given date is valid or not. If date is valid display in mm/dd/yyyy format.

import java.util.Scanner;
class DateValidator
{
void main()
{
Scanner sc = new Scanner(System.in); /*to input*/
System.out.print("Enter the date in dd/mm/yyyy format: "); /* to enter the date as per given format*/
String date = sc.nextLine(); /*to enter string*/
if (date.length() != 10 || date.charAt(2) != '/' || date.charAt(5) != '/')
{
System.out.println("Invalid format. Please enter in dd/mm/yyyy format.");
return;
}
String dayStr = date.substring(0, 2);
String monthStr = date.substring(3, 5);
String yearStr = date.substring(6);
for (int i = 0; i < dayStr.length(); i++)
{
if (dayStr.charAt(i) < '0' || dayStr.charAt(i) > '9')
{
System.out.println("Invalid day.");
return;
}
}
for (int i = 0; i < monthStr.length(); i++)
{
if (monthStr.charAt(i) < '0' || monthStr.charAt(i) > '9')
{
System.out.println("Invalid month.");
return;
}
}
for (int i = 0; i < yearStr.length(); i++)
{
if (yearStr.charAt(i) < '0' || yearStr.charAt(i) > '9')
{
System.out.println("Invalid year.");
return;
}
}
int day = Integer.parseInt(dayStr);
int month = Integer.parseInt(monthStr);
int year = Integer.parseInt(yearStr);
if (year < 1)
{
System.out.println("Invalid year.");
return;
}
if (month < 1 || month > 12)
{
System.out.println("Invalid month.");
return;
}
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
daysInMonth[1] = 29;
}
if (day < 1 || day > daysInMonth[month - 1])
{
System.out.println("Invalid day.");
return;
}
System.out.println("Valid date!");
System.out.println("Date in mm/dd/yyyy format: " + month +"/" + day + "/" +
year );
}
}
OUTPUT 13
Enter the date in dd/mm/yyyy format: 22/02/2024
Valid date!
Date in mm/dd/yyyy format: 2/22/2024
Enter the date in dd/mm/yyyy format: 30/02/2078
Invalid day.
Enter the date in dd/mm/yyyy format: 29/02/2000
Valid date!
Date in mm/dd/yyyy format: 2/29/2000
Program 14 : Write a program in blue j to delete a number from a SDA.

import java.util.*;
class delete
{
void main()
{
Scanner Sc = new Scanner(System.in); /*to input*/
System.out.println("Enter how many numbers you want to input:");
int n = Sc.nextInt();
int a[] = new int[n];
int c[] = new int[n - 1];
int i, e = -1, f, j, s = 0, g, h, z;
System.out.println("Enter numbers:"); /* to display for entering numbers*/
for (i = 0; i < n; i++)
{
a[i] = Sc.nextInt();
}
System.out.println("Enter a number to deleted");
int b = Sc.nextInt();
for (i = 0; i < n; i++)
{
if (b == a[i])
{
e = i;
break;
}
}
if (e == -1)
{
System.out.println("Number not found in the array.");
return;
}
f = e - 1;
j = e - 1;
while (f >= 0) /* to check if f is greater than or equal to 0 */
{
c[s] = a[f];
f--;
s++;
}
g = s;
h = s;
z = g + 1;
while (z < n)
{
c[h] = a[z];
h++;
z++;
}
System.out.println("Displaying the new array after deleting the given number:");
for (i = j; i >= 0; i--)
{
System.out.println(c[i]);
}
for (i = j + 1; i < n - 1; i++)
{
System.out.println(c[i]);
}
}
}
OUTPUT 14:-
Enter how many numbers you want to input:
5
Enter numbers:
12
15
78
90
89
Enter a number to deleted
16
Number not found in the array.
Enter how many numbers you want to input:
5
Enter numbers:
89
17
16
15
14
Enter a number to deleted
14
Displaying the new array after deleting the given number:
89
17
16
15
Enter how many numbers you want to input:
7
Enter numbers:
90
89
78
67
56
45
34
Enter a number to deleted
45
Displaying the new array after deleting the given number:
90
89
78
67
56
Program 15 : Write a program in blue j to input a string and display the ASCII code
of each character.

import java.util.*;
class ascii
{
void main()
{
Scanner Sc=new Scanner(System.in); /*to input*/
String s;
char c;
int i,l,d;
System.out.println("enter a string"); /* to display to enter a string*/
s=Sc.nextLine();
l=s.length();
for(i=0;i<l;i++)
{
c=s.charAt(i); /*to extract characters*/
d=(int)c;
System.out.println("ASCII CODE OF "+c+" = "+d);
}
}
}
OUTPUT 15 :-
enter a string
raman
ASCII CODE OF r = 114
ASCII CODE OF a = 97
ASCII CODE OF m = 109
ASCII CODE OF a = 97
ASCII CODE OF n = 110
enter a string
junior
ASCII CODE OF j = 106
ASCII CODE OF u = 117
ASCII CODE OF n = 110
ASCII CODE OF i = 105
ASCII CODE OF o = 111
ASCII CODE OF r = 114
enter a string
sqrt
ASCII CODE OF s = 115
ASCII CODE OF q = 113
ASCII CODE OF r = 114
ASCII CODE OF t = 116
Program 16 : A company announces and increment of their employees on seniority
basis as per the given condition: -

Age Increment

56 years and above 20% of basic


Above 45 and below 56 15% of basic
Upto 45 10% of basic

Write a program to find new basic by using the following class specifications:

Class : Increment
Data members:
String name. double basic, int age
Member methods:
Void getdata(); to accept name, basic and age
Void calc() to find increment and update basic
Void display(): to display age and basic in the following format:

Age Basic
-------- ---------
Write main method to create object and call all the methods defined above.

import java.util.*; //importing an another package


class Increment
{
//creating instance variables
String name;
double basic;
int age;
void getdata() //to input the name,basic salary,age.
{
Scanner Sc=new Scanner(System.in); /*to input*/
System.out.println("Enter name");
name=Sc.nextLine();
System.out.println("Enter basic salary");
basic =Sc.nextDouble();
System.out.println("Enter age");
age=Sc.nextInt();
}

void calc() //to calculate increment and update basic


{
double inc=0;
if(age>=56) /* checking if age is greater than or equal to 56*/
{
inc=0.20*basic;
basic=basic+inc;
}
if(age >45 && age<56)
{
inc=0.15* basic;
basic=basic+inc;
}
if(age<=45)
{
inc=0.10*basic;
basic=basic+inc;
}
}

void display() //displaying the details in the given format


{
System.out.println("age \t Basic");
System.out.println(age +"\t" + basic);
}

void main() // object to call methods


{
Increment b1=new Increment();
b1.getdata();
b1.calc();
b1.display();
}
}
OUTPUT 16:-
Enter name
RAM
Enter basic salary
1800
Enter age
60
age Basic
60 2160.0
Enter name
shyam
Enter basic salary
80000
Enter age
24
age Basic
24 88000.0
Enter name
akash
Enter basic salary
100000
Enter age
45
age Basic
45 110000.0
Program 17 : Define a class Recurring patterns and define methods in it which will
print the following
patterns :
a) The method takes an integer argument ‘n’ and print the following pattern. Shown
for
n=4.
a
aa
aaa
aaaa
aaa
aa
a
b) The method takes an integer argument ‘n’ and print the following pattern. Shown
for n=4.
1
121
12321
1234321
12321
121
1
c) The method takes an integer argument ‘n’ and print the following pattern. Shown
for n=4.
abcdcba
abc cba
ab ba
a a
ab ba
abc cba
abcdcba

import java.util.Scanner;
class RecurringPatterns
{
// Method to print pattern (a)
void patternA(int n)
{
int i,j;
for (i = 1; i <= n; i++)
{
// Print leading spaces
for ( j = 1; j <= n - i; j++)
{
System.out.print(" ");
}
// Print 'a'
for ( j = 1; j <= i; j++)
{
System.out.print("a ");
}
System.out.println();
}
for (i = n - 1; i >= 1; i--)
{
// Print leading spaces
for (j = 1; j <= n - i; j++)
{
System.out.print(" ");
}
// Print 'a'
for ( j = 1; j <= i; j++)
{
System.out.print("a ");
}
System.out.println();
}
}
// Method to print pattern (b)
void patternB(int n)
{
int i,j;
for ( i = 1; i <= n; i++)
{
// Print leading spaces
for ( j = i; j < n; j++)
{
System.out.print(" ");
}
// Print increasing numbers
for (j = 1; j <= i; j++)
{
System.out.print(j);
}
// Print decreasing numbers
for ( j = i - 1; j >= 1; j--)
{
System.out.print(j);
}
// Move to the next line
System.out.println();
}
// Lower part of the pyramid
for ( i = n - 1; i >= 1; i--)
{
// Print leading spaces
for (j = n; j > i; j--)
{
System.out.print(" ");
}
// Print increasing numbers
for ( j = 1; j <= i; j++)
{
System.out.print(j);
}
// Print decreasing numbers
for ( j = i - 1; j >= 1; j--)
{
System.out.print(j);
}
// Move to the next line
System.out.println();
}
}
// Method to print pattern (c)
void patternC(int n)
{
// Top part
int i,j;
for (i = 0; i < n; i++)
{
for ( j = 0; j < n - i; j++)
{
System.out.print((char) ('a' + j));
}
for (j = 0; j < 2 * i; j++)
{
System.out.print(" ");
}
for ( j = n - i - 1; j >= 0; j--)
{
System.out.print((char) ('a' + j));
}
System.out.println();
}
// Bottom part
for ( i = n - 2; i >= 0; i--)
{
for ( j = 0; j < n - i; j++)
{
System.out.print((char) ('a' + j));
}
for ( j = 0; j < 2 * i; j++)
{
System.out.print(" ");
}
for ( j = n - i - 1; j >= 0; j--)
{
System.out.print((char) ('a' + j));
}
System.out.println();
}
}
OUTPUT 17:-

Enter the value of n: 4

Pattern A:
a
aa
aaa
aaaa
aaa
aa
a

Pattern B:
1
121
12321
1234321
12321
121
1

Pattern C:
abcdcba
abc cba
ab ba
a a
ab ba
abc cba
abcddcba
Program 18 : write a program in blue -j where there are two single subscripted
variable A and B. Array A contains integer numbers in ascending order and array b
contains in descending order.Write a program in bluej to merge these array into the
third array C in such a way that resultant array will contain the number in ascending
order. Without using either of the sorting technique.

import java.util.Scanner;
class MergeArrays
{
void main()
{
Scanner sc = new Scanner(System.in); /*to input*/
System.out.println("Enter size of array A:");
int sizeA = sc.nextInt();
System.out.println("Enter size of array B:");
int sizeB = sc.nextInt();
int[] A = new int[sizeA];
int[] B = new int[sizeB];
int z=sizeA+sizeB;
int[] C = new int[z];
System.out.println("Enter elements of array A in ascending order:");
for (int i = 0; i < sizeA; i++)
{
A[i] = sc.nextInt();
}
System.out.println("Enter elements of array B in descending order:");
for (int i = 0; i < sizeB; i++)
{
B[i] = sc.nextInt();
}
int i = 0, j = sizeB - 1, k = 0;
while (i < sizeA && j >= 0)
{
if (A[i] <= B[j]) /*checking if A[i]<=B[j]*/
{
C[k++] = A[i++];
} else
{
C[k++] = B[j--];
}
}
while (i < sizeA)
{
C[k++] = A[i++];
}
while (j >= 0) /*checking if j>=0*/
{
C[k++] = B[j--];
}
System.out.println("Merged array C in ascending order:");
for ( i=0;i<z;i++)
{
System.out.println(C[i]);
}
}
}
OUTPUT 18 :-

Enter size of array A:


5
Enter size of array B:
5
Enter elements of array A in ascending order:
1
2
3
4
5
Enter elements of array B in descending order:
10
9
8
7
6
Merged array C in ascending order:
1
2
3
4
5
6
7
8
9
10
Program 19 : Write a program in bluej to input a string and remove all the common
characters from it.

import java.util.Scanner;
class RemoveCommonCharacters
{
String removeCommon(String str)
{
String result = "";
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (str.indexOf(ch) == str.lastIndexOf(ch))
{
result += ch;
}
}
return result;
}
void main()
{
Scanner sc = new Scanner(System.in); /*to input*/
RemoveCommonCharacters rcc = new RemoveCommonCharacters();
System.out.print("Enter a string: "); /* to display to enter a string*/
String input = sc.nextLine(); /*to input a string*/
String output = rcc.removeCommon(input);
System.out.println("String after removing common characters: " + output);
}
}
OUTPUT 19:-

Enter a string: hell


String after removing common characters: he
Enter a string: progress
String after removing common characters: poge
Program 20 : The International Mobile Station Equipment Identity or IMEI is a
number, usually unique, to identify mobile phones, as well as some satellite phones.
It is usually found printed inside the battery compartment of the phone.
The IMEI number is used by a GSM network to identify valid devices and therefore
can be used for stopping a stolen phone from accessing that network.
The IMEI (15 decimal digits: 14 digits plus a check digit) includes information
on the origin, model, and serial number of the device.
The IMEI is validated in three steps:
1. Starting from the right, double every other digit (e.g., 7 becomes 14).
2. Sum the digits (eg, 141+4).
3. Check if the sum is divisible by 10.

import java.util.*;
class IMEI
{
void main()
{
Scanner Sc = new Scanner(System.in); /*to input*/
System.out.println("Enter a 15-digit IMEI number:");
long b = Sc.nextLong(); String l="";
l=Long.toString(b);
if (l.length() != 15)
{
System.out.println("Invalid input. IMEI number must be 15 digits.");
return;
}
int d = 0;

for (int i = 1; i <= 15; i++)


{
int x = (int) (b % 10);
b = b / 10;
if (i % 2 == 0) /* if dividing I by 2 and checking if remainder is zero*/
{
int e = x * 2;

d += (e / 10) + (e % 10);
} else
{
d += x;
}
}

if (d % 10 == 0) /*dividing d by 10*/
{
System.out.println("The IMEI number is valid.");
} else
{
System.out.println("The IMEI number is invalid.");
}
}
}
OUTPUT 20:-
Enter a 15-digit IMEI number:
490154203237
Invalid input. IMEI number must be 15 digits.
Enter a 15-digit IMEI number:
490154203237518
The IMEI number is valid.

You might also like