Вы находитесь на странице: 1из 38

PROGRAM 1

Write a program accept a sentence and to convert each letter of a sentence to its opposite
case.
import java.util.*;
class convert
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s1=sc.nextLine();
String s=s1+" ";
int l=s.length();String g="";
for(int i=0;i<l;i++){
char ch=s.charAt(i);char ch2='\u0000';
if(Character.isLetter(ch)){
if(Character.isUpperCase(ch)){
ch2=Character.toLowerCase(ch);
g+=ch2;}
else if(Character.isLowerCase(ch))
{ch2=Character.toUpperCase(ch);
g+=ch2;}}
else if(ch==' ')
{
System.out.print(g+" ");
g="";}}}}

Output:
Variable description table(Program 1)
Variable Data type Description
s1 String To store the input given by user
s String To store the input by adding space
at end
l int To store the no. of characters in s
g String To store the converted string
i int Variable used in for-loop
ch char Character at position i in s
ch2 char Converted character of ch

PROGRAM 2
A class Telcall calculates the monthly phone bill of a consumer. Some of the members of the class are given
below:
Class name Telcall
Data members/instance variable: phno: phone number
name : customer name
n : number of calls made
amt : bill amount
Member functions/methods
Telcall() : Parameterised Constructor to assign values to data members
void compute() : to calculate the phone bill amount based on the slabs given below./void dispdata()
: to display the details in the specified format.
Phone Number Name Total calls Amount/ XXXXXXXXXXXX XXX
XXXXXXXX XXXX
The calculations need to be done as per the slabs.
Number of calls : Rate
1-100 : Rs.500/- rental charge only
101-200 : Rs. 1.00/- per call+rental charge
201-300 : Rs. 1.20/- per call+rental charge
above 300 : Rs. 1.50/- per call+rental charge
Specify the class Telcall, giving the details of the constructor, void compute() and void display. In main(),
create objects of the type Telcall and display the phone bill.

import java.util.*;
class Telcall
{
long pho;
String name;
int n;
double amt;
public Telcall(long p,String z,int d)
{
pho=p;
name=z;
n=d;
}

public void compute()


{
Scanner s=new Scanner(System.in);
System.out.println("enter the name");
name=s.nextLine();
System.out.println("enter the phonr number");
pho=s.nextLong();
System.out.println("enter the number of calls");
n=s.nextInt();
if(n>1&&n<=100)
amt=500;
else if(n>100&&n<=200)
amt=(1*n)+500;
else if(n>200&&n<=300)
amt=(1.20*n)+500;
else
amt=(1.50*n)+500;
}

public void dispdata()


{
System.out.println("PHONE NUMBER\tNAME\tTOTAL CALLS\tAMOUNT");
System.out.println(pho+"\t"+name+"\t"+n+"\t"+amt);
}

public static void main()


{

Telcall obj=new Telcall(12234324,"say",23);

obj.compute();
obj.dispdata();
}
}

OUTPUT:
Variable description table(Program 2)

Variable Data type Description


pho long Variable used to store phone
number of the customer
name String Variable used to store the name of
the customer
n int Variable used to store number of
calls made by the customer
amt double Variable used to calculate and
store the total bill amount of the
customer
p long A variable used in parameterized
constructor Tellcall() to assign
values to class variable
z String A variable used in parameterized
constructor Tellcall() to assign
values to class variable
d int A variable used in parameterized
constructor Tellcall() to assign
values to class variable

PROGRAM 3
Write a program using switch case to
1) print the given pattern 2) find the sum of the given series.
1+a2 - a4 + a6- a8……..n terms
A 2! 4! 6! 8!
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
import java.util.*;
class patt{
public static void main()
{
Scanner km=new Scanner(System.in);
System.out.println("ENTER 1 0r 2");
int ch=km.nextInt();
switch(ch){
case 1:
for(int i=65;i<=70;i++){
for(int j=65;j<=i;j++)
System.out.print((char)j); // prints the character whose ASCII value is j

for(int k=i-1;k>=65;k--)
System.out.print((char)k); // prints the character whose ASCII value is k
System.out.println();
}
break;
case 2:System.out.println("ENTER THE VALUES OF a AND n");
int a=km.nextInt();
int n=km.nextInt();
double sum=1.0d;
int powe=2;
int div=2;
for(int i=2;i<n;i++,div+=2,powe+=2){
int di=div;int fact=1;
for(;di>0;di--) //finds factoial of the digit
{
fact*=di;

}
if(i%2==0) // adds every even term to the sum
sum+=(Math.pow(a,powe)/fact);
else //subtracts every odd term from the sum
sum-=(Math.pow(a,powe)/fact);
}
System.out.println(sum);

break;
default: System.out.println("INVALID INPUT. ENTER ONLY 1 OR 2");
System.exit(0);

}
}
}

