Project Computer
Project Computer
Project Computer
import java.io.*;
class Deci_to_octal
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a decimal number :");
int n=Integer.parseInt(obj.readLine());
int r=0,ro=0,d=0,oct=0;
while(n!=0)
{
r=n%8; //finding the remainder by dividing by 8
ro=ro*10+r; //adding the remainder to result
n=n/8;
}
while(ro!=0) //reverses the result to get the octal equivalent
{
d=ro%10;
oct=oct*10+d;
ro=ro/10;
}
System.out.println("Output :"+oct);
}
}
output:
Enter a decimal number :445
Output :675
Question 2
A sentence is terminated by either "."or"?"or"!" Input a piece of text
consisting of sentences.Write a program to print the length of the sentence
measured in words and the frequency of vowels in each sentence.
Sample input:
HELLO!HOW ARE YOU?HOPE EVERYTHING IS FINE.BEST OF LUCK.
Sample output:
Sentence No.of vowels No.of words
1 2 1
2 5 3
3 8 4
4 3 3
p=i+1;
a[k]=s1; //stores each sentence to an array
k++;
}
}
System.out.println("Sentence\tNo. of vowels\t No. of words");
for(int i=0;i<=k;i++) // for taking each sentence from the array
{
int l=a[i].length();
int v=0,w=0;
for(int j=0;j<l;j++) //for extracting each letter of the sentence
{
char ch=a[i].charAt(j);
if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||
(ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')) // checking
for the vowels
v++;
else if((ch==' ')||(ch=='.')||(ch=='?')||(ch=='!')) //counting the
no: of words
w++;
}
System.out.println((i+1)+"\t\t"+v+"\t\t"+w);
}
}
}
output:
Enter the sentence:HURRAY!WE WON THE MATCH.ARE YOU NOT HAPPY?
Sentence No. of vowels No. of words
1 2 1
2 4 4
3 6 4
Question 3
Write a program to check and print whether the entered number is happy number or
not.A happy number is a number in which the eventual sum of the digits of the
number is equal to 1
Example:28=22+ 82 = 4 +64 =68
68=62+ 82=36 +64 =100
100=12+02+02=1+0+0=1
import java.io.*;
class Happy_no
{
}
}
output:
Enter the number:19
19 is a happy no.
Question 4
Write a program to print the following pattern as per limit entered by the user
sample input:4
sample output:
AZ
ABYZ
ABCXYZ
ABCDWXYZ
import java.io.*;
class Pattern1
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the limit:");
int n=Integer.parseInt(obj.readLine());
for(int i=0;i<n;i++) //to set the number of rows
{
for(int j=0;j<=i;j++) //prints the alphabets from first
{
char ch=(char)(65+j);
System.out.print(ch);
}
for(int j=i;j>=0;j--) //prints the alphabets from last
{
char ch=(char)(90-j);
System.out.print(ch);
}
System.out.println();
}
}
}
output:
Enter the limit:5
AZ
ABYZ
ABCXYZ
ABCDWXYZ
ABCDEVWXYZ
Question 5
Write a program to remove the vowels from every word of a sentence and print
it.
Sample input:
Kerala is Gods own country.
Sample output:
Krl s Gds wn cntry.
QUESTION 6
Write a program to print the twin primes between a certain limit as per the users
choice.
Sample input:
1
20
Sample output:
5 3
7 5
13 11
19 17
import java.io.*;
class Twin_prime
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the limits:");
int n=Integer.parseInt(obj.readLine());
int m=Integer.parseInt(obj.readLine());
if(n==1)
n=n+1;
for(int i=n;i<(m-1);i++) //checks in the limit
{
int flag=0,f=0;
for(int j=2;j<i;j++) //checks if the number is prime
{
if(i%j==0)
flag++;
}
int k=i+2;
for(int j=2;j<k;j++) //checks if number+2 is prime
{
if(k%j==0)
f++;
}
if((flag==0)&&(f==0))
System.out.println(k+" "+i);
}
}
}
output:
Enter the limits:1
35
5 3
7 5
13 11
19 17
31 29
Question 7
A bank intends to design a program to display the denomination of an input amount,
up to 5 digits.The available denomination with the bank are of rupees
1000,500,100,50,20,10,5,2 and 1.
Design a program to accept the amount from user ,print the amount in words and
display the break-up in descending order of denomination.(preference should be
given to the highest denomination available) along with the total number of notes.
[Note: only the denomination used should be displayed]
example:
Sample input:14788
Sample output:
Amount in words:ONE FOUR SEVEN EIGHT EIGHT DENOMINATIONS
1000 X 14 = 14000
500 X 1 = 500
100 X 3 = 300
50 X 1 = 50
20 X 1 = 20
10 X 1 = 10
5 X 1 = 5
2 X 1 = 2
1 X 1 = 1
TOTAL NUMBER OF NOTES=23
import java.io.*;
public class Denomination
{
public static void main(String args[]) throws IOException
{
int rev=0,amount,amt,rem;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Amount:");
amount=Integer.parseInt(br.readLine());
amt=amount;
while(amt !=0) //reverses the amount
{
rev=rev*10+amt%10;
amt=amt/10;
}
System.out.print("Amount in words :");
String
amtw[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
while(rev!=0)
{
rem=rev%10;
System.out.print(amtw[rem]+" ");
rev=rev/10;
}
int den[]={1000,500,100,50,20,10,5,2,1};
int i=0, tot=0;
System.out.println();
System.out.println("DENOMINATIONS");
while (amount!=0) //checks for each note
{
rev=amount/den[i];
if(rev!=0)
{
System.out.println(den[i]+"\t X\t " + rev + "\t =\t" + rev*den[i]);
tot+=rev;
}
amount=amount%den[i];
i++;
}
System.out.println("TOTAL NUMBER OF NOTES= "+ tot);
}
}
output:
Enter the Amount:1288
Amount in words :ONE TWO EIGHT EIGHT
DENOMINATIONS
1000 X 1 = 1000
100 X 2 = 200
50 X 1 = 50
20 X 1 = 20
10 X 1 = 10
5 X 1 = 5
2 X 1 = 2
1 X 1 = 1
TOTAL NUMBER OF NOTES= 9
Question 8
Write a program in java to find the least common multiple(L.C.M.) of two numbers
entered by the user
import java.io.*;
class Lcm
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int a,b,max,min,x,lcm=1;
System.out.print("Enter the 1st number :");
a=Integer.parseInt(br.readLine());
System.out.print("Enter the 2nd number:");
b=Integer.parselInt(br.readLine());
if(a>b)
{
max=a;
min=b;
}
else
{
max=b;
min=a;
}
/*
To find the maximum and minimum numbers,you can also use
int max=a>b?a:b;
int min=a<b?a:b;
*/
for(int i=1;i<=min;i++)
{
x=max*i; //finding multiples of the maximum number
if(x%min==0) //Finding the multiple of maximum numberwhich is
divisible by the minimum number.
{
lcm=x;//making the first multiple of maximum number which is
divisible by the minimum number,as the LCM
break;//exiting from the loop,as we don't need anymore
checking after getting the LCM
}
}
System.out.printIn("L.C.M.="+lcm);
}
}
output:
Enter the 1st number :2
Enter the 2nd number:3
L.C.M.=6
QUESTION 9
Write a program to find the kaprekar number within the two given limits as per the
users choice.
A positive whole number 'n' that has 'd' number of digits is squared and split into
two pieces, a right-hand piece that has 'd' digits and a left-hand piece that has
remaining 'd' or 'd-1' digits.
If the sum of the two pieces is equal to the number,then 'n' is a Kaprekar number.
Example: 9
9x9=81
right-hand piece of 81=1
lefthand piece of 81=8
sum=8+1=9,i.e. equal to the number
Hence,9 is a kaprekar number.
import java.io.*;
class Kaprekar_num
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the two limits:");
int n=Integer.parseInt(obj.readLine());
int m=Integer.parseInt(obj.readLine());
for(int i=n;i<=m;i++)
{
int a=i,f=0,d=0;
while(a!=0)
{
f=a%10;
d++; //to store the number of digits
a=a/10;
}
}
}
output:
Enter the two limits:1
100
1,9,45,55,99,
Question 10
Write a program to check whether the palprime numbers in the given number.Palprime
is a
number which is both prime and palindrome.
Example:11
11 is both prime and palindrome
import java.io.*;
class Palprime
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the limits:");
int n=Integer.parseInt(obj.readLine());
int a=Integer.parseInt(obj.readLine());
System.out.println("The palprime numbers are:");
if(n==1)
n=n+1;
for(int k=n;k<=a;k++)
{
int flag=0,m=k,d=0,rev=0;
for(int i=2;i<k;i++) //to check for prime
{
if(k%i==0)
{
flag++;
break;
}
}
while(m!=0) //to check for palindrome
{
d=m%10;
rev=rev*10+d;
m=m/10;
}
if((flag==0)&&(rev==k))
System.out.print(k+",");
}
}
}
output:
Enter the limits:2
100
The palprime numbers are:
2,3,5,7,11,
Question 11
Write a program to check whether the inputted number is a smith number or not.A
Smith number is a composite number,the sum of whose digits is the sum of the digits
of its prime factors obtained as a result of prime factorization(excluding 1).The
first few such numbers are 4,22,27,58,85,94,121.....
Example:666
Prime factors are 2,3,3, and 37
Sum of the digits are (6+6+6)=18
Sum of the digits of the factors=(2+3+3+(3+7))=18
output:
Enter the number121
Sum of Digit= 4
Sum of Prime Factor= 4
It is a Smith Number
Question 12
Stack is a kind of data structure which can store elements with the restriction
that an element can be added or removed from the top only.
Write a program to illustrate Stack operations.
import java.io.*;
public class Stackop
{
int s[]=new int[20];
int sp,n;
Stackop(int nn)
{
for(int i=0;i<20;i++)
s[i]=0;
n=nn;
sp=-1;
}
void pushdata(int item) //pushinn in data
{
if(sp==(n-1))
System.out.println("Stack Overflows");
else
{
sp++;
s[sp]=item;
}
}
void popdata() //poping out data
{
int v;
if(sp==-1)
System.out.println("Stack Underflows");
else
{
v=s[sp];
System.out.println("popped out element is="+v);
sp--;
}
}
void display()
{
if(sp==-1)
System.out.println("Stack empty");
else
{
System.out.println("SP--->|"+s[sp]+"|");
for(int i=sp-1;i>=0;i--)
{
System.out.println(" |"+s[i]+"|");
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of stack:");
int nn=Integer.parseInt(stdin.readLine());
Stackop obj=new Stackop(nn);
for(int j=0;j<nn;j++)
{
System.out.print("Enter the element"+(j+1)+" to be pushed:");
int n1=Integer.parseInt(stdin.readLine());
obj.pushdata(n1);
}
obj.popdata();
System.out.println("Displaying stack elements");
obj.display();
}
}
output:
Enter the size of stack:4
Enter the element1 to be pushed:4
Enter the element2 to be pushed:9
Enter the element3 to be pushed:5
Enter the element4 to be pushed:2
popped out element is=2
Displaying stack elements
SP--->|5|
|9|
|4|
Question 13
Write a program to check and print whether the entered word is palindrome or not.
Sample input:
madam
Sample output:
It is a palindrome.
import java.io.*;
class Palindrome_word
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the word:");
String s=obj.readLine();
int n=s.length();
String s1="";
for(int i=n-1;i>=0;i--)
{
char ch=s.charAt(i); //reverses the word
s1=s1+ch;
}
if(s.equalsIgnoreCase(s1))
System.out.println("It is a palindrome");
else
System.out.println("It is not a palindrome");
}
}
output:
Enter the word:malayalam
It is a palindrome
Question 14
Write a program to enter a date of birth and check whether it is valid or not.Also
print which day of the year is the inputted date of birth
Sample input
05
05
2012
Sample output:Valid date
157
import java.io.*;
class Date_of_birth
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the date in dd/mm/yyyy:");
int d=Integer.parseInt(obj.readLine());
int m=Integer.parseInt(obj.readLine());
int y=Integer.parseInt(obj.readLine());
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
//array for number of days in a month
int s=0;
if(y%4==0) //leap year
a[1]=29;
if(m<=12) //checks validity of month
{
if(d<=a[m-1]) //checks validity of day
{
System.out.println("Valid date");
for(int i=0;i<m-1;i++) //calculates which date of the year
s=s+a[i];
s=s+d;
System.out.println(s+"rd day of the year");
}
else
System.out.println("Invalid date");
}
else
System.out.println("Invalid date");
}
}
output:
Enter the date in dd/mm/yyyy:
2
6
2013
Valid date
153rd day of the year
Question 15
Write a program to accept a sentence and arrange the words in descending order of
their number of letters and print it.
Sample input:
My name is Meera.
Sample output:
Meera name is My
p=i+1;
j++;
}
else
continue;
}
for(int i=0;i<j;i++)
{
n1=A[i].length();
for(int l=0;l<j-1;l++)
{
n2=A[l].length();
if(n1>n2) //sorting the words in descending order of their length
{
String temp=A[i];
A[i]=A[l];
A[l]=temp;
}
}
System.out.println("The descending order of words:");
for(int i=0;i<j;i++)
System.out.print(" "+A[i]);
}
}
output
Enter the sentence:My name is rohan.
The descending order of words:
rohan name is My
Question 16
Write a program to print the combinations for a three letter word.
Sample input:top
Sample output:
top
tpo
otp
opt
pto
pot
Question 17
Write a program to check whether the first letters of every word of a sentence is
palindrome or not.
sample input:
maya and delna are mascots.
sample output:
madam
It is a palindrome.
output:
Enter the sentence:MAYAPUR AND DIMNA ARE MIRACLES.
MADAM
It is a palindrome word
Question 18
Write a program to encrypt a decoded message by promoting each letter by two and
printing the same.don't encrypt the space.
Sample input:RFC ICWUMPB GQ BML
sample output:THE KEYWORD IS DON
import java.io.*;
class Encrypt2
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the sentence:");
String s=obj.readLine();
int l=s.length();
System.out.println("Encrypted sentence:");
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch=='Y'||ch=='z'||ch==' ')
{
if(ch=='Y')
System.out.print("A");
if(ch=='Z')
System.out.print("B");
if(ch==' ');
{
System.out.print(' ');
continue;
}
}
else
{
int n=(int)ch;
/*extracts the integer value of the letter*/
int e=n+2; //increments by two
System.out.print(""+(char)e);
/*converts integer value to character value and prints*/
}
}
}
}
output:
Enter the sentence:HOW ARE YOU
Encrypted sentence:
JQY CTG AQW
Question 19
Write a program to convert a number from decimal to binary equivalent and binary
to decimal equivalent.Also write the main function.
import java.io.*;
class Conversion
{
public int deci_binary(int n)
{
int m=n,b=0,bin=0,k=0;
while(m!=0)
{
int d=m%2;
b=b*10+d;
k++;
m=m/2;
}
while(k!=0)
{
int d1=b%10;
bin=bin*10+d1;
b=b/10;
k--;
}
return bin;
}
public int binary_deci(int n)
{
int i=0,m=n,deci=0;
while(m!=0)
{
int d=m%10;
deci=deci+(d*(int)Math.pow(2,i));
i++;
m=m/10;
}
return deci;
}
public static void main(String args[])throws IOException
{
Conversion ob=new Conversion();
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter '1' for conversion decimal to binary");
System.out.println("Enter '2' for conversion binary to decimal");
System.out.print("Enter your choice:");
int p=Integer.parseInt(obj.readLine());
switch (p)
{
case 1:
{
System.out.print("Enter the decimal number:");
int c=Integer.parseInt(obj.readLine());
int e=ob.deci_binary(c);
System.out.println("The binary equivalent is "+e);
}
break;
case 2:
{
System.out.print("Enter the binary number:");
int c=Integer.parseInt(obj.readLine());
int e=ob.binary_deci(c);
System.out.println("The decimal equivalent is "+e);
}
break;
default:
System.out.println("Invalid input");
}
}
}
output:
Enter '1' for conversion decimal to binary
Enter '2' for conversion binary to decimal
Enter your choice:1
Enter the decimal number:85
The binary equivalent is 1010101
Question 20
Write a program to print all the goldblack numbers below the inputed number.A
goldblack number is a nuber expressed as the sum of two prime numbers.example:47
import java.io.*;
class Goldblack
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number:");
int n=Integer.parseInt(obj.readLine());
int g=0;
for(int i=2;i<n/2;i++)
{
int flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
flag++;
}
int p=n-i;
int f=0;
for(int l=2;l<p;l++)
{
if(p%l==0)
f++;
}
if((flag==0)&&(f==0)&&(p!=0)&&(p!=1))
{
g++;
System.out.println(""+i+" "+p);
}
}
}
}
output:
Enter the number:50
3 47
7 43
13 37
19 31
Question 21
Write a program to print the following pattern as per limit entered by the user
Sample input:5
Sample output:
12345 54321
1234 4321
123 321
12 21
1 1
import java.io.*;
class Pattern2
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the limit:");
int n=Integer.parseInt(obj.readLine());
int k=n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
System.out.print(j);
for(int j=1;j<=i;j++)
System.out.print(" ");
for(int j=k;j>=1;j--)
System.out.print(j);
System.out.println();
k--;
}
}
}
output:
Enter the limit:4
1234 4321
123 321
12 21
1 1
Question 22
Write a program to convert a sentence into piglatin
sample input:
My name is Khan.
Sample output:
Myay amenay isay ankhay
}
}
outter: for(int i=0;i<=k;i++)
{
int a=0;
int l=w[i].length();
inner:for(int j=0;j<l;j++)
{
char ch1=w[i].charAt(j);
if((ch1=='a')||(ch1=='e')||(ch1=='i')||(ch1=='o')||(ch1=='u')||
(ch1=='A')||(ch1=='E')||(ch1=='I')||(ch1=='O')||
(ch1=='U'))
/*checking for vowel*/
{
String s2=w[i].substring(j,l)+w[i].substring(0,j)+"ay";
/*converts to piglatin form*/
System.out.print(s2);
a++;
break inner;
}
if(a==0)
System.out.print(w[i]+"ay");
System.out.print(" ");
}
}
}
output:
Enter the sentence:My name is rohan.
Myay amenay isay ohanray
Question 23
Write a program to enter a sentence and a number as per the users choice and print
the word which is of the same size of the number entered.
import java.io.*;
class Word_of_size
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the sentence:");
String s=obj.readLine();
int n=s.length();
System.out.print("Enter the number:");
int a=Integer.parseInt(obj.readLine());
int p=0;
System.out.print("The word with length "+a+" is: ");
for(int i=0;i<n;i++)
{
char ch=s.charAt(i);
if((ch==' ')||(ch=='.'))
{
String s1=s.substring(p,i);
p=i+1;
int l1=s1.length();
if(l1==a)
System.out.println(s1);
}
}
}
}
output:
Enter the sentence:Failures are the stepping stones of success.
Enter the number:6
The word with length 6 is: stones
Question 24
Write a program to print a number in words.
Sample input:101
Sample output:ONE HUNDRED AND ONE
import java.io.*;
public class Word_num
{
String word1(int x)
{
String s=" ";
switch(x)
{
case 1:
s="ONE";
break;
case 2:
s="TWO";
break;
case 3:
s="THREE";
break;
case 4:
s="FOUR";
break;
case 5:
s="FIVE";
break;
case 6:
s="SIX";
break;
case 7:
s="SEVEN";
break;
case 8:
s="EIGHT";
break;
case 9:
s="NINE";
break;
}
return s;
}
String word2(int x)
{
String s=" ";
switch(x)
{
case 2:
s="TWENTY";
break;
case 3:
s="THIRTY";
break;
case 4:
s="FOURTY";
break;
case 5:
s="FIFTY";
break;
case 6:
s="SIXTY";
break;
case 7:
s="SEVENTY";
break;
case 8:
s="EIGHTY";
break;
case 9:
s="NINETY";
break;
}
return s;
}
String word3(int x)
{
String s=" ";
switch(x)
{
case 0:
s="TEN";
break;
case 1:
s="ELEVEN";
break;
case 2:
s="TWELVE";
break;
case 3:
s="THIRTEEN";
break;
case 4:
s="FOURTEEN";
break;
case 5:
s="FIFTEEN";
break;
case 6:
s="SIXTEEN";
break;
case 7:
s="SEVENTEEN";
break;
case 8:
s="EIGHTEN";
break;
case 9:
s="NINETEEN";
break;
}
return s;
}
Question 25
A wondrous square is an n by n grid which fulfils the following conditions:
>It contains integers from 1 to n*n,where each integer appears only once.
>The sum of integers in any row or column must add upto 0.5 x n x((n2+1).
For example,the following grid is a wondrous square where the sum of each row or
column is 65 when n=5.
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Write a program to read n(2<=n<=10)and the values stored in these n by n cells and
output if the grid represents a wondrous square.
Also output all the prime numbers in the grid along with their row index and column
index as shown in the output.A natural number is said to be prime if it exactly has
two divisors.For example,2,3,5,11,......The first element of the given grid i.e. 17
is stored at row index 0 and column index 0 and the next element in the row i.e. 24
is stored at row index 0 and column index 1.
import java.io.*;
class Wondrous_Square
{
int arr[][],arr1[];;
int n,i,j,x=0,r,c;
int flag;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take()throws Exception
{
System.out.print("\nEnter the size of array(row and column same):");
n=Integer.parseInt(br.readLine().trim());
arr=new int[n][n];
arr1=new int[2*n];
System.out.println("\nEnter the value:");
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
arr[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("\nThe matrix is\n");
for(i=0;i< n;i++)
{
r=0;
c=0;
for(j=0;j< n;j++)
{
System.out.print(arr[i][j]+" ");
r=r+arr[i][j];
c=c+arr[j][i];
}
System.out.println();
arr1[x]=r;
arr1[x+n-1]=c;
x++;
}
for(i=0;i< x;i++)
{
if(arr1[i]!= 0.5 * n * (n*n + 1))
break;
}
if(i==x)
System.out.println("YES IT REPRESENTS A WONDROUS SQUARE.");
else
System.out.println("IT IS NOT A WONDROUS SQUARE.");
System.out.println("PRIME\tROW\tCOLUMN");
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
if(prime(arr[i][j]))
System.out.println(arr[i][j]+"\t"+i+"\t"+j);
}
}
}
private boolean prime(int no)
{
int index;
for(index=2;index< no;index++)
{
if(no%index==0)
break;
}
if(index==no)
return true;
else
return false;
}
public static void main(String args[])throws Exception
{
Wondrous_Square ob=new Wondrous_Square();
ob.take();
}
}
output:
Enter the size of array(row and column same):4
The matrix is
16 15 1 2
6 4 10 14
9 8 12 5
3 7 11 13
YES IT REPRESENTS A WONDROUS SQUARE.
PRIME ROW COLUMN
2 0 3
5 2 3
3 3 0
7 3 1
11 3 2
13 3 3
Question 26
Write a program to accept two strings.Display the new string by taking each
character of the 1st string from left to right and of 2nd string from right to
left.the letters should be taken alternatively from each string.Assume that the
length of both strings are same.
sample input:history
science
sample output:heicsnteoitrcys
import java.io.*;
class New_word
{
public static void main(String args[])throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the one string:");
String s1=obj.readLine();
int n=s1.length();
System.out.print("Enter the another string of same length:");
String s2=obj.readLine();
for(int i=0;i<n;i++)
{
int p=n-i-1;
char ch1=s1.charAt(i);
char ch2=s2.charAt(p);
System.out.print(""+ch1+""+ch2);
}
}
}
output:
Enter the one string:COMPUTER
Enter the another string of same length:CHEMISTRY
CROTMSPIUMTEEHRC
Question 27
Write a program to cancel the repetitions of letters that occur in a word in a
sentence.
Sample input:
Myyy nameeee is Rajuuuuuu.
Sample output:
My name is Raju.
for(int i=0;i<l-1;i++)
{
ch1=s.charAt(i); //extracting the first character
ch2=s.charAt(i+1);//extracting the next character
//adding the first character to the result if the current and the next
characters are different
if(ch1!=ch2)
{
ans=ans+ch1;
}
}
System.out.println("word after removing repeated characters:"+ans);
}
}
output:
Enter the sentence:jaaavaaa iiisss.
word after removing repeated characters:java is
Question 28
Write a program on Java to input a number in decimal numbersystem and convert it
into
its equivalent number in the Hexadecimal number system.Hexadecimal Number system is
a number
system which can represent a number in any other number system in terms of digits
ranging from
0 to 9 and then A-F only.This number system consists of only sixteen basic digits
i.e. 0,1,
2,3,4,5,6,7,8,9,A,B,C,D,E and F.Here 10 is represented as A,11 as B and so on till
15 which is
represented as F.Far example :47 in hexa decimal number system can be represented
as 2F in the
Hexadecimal number system.
import java.io.*;
public class DecimalToHexa
{
public static void main(String args[])throws Exception
{
InputStreamReader isr =new InputStreamReader(System.in);
BufferedReader stdin=new BufferedReader(isr);
System.out.print("Enter the number in decimal number system:");
int n=Integer.parseInt(stdin.readLine());
int r;
String s=""; //variable to store the result after
//sorting the digits as in hexadecimal number system
char
dig[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(n>0)
{
r=n%16; //finding the remainder
s=dig[r]+s; //adding the remainder to the result
n=n/16;
}
System.out.print("Output="+s);
}
}
output:
Enter the number in decimal number system:2699
Output=A8B
Question 29
Write a program on Java to print the following pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
import java.util.*;
public class PatternN2
{
public static void main(String args[])
{
int i,j,n;
Scanner kb=new Scanner(System.in);
System.out.print("Enter n(below 9) : ");
n=kb.nextInt();
System.out.println();
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
System.out.print(" ");
for(j=1;j<=i;j++)
System.out.print(j+" ");
for(j=i-1;j>0;j--)
System.out.print(j+" ");
System.out.println();
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=i;j++)
System.out.print(" ");
for(j=1;j<=n-i;j++)
System.out.print(j+" ");
for(j=n-1-i;j>0;j--)
System.out.print(j+" ");
System.out.println();
}
}
}
output:
Enter n(below 9) : 4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
Question:30
Queue is a linear data structure which follows FIFO(FIRST IN FIRST OUT) rule.Write
a program to show insertion and Deletion in a Queue.
import java.io.*;
public class Queueop
{
int q[]=new int[20];
int f,r,size;
Queueop(int nn)
{
for(int i=0;i<20;i++)
q[i]=0;
size=nn;
r=-1;
f=-1;
}
void insertqueue(int item)
{
if(r==(size-1))
System.out.println("Queue Overflows");
else
{
if(f==-1&&r==-1)
{
f=0;
r=0;
}
else
r=r+1;
q[r]=item;
}
}
void deletequeue()
{
int v;
if(f==-1&&r==-1)
System.out.println("Queue Underflows");
else
{
v=q[f];
System.out.println("Deleted element is="+v);
if(f==r)
{
f=-1;
r=-1;
}
else
f=f+1;
}
}
void display()
{
System.out.println("Elements of the queue");
for(int j=f;j<=r;j++)
{
System.out.println(q[j]);
}
}
public static void main(String args[])throws IOException
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of queue:");
int nn=Integer.parseInt(stdin.readLine());
Queueop obj=new Queueop(nn);
for(int j=0;j<nn;j++)
{
System.out.print("Enter the element"+(j+1)+" to be inserted:");
int n1=Integer.parseInt(stdin.readLine());
obj.insertqueue(n1);
}
obj.deletequeue();
obj.display();
}
}
output:
Enter the size of queue:3
Enter the element1 to be inserted:4
Enter the element2 to be inserted:7
Enter the element3 to be inserted:5
Deleted element is=4
Elements of the queue
7
5