0% found this document useful (0 votes)
185 views

Null PDF

The program uses a switch statement to provide a menu with two options: 1) check if a number is a Disarium number or not and 2) check if a number is a Duck number or not. It accepts a number from the user, checks the conditions for the selected option, and prints the result. Key variables include num to store the input number, copy for manipulating the number, sum to calculate the sum of digits, and c to count zeros.

Uploaded by

Manju Yadav
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)
185 views

Null PDF

The program uses a switch statement to provide a menu with two options: 1) check if a number is a Disarium number or not and 2) check if a number is a Duck number or not. It accepts a number from the user, checks the conditions for the selected option, and prints the result. Key variables include num to store the input number, copy for manipulating the number, sum to calculate the sum of digits, and c to count zeros.

Uploaded by

Manju Yadav
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/ 35

Question 1:

Using the switch statement, write a menu driven program to perform following operations:
(i) To print the value of Z where Z = X3+0.5X− where x ranges from –10 to 10 with an increment of 2
and Y remains constant at 5.5.
(ii) To print the Floyds triangle with N rows
Example: If N = 5,
Output:
1
23
456
7 8 9 10
11 12 13 14 15

import java.util.*;
class program1
{
public static void main(String args[])
{
int rows, number = 1, counter, j,ch;
double x,z,y=5.5;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 to display value of function and 2 to print Floyd's triangle:");
ch= sc.nextInt();
switch(ch)
{
case 1:
for(x= -10; x<=10; x+=2)
{
z= Math.pow(x,3)+0.5*x -y;
System.out.println("Z="+z);
}
break;
case 2:
System.out.println("Enter the number of rows for floyd's triangle:");
rows= sc.nextInt();
System.out.println("Floyd's triangle");
System.out.println("****************");
for ( counter = 1 ; counter <= rows ; counter++ )
{
for ( j = 1 ; j <= counter ; j++ )
{
System.out.print(number+" ");
number++;
}
System.out.println();
}

break;
default: System.out.println("Wrong choice...");
}}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


rows int To store no. Of rows in the pattern
number int Counter variable
counter int Loop control variable
j int Loop control variable
ch int To input choice
x double To store the value of coefficient of the expression
y double To store the value of coefficient of the expression
z double To store the value of the function
Question 2:
Write a Java program to accept a word in uppercase and remove all the repetition of letters in it. The last
occurrence of each letter should only be retained.
Enter a word in upper case: ATTENDANCE
TDANCE
Enter a word in lower case: ABSENT
ABSENT

import java.util.*;
public class program2
{
public static void main(String args[])
{
String s1, st="", s2="";
char x=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
s1= sc.nextLine();
s1=s1.toUpperCase();
for(int i= s1.length()-1; i>=0; i--)
{
st= st +s1.charAt(i);
}
for(int i=0;i<st.length()-1;i++)
{
x=st.charAt(i);
if(st.indexOf(x)!=st.lastIndexOf(x))
{
for(int j=i+1;j<st.length();j++)
{
char y=st.charAt(j);
if(x==y)

st=st.substring(0,j)+st.substring(j+1,st.length());
}
}
}
for(int i= st.length()-1; i>=0; i--)
s2+= st.charAt(i);
System.out.println(s2);
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


st String To store the reverse of the string
s1 String To input the String
s2 String To store the final output
x char To extract character
i int Loop control variable
j int Loop control variable
y char To extract character
Question 3:

A tech number has even number of digits. If the number is split in two equal halves, then the square of sum of
these
halves is equal to the number itself. Write a program to generate and print all four digits tech numbers. Example:
Consider the number 3025. Square of sum of the halves of 3025 = (30 + 25)2= (55)2= 3025 is a tech number.

import java.util.*;
public class program3
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int k=n,c=0,f=0,fs=0,ls=0,t=0,num=0;
while(k>0)
{
c=c+1;
k=k/10;
}
if(c%2==0)
{
c=c/2;
f=(int)Math.pow(10,c);
fs=n%f;
ls=n/f;
t=fs+ls;
num=(int)Math.pow(t,2);
if(num==n)
System.out.println("Tech Number");
else
System.out.println("Not a Tech Number");
}
else
{
System.out.println("Not a Tech Number");
}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


k int To store the backup of the number
n int To input a number
c int To store number of digits
f int Intermediate variable
fs int To store half of the number
ls int To store another half of the number
t int To store the sum of both the halves
num int To calculate the square of the sum of both the halves
Question 4:
Write a menu driven program in Java to input a number and check whether it is a:
(a) DISARIUM number or not (b) DUCK number or not
Note:
DISARIUM Number:
A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the
original number. For example 135 is a DISARIUM
For example: 11+32+53= 135, some other DISARIUM are 89, 175, 518 etc.
DUCK Number:
A Duck number is a number which has zeroes present in it, but there should be no zero present in the beginning of
the number.
For example: 3210, 7056, 8430709 are all Duck numbers whereas 08237 is not.

import java.util.*;
public class program4 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int copy , d = 0, sum = 0,ch,num,p,c=0;
System.out.println("Enter 1 to check Disarium number and 2 to check Duck Number");
ch= sc.nextInt();
System.out.print("Input a number : ");
num = sc.nextInt();
switch(ch)
{
case 1:String s = Integer.toString(num);
copy = num;
int len = s.length();
while(copy>0)
{
d = copy % 10;
sum = sum + (int)Math.pow(d,len);
len--;
copy = copy / 10;
}
if(sum == num)
System.out.println("Disarium Number.");
else
System.out.println("Not a Disarium Number.");
break;
case 2:
while(num>0)
{
p= num%10;
if(p==0)
c++;
num/=10;
}
if(c>0)
System.out.println("Duck Number.");
else
System.out.println("Not a Duck Number.");
break;
default:System.out.println("Wrong choice");
}

}
}

Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


copy int To store the copy of the number
d int To extract the digit
sum int To store the sum
ch int To input choice
num int To accept a number
p int To extract the digit
c int To count number of zeros
len int To store the length of the number
s String To store the number in String format
Question 5:

Write a Java program to accept a sentence and count the number of palindromes in it.
Enter a Sentence: Nitin Arora uses only liRIL soap
Number of Palindromes=3

import java.util.*;
class Palindrome{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter a string:");
String str, wrd="", rev="";
int count=0,i,j;
str=sc.nextLine();
str=str+' ';
for( i=0;i < str.length();i++){
char c= str.charAt(i);
if(c!=' ')
wrd+=c;
else
{
int len= wrd.length();
for(j=len-1; j>=0; j--)
{
rev= rev+ wrd.charAt(j);
}
if(wrd.equalsIgnoreCase(rev)==true)
count++;
rev="";
wrd="";
}
}

System.out.println("NUMBER OF PALINDROMES: "+count);


}
}