OUTPUT:
Variable description table(Program 3)

Variable Data type Description


ch int To take input from the user
according to the choice of the
user
i int Variable used in for-loop
j int Variable used in for loop
k int Variable used in for-loop
a int Variable used to take input of
the number used to find the
sum
n int Variable used to store limit
powe int Variable used in for-loop and
to raise a to p to find sum
div int Variable used in for-loop to
find the sum
di int Variable used to store vaslue
of div and calculate sum
accordingly.
fact int Variable used to store the
factorial of di
sum double Variable used to calculate the
sum of the series

PROGRAM 4
Write a program to generate and print the fibonacci strings upto n terms. The sum of (‘+’ i.e.
concatenation) first two strings is the third string. Eg. “a�? is first string, “b�? is second
string, then the third will be “ba�?, and fourth will be “bab�? and so on.
A sequence of fibonacci strings is generated as follows:
S0 = “a�?, S1 = “b�?, Sn = S(n – 1) + S(n – 2) where ‘+’ denotes concatenation. Thus the
sequence is a, b, ba, bab, babba, babbabab, … n terms.*/

import java.util.*;
class FIBONACCI{
public static void main(){
Scanner q=new Scanner(System.in);
System.out.println("ENTER TWO STRINGS");
String a=q.nextLine();
String b=q.nextLine();
System.out.println("ENTER THE VALUE OF n");
int n=q.nextInt();
String ar[]=new String[n];
ar[0]=a;
ar[1]=b;
System.out.print(a+" "+b+" ");
for(int i=2;i<n;i++){
String t=ar[(i-1)];
String t1=ar[(i-2)];
ar[i]=t+t1;
System.out.print(ar[i]+" ");
} }}
OUTPUT:

Variable description table(Program 4)


Variable Data type Description
a String Variable used to take input
from the user for one string
b String Variable used to take input
from the user for other string
n int Variable used to take input of
the limit of fibonacci series
ar[] String Array used to store each
string in the series
i int Variable used in for-loop
t String Variable used in the procees
of finding the Fibonacci
series
t1 String Variable used in the procees
of finding the Fibonacci
series

PROGRAM 5
Write a program to input a sentence. Count and print the frequency of each alphabet present in the string.
The output should be given as:
Sample Input:​ Java is Fun
Sample Output:
==========================
Alphabet Frequency
==========================
J 1
F 1
a 2
v 1
i 1
s 1
u 1
n 1

package record;
import java.util.*;
class Freq{
public static void main(){
Scanner q=new Scanner(System.in);
System.out.println("Enter a String");
String in=q.nextLine();
int l=in.length();
System.out.println("------------------------------------");
System.out.println("------------------------------------");
System.out.println("ALPHABET FREQUENCY");
System.out.println("------------------------------------");
System.out.println("------------------------------------");
for(int i=0;i<l;i++){
char c=in.charAt(i);
int count=0;
if(c!=' ')
{
if(in.indexOf(c)==i){
for(int j=i;j<l;j++){
char p=in.charAt(j);
if(in.charAt(j)==c)
count++;}}
else
continue;
System.out.println(c+ " "+ count);
} } }}

OUTPUT:
Variable Description table(Program 5)
Variable Data type Description
in String To take input of a sentence
from the user
l int To store the length of in
i int Variable used in for-loop
c char Variable used to extract
character at i in string in
count int Variable used as a counter to
find the frequency of each
alphabet
j int Variable used in for-loop
p char Variable used to extract
character at j in string in

PROGRAM 6
Write a program to ​accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is
separated by single space. Decode the words according to their potential. The potential of a word is found by
adding the encrypted value of the alphabets. The encryption of alphabets are to be done as follows: A=1
B=2 ,C=3,…..Z = 26
Output the result in format given below
THE SKY IS THE LIMIT.​
POTENTIAL : THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
import java.util.*;
class terminate_p6{
public static void main(){
Scanner q=new Scanner(System.in);
System.out.println("Enter a sentence that ends with . or, or ? or !");
String in=q.nextLine();
int l=in.length();
char la=in.charAt(l-1);
if(la!='.'&&la!=','&&la!='?'&&la!='!') { //checks if the string ends with ',' or'.' or'?' or '!'
System.out.println("INVALID INPUT");
System.exit(0);}
in=in.substring(0,l-1)+" "; //extracts string excluding last character
for(int i=0;i<l-1;)
{
int pot=0;
int j=i;
while(in.charAt(j)!=' '){
int c=0;
char ch=in.charAt(j);
if(Character.isUpperCase(ch))
c=(char)ch-64; //potential of letter in smaller case
if(Character.isLowerCase(ch))
c=(char)ch-96; //potential of letter in upper case
pot+=c;
j++;}
String word=in.substring(i,j); // extracts word between two spaces
System.out.println(word+ "\t"+ pot);
i=j+1;}}}

OUTPUT:

Variable Description Table(Program 6​)


Variable Data type Description
in String To take input of a sentence
from the user
l int To store the length of the
string in
la char To extract the last character
of in
i int Variable used in for-loop
pot int Variable used to calculate the
total potential of each word
in the string
j int Variable used to indicate
position of each character in
each word of in
ch char Variable used to extract the
character at j in each word of
in
word String To store the string without
the word whose potential
value is printed
PROGRAM 7
Write a program to accept a 3 lettered word and print its anagrams.
Note:​ Anagrams are words made up of all the characters present in the original word by
re-arranging the characters.
Example:​ Anagrams of the word TOP are: TOP, TPO, OPT, OTP, PTO and POT
import java.util.*;
class anagram{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the word");
String s=sc.next();
int l=s.length();
if(l>3)
{System.out.println("Enter a 3 lettered word:--Terminating");
System.exit(0);
}
else{
for(int i=0;i<l;i++)
{for(int j=0;j<l;j++)
{for(int k=0;k<l;k++)
{if(i!=j&&j!=k&&k!=i)
{String ana=s.charAt(i)+""+s.charAt(j)+""+s.charAt(k);
System.out.println(ana);
}}
System.out.println();
}}}}}

Output:
Variable Description table(Program 7)
Variable Data type Description
s String Variable used to take input from
user
l int To store length of string s
i int Variable used in for-loop
j int Variable used in for-loop
k int Variable used in for-loop
ana String To store the new string
PROGRAM 8
The consecutive Primetumbers are known as Prime Triplets if they satisfy the follows;
(n,n+2,n+6) are all Prime or (n,n+4,n+6) are all Prime . Where t is an integer number >0
ift=5,then 5,7,11 are all Prime so they arre Prime Triplets.
Ift=7, then (7,7+2, 7+6) 7,9,13 all aretot primes so they aretot the Prime Triplets.
But ift=7, then (7,7+4,7+6) 7,11,13 all are Prime and they are Prime Triplets.
Write a program to input a start limit S>0 and a last limit L>0. Print all the Prime Triplets
between S and L with sutiablemessage . The Prime Triplets may be greater or be greater or
lesser than L depending upon the conditions used for generating Prime combinations. Print
the totaltumber of primer Triplets at the end.
Input
s=3, l=15
Output
Prime Triplets
5 7 11
7 11 13
11 13 17
13 17 1
Total Prime Triplet combinations are =4
package record;
import java.util.*;
class PRIME_TRIPLE
{
public static void main()
{
Scanner km=new Scanner(System.in);
System.out.println("ENTER THE STARTING LIMIT s");
int S=km.nextInt();
System.out.println("ENTER THE ENDING LIMIT l");
int L=km.nextInt();
int c=0;
for(int i=S;i<=L;i++)
{
int k=primecheck(i);
int m=0;int t=0;
if(k!=0)
{
t=k+6;
if(primecheck(t)!=0)
{
m=k+2;
if(primecheck(m)==0)
{
m=k+4;
if(primecheck(m)!=0)
{
System.out.println(i+" "+m+" "+t);
c++;
}
else
continue;
}
else
{
System.out.println(i+" "+m+" "+t);
c++;}}}}
System.out.println("TOTAL PRIME TRIPLETS COMBINATION ARE "+c);
}
public static int primecheck(int a)
{
int y=0;
for(int i=2;i<a;i++)
if(a%i==0)
y++;
if(y==0)
return(a);
else
return(0);}}