Output:
VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


str String To store the String
rev String To store the reverse of the word
wrd String To extract word
i int Loop control variable
j int Loop control variable
c char To extract each character
count int To count the number of palindromes
Question 6:
Write a program to accept a sentence and print it in toggled case.
Input : Laugh and the world laughs with you, Weep, and you weep alone.
Output: lAUGH AND THE WORLD LAUGHS WITH YOU, wEEP, AND YOU WEEP ALONE.

import java.util.*;
public class toggle
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String str, str1="";
int i,l;
System.out.println("Enter a string:");
str= sc.nextLine();
l= str.length();
for(i=0; i<l; i++)
{
char c=str.charAt(i);
if(Character.isUpperCase(c))
str1+= Character.toLowerCase(c);
else
str1+= Character.toUpperCase(c);
}
System.out.println("Sentence after toggling:"+str1);
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


str String To store the String
i int Loop control variable
l int To find the length of the String
Str1 String To store the String in toggled form
Question 7:
Write a Java program to accept a name and print it in initialised form as follows.
Enter a name: Henry Louis Vivian Derozio
H. L.V.Derozio
import java.util.*;
public class initials
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String str, str1="";
int i,l;
System.out.println("Enter a name:");
str= sc.nextLine();
str=' '+str;
l= str.length();
int p= str.lastIndexOf(' ');
for(i=0; i<l; i++)
{
char c=str.charAt(i);
if(c==' ')
{
if(i!=p)
str1+= str.charAt(i+1)+".";}

}
str1= str1+ str. substring(p+1);
System.out.println("Initials of name-"+str1);
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