OUTPUT:

Variable description table(Program 8)


Variable Data type Description
S int Variable used to take input of
the starting limit
L int Variable used to take input of
final limit
c int Counter variable to store the
number of prime
combination triplets
i int Variable used in for-loop
a int Variable used in
parameterized function
primecheck()
y int Counter variable used to
store number of factors of a
k int Variable used to check if i is
prime or not and stores the
returned value of
primecheck()
t int To store the other prime
numbers in the combination
m int To store the other prime
numbers in the combination

PROGRAM 9
A class ​Linear​contains the 10 character array elements. Some of the data members/ member functions are
given below:
Class name:​ Linear
Data member/instance variable:
ln[ ]: array to store 10 elements of char datatype
sr : search value of char type
Member functions/methods:
intlinSearch(charsr1): to search for the element using Linear search and recursive technique and return 1 if
found otherwise returns -1
Specify the class ​Linear​giving details of the​ intlinSearch(int)​. Define the m
​ ain()​ function to create an object,
take input in main() and call the functions accordingly to enable task.
package record;
import java.util.*;
class Linear{
String a[]=new String[10];
int f=0;
public void main(){

//Linear obj=new Linear();


Scanner q1=new Scanner(System.in);
System.out.println("ENTER 10 ELEMENTS");
for(int i=0;i<10;i++){
a[i]=q1.next(); } //takes input

System.out.println("ENTER SEARCH ELEMENT");

String se=q1.next();

find(0,se);}
public int find(int i,String search){
if(i<10){
if(a[i].equals(search))
{
System.out.println(search+" FOUND AT "+ ++i); // output if search element is found
f=1;
return 1;
}
find(++i,search);}
else{
if(f==0){
System.out.println("SEARCH ELEMENT NOT FOUND"); // output if search element is found
return 0;
}}
return 1;}}
Output:

​Variable description table(Program 7)


Variable Data type Description
a[] String An array used to take input
from the user
f int A variable used to check if
the search element is found
or no
i int Variable used in for-loop
search String A variable used to take input
from the user

PROGRAM 10
Write a program to input a sentence and print only the double lettered word.
Input
Rabbits eat carrots from the green fields
Output:
Rabbits
Carrots
green

import java.util.*;
class doubleletter
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.nextLine()+" ";
int l=s.length();
String wd="";
System.out.println("The words with double letters are");
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch!=' ')
{
wd+=ch;}
else if(ch==' '){
int l1=wd.length();
for(int j=0;j<l1-1;j++)
{
if(wd.charAt(j)==wd.charAt(j+1))
{
System.out.println(wd);}
}wd="";}}}}

OUTPUT:
Variable Description table(Program 10)
Variable Data type Description
s String Variable used to store input from the
user
l int Variable used to store the length of
string s
wd String Variable used to store the double
lettered words in the sentence
ch char Variable used to extract each
character in s
l1 int To store length of wd
j int Variable used in for-loop

PROGRAM 11
Write a program to input a sentence in uppercase and encode it with the number of characters it has to be
encoded. Print the encoded sentence. (Assume the encode number =3)
Input
ZEBRA STRIPES
Output:
CHEUD VWULSHV
package RECORD;
import java.util.*;
class p11
{
public static void main()
{
Scanner j=new Scanner(System.in);
System.out.println("Enter sentence in uppercase");
String s=j.nextLine();
System.out.println("Enter encode number");
int c=j.nextInt();
int l=s.length();
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(Character.isUpperCase(ch))
{
if((ch+c)>90)
System.out.print((char)(64+(ch+c-90)));
else
System.out.print((char)(ch+c));
}
else
System.out.print(ch);
}
}
}
OUTPUT:

Variable Description Table(Program 11)


Variable Data type Description
s String To take input of the
sentence from the user
c int To take input of encode
number form the user
l int To store length of string s
i int Variable used in for-loop
ch char To extract char at i in s

PROGRAM 12
Write a program to inputmarks for 10 students and sort them using selection sort technique
import java.util.*;
public class selection_sort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
System.out.println("ENTER THE MARKS");
for(int i=0;i<10;i++)
{ a[i]=sc.nextInt();}
for(int i=0;i<10;i++)
{ int s=a[i];
int pos=i;
for(int j=i+1;j<10;j++)
{ if(a[j]<s)
{ s=a[j];pos=j;}}
a[pos]=a[i];
a[i]=s;
}
System.out.println("THE SORTED MARKS");
for(int k=0;k<10;k++)

System.out.println(a[k]+"\t");
}
}

OUTPUT:

Variable description table(Program 12)


Variable Data type Description
a[] int Variable used to take input from
the user
i int Variable used in for-loop
s int Variable used in sorting process
pos int Variable used in sorting process
j int Variable used in for-loop
k int Variable used in for-loop

PROGRAM 13
Write a program to initialize an integer array and use binary search technique to search the element in the
array.

import java.util.*;
class bubblesearch
{public static void main()
{
Scanner s=new Scanner(System.in);int a[]=new int[15];
System.out.println("Enter the 10 nos");
for(int i=0;i<10;i++){
a[i]=s.nextInt();}
System.out.println("Enter the search element ");
int sr=s.nextInt();
int l=0,u=9,m=0,flg=0;
while(l<=u)
{
m=(l+u)/2;
if(a[m]<sr)
l=m+1;
else if(a[m]>sr)
u=m-1;
else
{
System.out.println("Search element found at "+(m+1)+"th position");
flg=1;break;}
}if(flg==0)
System.out.println("Element not found");}}

Output:

Variable Description table(Program 13)


Variable Data type Description
i int Variable used in for-loop
a[] int An array used to take input from
the user
sr int Variable used to store the
searching element
l int Variable used in bubble sort
u int Variable used in bubble sort
m int Variable used in bubble sort
flg int Variable used in bubble sort

PROGRAM 14
Write a program to accept a sentence, find the longest word using String Tokenizer and sort the words in the
sentence using Bubble sort.

import java.util.*;
class bubb_sorT
{
public static void main()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a sentence");
String a=s.nextLine();
StringTokenizer a1=new StringTokenizer(a);
int b=0;String longest=" ";
while(a1.hasMoreTokens())
{String bc=a1.nextToken();
int c=bc.length();
if(c>b)
{longest=bc;
b=c;}}
System.out.println("The longest word is "+longest);
int l=a.length();
int c=1;String a2=a+" ";
for(int i=0;i<l;i++)
{char ch2=a2.charAt(i);
if(ch2==' ')
{c++;}}String v[]=new String[c];String str="";int y=0;
for(int i=0;i<=l;i++){
char ch=a2.charAt(i);
if(ch!=' ')
{str+=ch;}
else if(ch==' ')
{v[y]=str;y++;
str="";}
}for(int i=0;i<y;i++)
{
for(int j=0;j<y-i-1;j++)
{
if(v[j].compareTo(v[j+1])<0)
{
String temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
}
}
System.out.println("THE ARRAY IN DESCENDING ORDER::");
for(int i=0;i<y;i++)
System.out.println(v[i]);
}
}

OUTPUT:

Variable Description Table(Program 14)