str String To store the name
i int Loop control variable
l int To find the length of the String
p int To store the last index of space
c char To extract each character
Str1 String To store initials of the name
Question 8:
An equation of the form ax2 + bx + c=0 is called a quadratic equation, where a, b and c are constants and x is a
variable. b2 – 4ac is called the discriminant (d) of the equation as it is used to discriminate between the nature of
its
roots as follows.
d=b2–4ac Nature of roots
b2–4ac<0 Imaginary Roots
b2–4ac=0 Real and Equal
b2–4ac>0 Real and Unequal
Design a program that accepts the values of a, b and c and calculates the real roots of the quadratic equation using
the formulae

r1, r2 =
If the roots are imaginary the program displays a message and terminates.

import java.util.*;
public class Quadratic {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
double a , b , c, root1, root2, determinant;
System.out.println("Enter the coefficients of quadratic expression:");
a= sc.nextDouble();
b= sc.nextDouble();
c=sc.nextDouble();
determinant = b * b - 4 * a * c;
if(determinant > 0) {
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.println("Root1 = "+root1);
System.out.println("Root2 = "+root2);
}
else if(determinant == 0) {
System.out.println("Roots are Real and Equal");
}
else {
System.out.println("Roots are imaginary");
}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


a double To store the value of coefficient
b double To store the value of coefficient
c double To store the value of coefficient
root1 double To store the value of first root
Root2 double To store the value of second root
determinant double To store the value of determinant
Question 9:
Write a program to accept words in mixture of cases from the user and count the number of palindromes in them
until the user enters STOP (in upper case). The program should display the count of palindromes along with the
longest palindrome entered by the user (if any). Assume no two palindromes entered would have the same length.
Examples:
INPUT 1
Hah
I
knew
you
would
surely
attempt
this
question
remember
the
Malayalam
program
STOP
OUTPUT 1
Number of Palindromes: 3
Longest Palindrome: Malayalam

import java.util.*;
public class program9 {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
String wrd, rev="";
int i, l, count=0,max=0;
String maximum="";
System.out.println("Enter words and type stop to exit");
do
{
System.out.println("Enter word:");
wrd=sc.next();
for(i=wrd.length()-1;i>=0; i--)
{rev+= wrd.charAt(i);}
if(wrd.equalsIgnoreCase(rev)==true){
count++;
l=wrd.length();
if(l>max)
{
max=l;
maximum=wrd;}
}
rev="";
}while(wrd.equalsIgnoreCase("stop")!=true);
System.out.println("No. of Pallindromic words="+count);
System.out.println("Longest Palindrome="+maximum);
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


wrd String To extract word of the String
rev String To find the reverse of the word
maximum String To store the maximum length palindromic word
i int Loop control variable
l int To find the length of the String
count int To count no. Of palindromic words
max int To store the length of the maximum length palindromic word
Question 10:

Design a menu driven program to calculate the amount after applying simple/compound interest on a given sum of
money. The rate of interest (for SI as well as CI) is as follows:
Time Period Rate
Upto 12 months 6.25%
More than 12 months and upto 36 months 7.25%
More than 36 months 7.00%
The time of deposit and principal is accepted from the user.
For SI, A=P 1+ For CI, A= P 1 +

import java.util.*;
public class program10
{
public static void main(String args[ ])
{
Scanner sc= new Scanner(System.in);
int ch;
double si, ci, p, t, amt;
System.out.println("Enter the principal amount and time in months:");
p= sc.nextDouble();
t=sc.nextDouble();
System.out.println("Enter 1 for Simple Interest and 2 for Compound Interest:");
ch= sc.nextInt();
switch(ch)
{
case 1: if(t<=12)
si= (p*t*6.25)/100;
else if(t>12 && t<=36)
si= (p*t*7.25)/100;
else
si= (p*t*7.00)/100;
amt= p+ si;
System.out.println("Amount after simple interest="+amt);
break;

case 2:
if(t<=12)
amt= p*(double)Math.pow((1+(6.25/100)),t);
else if(t>12 && t<=36)
amt= p*(double)Math.pow((1+(7.25/100)),t);
else
amt= p*(double)Math.pow((1+(7.00/100)),t);
System.out.println("Amount after compound interest="+amt);
break;

default:System.out.println("Wrong choice");
}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


ch int To input choice
p double To store the Principal amount
t double To store time in months
amt double To calculate the amount
ci double To store compound interest
si double To store simple interest
Question 11:
Write a program to accept a multi digit number and form a new number with its non prime digits.
Example Input: 294556 Output: 946 , Input: 3752 Output: 0

import java.util.*;
public class program11 {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
int i,n, p, count=0,max=0;
String str="";
System.out.println("Enter a multi digit number:");
n=sc.nextInt();
while(n>0)
{
p=n%10;
count=0;
for(i=1; i<=p; i++)
{
if(p%i==0)
count++;
}
if(count!=2)
str=p+str;
n= n/10;
}
System.out.println("Number formed with nonprime digits="+str);
}
}

Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


i int Loop control variable
n int To store a multidigit number
p int To extract the digits of the number
str String To create a no with non prime digits
count int To count no. Of factors
Question 12:
Design a program to accept the age of road accident victims and do the following.
i) Print the number of victims in age group below 30, 31 to 50 and above 50.
ii) Print the age profile with highest number of casualties.
iii) Print the average age of victims taking together all casualties into consideration
The program should continuously take inputs till the user enters the age of victim as 0.