Variable Data type Description
a String To input a sentence from
the user
a1 StringTokenizer To create string tokenizer
of a
b int To store the length of the
longest word after finding
it
longest String To store the longest word in
the string
bc String To store the stings which
have more tokens
c int To store length of bc
l int To store length of a
i int Variable used in for-loop
a2 String To store a by adding space
at the end
ch2 char To extract character at i of
a2
v[] String An array to store each
word of a
str String Variable used to extract
words of a
y int Counter variable for length
of v[]
ch char To extract character at i of
a2
temp int Variable used in bubble
sort
j int Variable used in for-loop

PROGRAM 15
Write a program to accept an array of n elements and delete an element by accepting the position of the
element to be deleted from the user.
import java.util.*;
class deleting{
public static void main(){
Scanner sc =new Scanner(System.in);
System.out.println("ENTER THE NUMBER OF ELEMENTS IN THE ARRAY");
int n=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n-1];
System.out.println("ENTER THE ELEMENTS FOR THE ARRAY");
for(int i=0;i<n;i++)
{a[i]=sc.nextInt();}
System.out.println("ENTER THE ELEMENT TO BE DELETED");
int p=sc.nextInt();
for(int i=0;i<n;i++){
if(a[i]==p)
{int pos=i;
if(pos<n-1)
{a[pos]=a[i+1];}
else if(pos==n){
for(int j=p;j<n-1;j++)
a[j]=a[j+1];}}}
System.out.println("array");
for(int i=0;i<n-1;i++)
System.out.println(a[i]);}}
Output:

Variable description table(Program 15)


Variable Data type Description
n int A variable used to take input for
number of elements in the array
a[] int An array to store the input from
user
b[] int An array to store the array after
deleting the element
i int A variable used in for-loop
p int Variable used to take input of the
element to be deleted
pos int A variable used in the process of
deleting
j int A variable used in for-loop

PROGRAM 16
A special number is a number in which the sum of the factorial of each digit is equal to the number itself.
For example: 145.
1! + 4! + 5! = 1 + 24 + 120 = 145.
Thus, 145 is a special number.
Design a class Special to check if a given number is a special number. Some of the members of the class are
given below:
Class name: Special
Data members:
n: to store the integer.
Member functions:
Special(): constructor to assign 0 to n.
Special(int): parameterized constructor to assign a value to ‘n’.
void sum(): calculate and display the sum of the first and last digit of ‘n’.
voidisSpecial(): check and display if the number ‘n’ is a special number.
Specify the class Special giving details of the constructors, void sum() and void isSpecial(). You need not
write the main() function.

import java.util.*;
class REC
{
public void main()
{
Scanner sc = new Scanner(System.in);
REC obj = new REC(); // creating object
System.out.println("Enter a number"); // taking input
int n = sc.nextInt();
int t = n; // substituting value of n in a temporary variable
int sum = 0;
while(t>0)
{
int d = t%10; // extracting digits
int a = obj.fact(d); // calling recursive function
sum += a; // finding sum of factorial of digits
t = t/10;
}
if(sum == n) // checking if sum of factorial of digits is equal to n
System.out.println(n+ " is a special number.");
else
System.out.println(n+ " is not a special number.");
}
int fact(int num) // finding factorial using recursion
{
if(num == 1)
return 1;
else
return num * fact(num - 1);
}
}
Output:

Variable description table(Program 16)


Variable Data type Description
n int Variable used to take input
from the user
t int To store n in a temporary
variable
sum int To calculate the sum of
factorial of each digit
d int To extract the digits of n
a int To store the digits factorial
num int Variable used in function
fact()

PROGRAM 17
Write a program to input an array of 10 integer elements and sort it using Insertion sort technique.
import java.util.*;
class INSERTION_SORT
{
public static void main()
{
Scanner s = new Scanner(System.in);
int a[] = new int[10];
System.out.println("ENTER 10 ELEMENTS");
for(int i=0;i<10;i++)
a[i] = s.nextInt();
for(int i=1;i<10;i++)
{
int t = a[i];
for(int j=i-1;j>=0&&t<a[j];j--)
{
int t1 = a[j];
a[j] = t;
a[j+1] = t1;
}
}
System.out.println(" THE RESULT ");
for(int i=0;i<10;i++)
System.out.println(a[i]);
}
}
OUTPUT:

Variable Description table(Program 17)


Variable Data type Description
a[] int An array used to take input
of 10 integer elements
i int Variable used in for-loop
t int Variable used in selection
sort process
j int Variable used in for-loop
t1 int Variable used in selection
sort process

PROGRAM 18
Write a program to input 2 D array of 4x4 order and:
i) Print the array in matrix form
ii) Print the sum of left diagonal
iii) Print the sum of right diagonal
iv) Print the sum of outer boundary elements of the matrix

import java.util.*;
class Sum_of_diagonals
{
public static void main()
{
Scanner s = new Scanner(System.in);
System.out.println("ENTER A 4x4 NUMBERS ");
int a[][] = new int[4][4];
for(int i = 0;i<4;i++)
{
for(int j = 0;j<4;j++)
a[i][j] = s.nextInt();
}
System.out.println("THE MATRIX");
for(int i = 0;i<4;i++)
{
for(int j = 0;j<4;j++)
System.out.print(a[i][j]);
System.out.println();
}
int ld = 0,rd = 0,o = 0;
for(int i = 0;i<4;i++)
ld += a[i][i];
for(int i = 0,j = 3;i<4;i++,j--)
rd += a[i][j];
for(int i = 0;i<4;i++)
{
for(int j = 0;j<4;j++)
{
if(i == 0||i ==3 ||j ==0|| j ==3)
o += a[i][j];
}
}
System.out.println("THE SUM OF LEFT DIAGONAL : "+ld);
System.out.println("THE SUM OF RIGHT DIAGONAL : "+rd);
System.out.println("THE SUM OF OUTER DIAGONAL : "+o);
}}

OUTPUT:

n
Variable description table(Program 18)
Variable Data type Description
a[][] int A double dimensional array
used to input 16 elements
from the user
i int A variable used in for-loop
j int A variable used in for-loop
ld int Variable used to calculate
sum of left matrix
rd int Variable used to calculate
sum of right diagonal
o int Variable used to calculate
sum of outer diagonal

PROGRAM 19
Write a program to input a word and check if it is a Palindrome using String Buffer package
LIBRAR_Functions;
import java.util.*;
class palindromee
{
public static void main()
{
Scanner s=new Scanner(System.in);
System.out.println("enter a word");
String sr=s.next();
StringBuffer a=new StringBuffer(sr);
String k=(a.reverse()).toString();
if(k.equals(sr))
System.out.println("String is a palindrome");
else
System.out.println("String is not a palindrome");
}
}
OUTPUT:
Variable Description table(Program 19)
Variable Data type Description
sr String A variable used to take
input from the user
a StringBuffer Used to create string buffer
of sr
k int To store reversed string

PROGRAM 20
Write a program to input a number and check if it is a Kaprekar number.
A Kaprekar number is a number whose square when divided into two parts and such that sum of parts is
equal to the original number and none of the parts has value 0.
Given a number, the task is to check if it is Kaprekar number or not.
Input : n = 45
Output : Yes
Explanation : 45​2​ = 2025 and 20 + 25 is 45

PROGRAM 21
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
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
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.
Input:
INPUT CODE: 0201530821
OUTPUT: VALID ISBN CODE
import java.util.*;
class ISBn
{
public static void main()
{
Scanner q=new Scanner(System.in);
System.out.println("ENTER ISBN CODE");
String in=q.next();
int l=in.length();
if(l!=10) //ISBN code is always of 10 digits
{
System.out.println("INVALID ISBN CODE");
System.exit(0);
}
int sum=0;
if(in.charAt(l-1)=='X')sum=10; // if the last digit is 'X' then 10 is added to the sum
int m=10;
for(int i=0;i<l;i++){
char c=in.charAt(i); //extracts the character at i
String d=""+c;
if(c=='X')
break;
else{
sum+=(Integer.parseInt(d))*m; //adds product the number and its position to the sum
m--;
}
}
if(sum%11==0) //output
System.out.println("VALID ISBN CODE");
else
System.out.println("INVALID ISBN CODE");
}
}
OUTPUT:
Variable Description Table(Program 21)
Variable Data type Description
in String To take input from the user
l int To store the no. of
characters in in
sum int To calculate sum of
product of digit and its
position
m int To store position of digit
i int Variable used if for-loop
c char Extracts character at i
d String Stores all the char c to a
string