import java.util.*;
public class program12 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int i, l, count=0,a1=0, a2=0, a3=0, n, max;
double avg1, avg2, avg3;
String maximum;
System.out.println("Enter age of road accident victims and type 0 to exit");
do
{
System.out.println("Enter age:");
n=sc.nextInt();
count++;
if(n<=30)
a1++;
else if(n>30 && n<=50)
a2++;
else
a3++;
}while(n!=0);
if(a1>a2 && a1> a2){
max=a1;
maximum=" age group of below 30";}
else if(a2>a1 && a2> a3){
max= a2;
maximum=" age group of 31 to 50";}
else{
max=a3;
maximum="age group of above 50";}
avg1=(double)a1/(a1+a2+a3);
avg2=(double)a2/(a1+a2+a3);
avg3=(double)a3/(a1+a2+a3);
System.out.println("No. of victims in the age group of below 30="+a1);
System.out.println("No. of victims in the age group of 31 to 50="+a2);
System.out.println("No. of victims in the age group of above 50="+a3);
System.out.println("age profile with highest number of casualties="+maximum);
System.out.println("average age of victims age group of below 30="+avg1);
System.out.println("average age of victims age group of 31 to 50="+avg2);
System.out.println("average age of victims age group of above 50="+avg3);
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


count int To count total no. Of persons
a1 int To store no. of victims in the age group of below 30
a2 int To store no. of victims in the age group of 31 to 50
a3 int To store no. of victims in the age group of above 50
avg1 double To store average age of victims age group of below 30
avg3 double To store average age of victims age group of above 50
maximum int To store age profile with highest number of casualties
avg2 double To store average age of victims age group of 31 to 50
n int To input age
max String To store the value of age profile with highest number of casualties
Question 13:

Write a program to accept a word (in upper case) and check if it is a unique letter word. A unique letter word is one
that does not have any repetition of letters.
Examples: QUBIT is a unique letter word
PROGRAM is not unique letter word

import java.util.*;
public class program13
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
str = str.toUpperCase();
boolean isUnique = true;
int len = str.length();