PROGRAM 22
Write a program to input 2 D array of m x n order and find the transpose of the array.
A transpose of an array is obtained by interchanging the elements of rows and columns.
Print both the original matrix and the transpose matrix
import java.util.*;
class TRANSPOSE
{
public static void main()
{
int a[][]=new int[4][4];
int b[][]=new int[4][4];
Scanner km = new Scanner(System.in);
System.out.println("ENTER THE ELEMENTS");
int r=0;
for( r=0;r<4;r++)
{
for(int c=0;c<4;c++)
{
a[r][c]=km.nextInt();
}
}
for( r=0;r<4;r++)
{
for(int c=0;c<4;c++)
{
for( r=0;r<4;r++)
b[c][r]=a[r][c];
}
}
System.out.println("ORIGINAL ARRAY");
for(r=0;r<4;r++)
{
for(int c=0;c<4;c++)
System.out.print(a[r][c]);
System.out.println();
}
System.out.println("THE TRANSPOSE MATRIX");
for(int c=0;c<4;c++)
{
for( r=0;r<4;r++)
System.out.print(b[c][r]);
System.out.println();
}
}
}

OUTPUT:

Variable Description Table(Program 2​2)


Variable Data type Description
a[][] int A double dimensional array
used to take input
b[][] int A double dimensional array
used to take input
r int Variable used in for-loop
c int Variable used in for-loop
PROGRAM 23
Write a program to accept a sentence and insert “CS�? next to each vowel found in the
sentence.
Input: Computer Science
CoCSmpuCSteCSrSciCSeCSnceCS
import java.util.*;
class VOWEL
{
public static void main()
{
Scanner km = new Scanner(System.in);
System.out.println(" ENTER A SENTENCE ");
String str = km.nextLine();
int l = str.length();
String str2 = "";
for(char i=0;i<l;i++)
{
char STR = str.charAt(i);
str2+=STR;

if(STR=='A'||STR=='a'||STR=='E'||STR=='e'||STR=='I'||STR=='i'||STR=='O'||STR=='o'||STR=='U'||STR=='u')
str2+="CS";
}
System.out.println("OUTPUT: "+str2);
}
}
OUTPUT:
Variable Description Table(Program 23)
Variable Data type Descritpion
str String To take input from the user
l int To store the no. of
characters in str
str2 String To store STR
i int Variable used in for-loop
STR char To extract character of str
at i

PROGRAM 24
Write a program as per user’s choice to:-
i) Create two text files to store the name and mobile number of students.

ii) Read the name of the student and display the name along with the mobile number of the student

iii) Terminate the program.

PROGRAM 25
A class dec_Bin has been defined to convert a decimal number to its equivalent binary number. Some of the
members of the class are given below:
Class name :dec_Bin
Data members:
n : integer to be converted to its binary equivalent.
s : binary equivalent number.
i : incremental value of power.
Member functions:
dec_Bin() : constructor to assign initial values to data members.
void getdata() : accepts the value of n
void recursive(int ) : to calculate the binary equivalent of ‘n’ using Recursive Technique
voidputdata() : to display decimal number ‘n’ and its binary equivalent
Specify the class dec_Bingiving the details of the constructor and the functions void getdata(), void
recursive(int) and void putdata(). The main function need not be written.

import java.util.*;
class Bin_EqUI
{
int n;int s;int i;
public void input()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the number");
n=s.nextInt();
}
public void recu(int l)
{
if(l>0)
{
s=s+(l%2)*(int)Math.pow(10,i);
i++;
recu(l/2);
}
}
public void Pr()
{
recu(n);
System.out.println("Binary Equivalent="+s);
}
public static void main()
{
Bin_EqUI obj=new Bin_EqUI();
obj.input();
obj.Pr();
}}

OUTPUT:

Variable Description Table(Program 25)


Variable Data type Description
n int To take input of the number
from the user
s int To calculate and store the
binary equivalent of n
i int Counter variable
l int Variable used in
parameterized constructer
recu()

Вам также может понравиться