for (int i = 0; i < len; i++) {

char ch = str.charAt(i);

for (int j = i + 1; j < len; j++) {


if (ch == str.charAt(j)) {
isUnique = false;
break;
}
}

if (!isUnique)
break;
}
if (isUnique)
System.out.println("Unique String");
else
System.out.println("Not Unique String");
}
}

Output:
VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


i int Loop control variable
j int Loop control variable
str String To input String
isUnique boolean To check whether the String is unique or not
len int To store length of the String
ch char To extract each character of the String
Question 14:

Write a program to remove the repetition of words from a user input sentence entered in upper case.
INPUT: SHE SELLS SEA SHELLS ON THE SEA SHORE
OUTPUT: SHE SELLS SEA SHELLS ON THE SHORE

INPUT: TWO AND TWO MAKES TWENTY TWO


OUTPUT: TWO AND MAKES TWENTY

import java.util.*;
class program14
{
public static void main(String[] args)
{
String input="";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence: ");
input= sc.nextLine();
String[] words=input.split(" ");
for(int i=0;i<words.length;i++)
{
if(words[i]!=null)
{

for(int j=i+1;j<words.length;j++)
{

if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}
for(int k=0;k<words.length;k++)
{
if(words[k]!=null)
{
System.out.print(words[k] + " ");
}

}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


input String To input a String
i int Loop control variable
j int Loop control variable
Words[] String Array to store words of the String
Question 15:
Write a program to accept two words (in any case) and check if they are anagrams.
e.g. Listen and Silent are anagrams because they consist of the same set of English letters
that is e, i, l, n, s and t.

import java.util.*;

public class program15


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the First String : ");
String s1 = sc.nextLine();

System.out.print("Enter the second String : ");


String s2 = sc.nextLine();

if (checkAnagram(s1, s2))
System.out.println(s1 + " and " + s2 + " are Anagrams");
else
System.out.println(s1 + " and " + s2 + " are NOT Anagrams");

}
public static boolean checkAnagram(String s1, String s2)
{
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
if (s1.length() != s2.length())
return false;
else
{
for (int i = 0; i < s1.length(); i++)
{
for (int j = 0; j < s2.length(); j++)
{
if (s1.charAt(i) == s2.charAt(j))
{
s2 = s2.substring(0, j) + s2.substring(j + 1);
break;
}
}
}

if (s2.length() == 0)
{
return true;
} else
{
return false;
}
}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


S1 String To input the String
S2 String To input the String
i int Loop control variable
j int Loop control variable
Question 16:

Write a Java program to accept a sentence in uppercase and count the frequency of each letter present in it.
Enter a sentence in upper case: DO OR DIE
D--->2
E--->1
I--->1
O--->2
R--->1

import java.util.*;
public class program16
{
public static void main(String args[])
{
int c=0,f=0;
char x;
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
st=st.toUpperCase();
System.out.println("Character\tFrequency");
for(char i='A';i<'Z';i++)
{
f=0;c=0;
for(int j=0;j<st.length();j++)
{
x=st.charAt(j);
if(x==i)
{
f=1;
c++;
}
}
if(f==1)
System.out.print(" "+i+"\t \t"+c+"\n");
}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


c int To count frequency of characters
f int Flag variable
x char To extract the characters
i char Loop control variable
st String To input the String
Question 17:

A strong password refers to a string of minimum length 8, having combination of characters consisting of
uppercase
letters, lower case letters, digits and other characters (excluding blank space character), otherwise it is said to be a
weak password. Write a program to accept a password string from the user and classify it as strong or weak. If the
user enters a blank space character at any position of the password string or a string of length less than 8, then
display a message as INVALID INPUT!

import java.util.*;
public class program17 {
public static final int PASSWORD_LENGTH = 8;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("Input a password : ");
String s = sc.nextLine();
if(s.length()<8)
System.out.println("Password is invalid " );
else
{
if (is_Valid_Password(s)) {
System.out.println("Password is Strong " );
} else {
System.out.println("Password is Weak");
}

}}

public static boolean is_Valid_Password(String password) {

if (password.length() < PASSWORD_LENGTH) return false;

int charCount = 0;
int numCount = 0;
int sp=0;
int l=0, u=0;
for (int i = 0; i < password.length(); i++) {

char ch = password.charAt(i);

if (is_Numeric(ch)) numCount++;
else if (is_Letter(ch)) {
if (Character.isUpperCase(ch)== true) u++;
else l++;
charCount++;}
else if (Character.isWhitespace(ch)==false) sp++;
else return false;
}
return (password.length()>=8 && charCount >= 1 && numCount >= 1 && sp>=1 && l>=1 && u>=1 );
}
public static boolean is_Letter(char ch) {
ch = Character.toUpperCase(ch);
return (ch >= 'A' && ch <= 'Z');
}

public static boolean is_Numeric(char ch) {

return (ch >= '0' && ch <= '9');


}

Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


PASSWORD_LENGTH int To store fixed length of password
s String To accept the password from the user
charCount int To count number of letters
numCount int To count number of digits
s int To count number of blank spaces
u int To count number of uppercase letters
l int To count number of lower case letters
i int Loop control variable
ch char To extract each character of the password
Question 18:
A number is called Automorphic number if and only if its square ends in the same digits as the number itself. Write
a program to input a number and check if it is an automorphic number. e.g. 76, (76)2=5776, 5776 ends in 76.
import java.util.*;
public class program18 {

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.print("Input a number : ");
int num = sc.nextInt();
int sq_num = num*num;

String str_num = Integer.toString(num);


String square = Integer.toString(sq_num);

if(square.endsWith(str_num))
System.out.println("Automorphic Number.");
else
System.out.println("Not an Automorphic Number.");
}
}

Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


num int To input a number
sq_num int To store the square of the number
str_num String To store the number in String format
square String To store the square of the number in String format
Question 19:
Write a program to accept a decimal number (only positive whole numbers) and convert it into binary number.
INPUT: 12
OUTPUT: 1100

import java.util.*;
class program19
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number: ");
int num, p;
String s="";
num= sc.nextInt();
if(num<0)
System.out.print("Number is invalid");
else{
while (num >0)
{
p = num % 2;
s=p+s;
num /= 2;
}
System.out.print("\nBinary representation is:"+s);
System.out.println();}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


num int To store the decimal no
p int To store intermediate result
s String To store equivalent binary no
Question 20:
A library issues books on a rental basis at a 2% charge on the cost price of the book per day. As per the rules of the
library, a book can be retained for 7 days without any fine. If the book is returned after 7 days, a fine will also be
charged for the excess days as per the chart given below:
Number of excess days Fine per day
1 to 5 2.00
6 to 10 3.00
Above 10 days 5.00
Design a class Library to accept the cost of the book and the number of days the book was kept by the borrower,
compute the fine payable accordingly

import java.util.*;
class Library
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int day,cost,d;
double fine,ch;
System.out.println("Enter the cost price of the book: ");
cost=sc.nextInt();
System.out.println("Enter number of days: ");
day=sc.nextInt();
if(day<=7)
{
ch= (cost*0.02)*day;
System.out.println("Rental Charge="+ch);}
else{
d= day-7;
if(d>=1 && d<=5)
fine=d * 2.00;
else if(d>=6 && d<=10)
fine=(5 * 2.00)+((d-5) * 3.00 );
else
fine=(5 * 2.00) + (5 * 3.00) + ((d-10) * 5.00);
ch= (cost*0.02)*7;
System.out.println("Fine for "+day+" days is Rs."+(fine+cost));
}
}
}
Output:

VARIABLE DESCRIPTION TABLE:

VARIABLE NAME DATA TYPE PURPOSE


day int To store number of days, the book is borrowed for.
cost int To store the cost price of the book
d int To store the number of excess days
fine double To calculate fine
ch double To store the total charge to be given

You might also like