Sample Practical File2
Sample Practical File2
SOURCE CODE:
import java.util.Scanner;
class HarshadNumber
int n = sc.nextInt();
int c = n, d, sum = 0;
while(c>0)
d = c%10;
sum = sum + d;
c = c/10;
if(n%sum == 0)
else
}
OUTPUT:
SOURCE CODE:
import java.util.Scanner;
class GCD
int n1=sc.nextInt();
int n2=sc.nextInt();
int r;
while(n2!=0)
r=n1%n2;
n1=n2;
n2=r;
System.out.println("GCD="+n1);
}
OUTPUT:
SOURCE CODE:
import java.util.Scanner;
class AutomorphicNumber
System.out.println("Enter a number");
while(temp>0)
c++;
temp=temp/10;
if(num == lastSquareDigits)
System.out.println("Automorphic number");
else
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
num int To accept the number given by user. main()
4. A special two-digit number is such that when the sum of the digits is added to
the product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.Sum of digits = 5+9=14 Product of its digits = 5
x 9 = 45 Sum of the digits and product of digits = 14 + 45 = 59.
Write a program to accept a two-digit number. Add the sum of its digits to the
product of its digits. If the value is equal to the number input, output the message
“special-two digit number” otherwise, output the message “Not a special two-
digit number”.
SOURCE CODE:
import java.util.Scanner;
class Special2digitNumber
int product=1,sum=0,t=0;
int n=sc.nextInt();
product=(n%10)*(n/10);
sum=(n%10)+(n/10);
t=product+sum;
if(t==n)
else
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
product int To calculate the product of the digits main()
5. Write a program to input a number. Count and print the frequency of each digit
present in that number. The output should be given as: Sample Input: 44514621
Sample Output: =====================
Digit Frequency
=====================
1 2
2 1
4 3
5 1
6 1
SOURCE CODE:
import java.util.Scanner;
class FrequencyOfEachDigit
System.out.println("ENTER A NUMBER");
int n=sc.nextInt();
for(int i=0;i<10;i++)
f[i]=0;
System.out.println("====================================");
System.out.println("====================================");
int d;
while(n>0)
{
d=n%10;
f[d]++;
n=n/10;
for(int i=0;i<10;i++)
if(f[i]!=0)
System.out.println("\t"+i+"\t"+"\t"+f[i]);
OUTPUT:
6. Write a program to input a string (word). Convert it into lowercase letters. Count and
print the frequency of each alphabet present in the string. The output should be given
as:
Alphabet Frequency
==========================
a 2
b 1
e 1
h 1
l 1
p 1
s 1
t 1
SOURCE CODE:
import java.io.*;
class Frequency_ALPHABET
String s = br.readLine();
s=s.toLowerCase();
int l=s.length();
char ch;
System.out.println("==========================");
System.out.println("Alphabet\tFrequency");
System.out.println("==========================");
int count=0;
count = 0;
ch=s.charAt(j);
if(ch==i)
count++;
if(count!=0)
System.out.println(i+"\t\t"+count);
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
s String To initialise the string main()
7. Write a program to find the shortest and the longest word in a sentence and print
them along with their length. Sample Input: I am learning Java
Sample Output:
Shortest word = I
Length = 1
Length = 8
SOURCE CODE:
import java.io.*;
class Short_long_word
String s=br.readLine();
s=s+" ";
int len=s.length();
String x="",maxw="",minw="";
char ch;
int p,maxl=0,minl=len;
for(int i=0;i<len;i++)
ch=s.charAt(i);
if(ch!=' ')
x=x+ch;
else
{
p=x.length();
if(p<minl)
minl=p;
minw=x;
if(p>maxl)
maxl=p;
maxw=x;
x="";
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
s String To accept the line main()
SOURCE CODE:
import java.util.*;
class MERGE_2_ARRAY
int m,n,i;
m = in.nextInt();
n = in.nextInt();
A[i]=in.nextInt();
for(i=0;i<n;i++)
B[i]=in.nextInt();
for(i=0;i<m;i++)
C[i]=A[i];
for(i=0;i<n;i++)
{
C[m+i]=B[i];
for(i=0;i<(m+n);i++)
System.out.println(""+C[i]);
OUTPUT:
C[] int Array after merging array A[] and B[] main()
9. Write a Program to Search a particular element of an array using Binary Search
Technique.
SOURCE CODE:
import java.util.Scanner;
class Binary_SEARCH
num = input.nextInt();
array[counter] = input.nextInt();
item = input.nextInt();
first = 0;
last = num - 1;
first = middle + 1;
break;
}
else
last = middle - 1;
OUTPUT:
10. Write a Program to Input a Sentence which ends with a full stop and print the
words of the sentence in the following format:-
Input: City Of Joy.
SOURCE CODE:
import java.util.Scanner;
class reverse
int l,i;
System.out.println("ENTER A SENTENCE");
s=sc.nextLine();
s=s+" ";
l=s.length();
for(i=0;i<l-1;i++)
x=s.charAt(i);
if(x!=' ')
w=w+x;
else
r=w+r;
w=" ";
}
OUTPUT:
11. Write a Program in Java to input a number and check whether it is an Evil Number
or not. Evil Number : An Evil number is a positive whole number which has even
number of 1’s in its binary equivalent. Example: Binary equivalent of 9 is 1001, which
contains even number of 1’s. A few evil numbers are 3, 5, 6, 9....
Design a program to accept a positive whole number and find the binary equivalent of
the number and count the number of 1’s in it and display whether it is a Evil number or
not with an appropriate message. Output the result in format given below:
INPUT : 15
NO. OF 1’s : 4
SOURCE CODE:
import java.util.*;
class EvilNumber
String toBinary(int n)
int r;
String s="";
char dig[]={'0','1'};
while(n>0)
r=n%2;
s=dig[r]+s;
n=n/2;
return s;
int countOne(String s)
int c = 0, l = s.length();
char ch;
for(int i=0; i<l; i++)
ch=s.charAt(i);
if(ch=='1')
c++;
return c;
int n = sc.nextInt();
int x = ob.countOne(bin);
if(x%2==0)
else
}
OUTPUT:
Examples:
Input: s = "paris"
Output: arispay
Input: s = "amazon"
Output: amazonay
SOURCE CODE:
import java.util.Scanner;
class PigLatin
System.out.println("ENTER A WORD");
String s=in.nextLine();
s=s.toLowerCase();
int i;
for(i=0;i<s.length();i++)
char ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
break;
String f=s.substring(i)+s.substring(0,i)+"ay";
System.out.println(f);
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
13. Write a program to accept a list of 20 integers. Sort all the numbers in ascending
order by using 'Bubble Sort' technique.
SOURCE CODE:
import java.util.Scanner;
class BubbleSort
num = input.nextInt();
array[i] = input.nextInt();
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
System.out.println(array[i]);
OUTPUT:
14. Write a program in Java to accept the name of an employee and his/her annual
income. Pass the name and the annual income to a function Tax(String name, int
income) which displays the name of the employee and the income tax as per the given
tariff: Annual Income Income Tax
Up to ₹2,50,000 No tax
₹2,50,001 to ₹5,00,000 10% of the income exceeding ₹2,50,000
SOURCE CODE:
import java.util.Scanner;
double tax;
tax = 0;
else
String n = in.nextLine();
int i = in.nextInt();
EmployeeTax obj = new EmployeeTax();
obj.tax(n, i);
OUTPUT:
15. Write a class with the name Area using function overloading that computes the area
of a parallelogram, a rhombus and a trapezium.
SOURCE CODE:
import java.util.Scanner;
return c * d1 * d2;
return c * (a + b) * h;
double ht = in.nextDouble();
double d1 = in.nextDouble();
double d2 = in.nextDouble();
double a = in.nextDouble();
double b = in.nextDouble();
double h = in.nextDouble();
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
1. void display(String str, char ch) — checks whether the word str contains the letter ch
at the beginning as well as at the end or not. If present, print 'Special Word'
2. void display(String str1, String str2) — checks and prints whether both the words
are equal or not.
3. void display(String str, int n) — prints the character present at nth position in the
word str.
SOURCE CODE:
import java.util.Scanner;
class OVERLOAD
char c1,c2;
c1=str.charAt(0);
c2=str.charAt(str.length()-1);
System.out.println("SPECIAL WORD");
else
if(str1.equals(str2)==true)
else
char c3;
c3=str.charAt(n);
System.out.println("ENTER A WORD");
String s =sc.nextLine();
char c=sc.next().charAt(0);
obj.display(s,c);
String x=sc.nextLine();
String s2=sc.nextLine();
obj.display(x,s2);
String s3=sc.nextLine();
int n=sc.nextInt();
obj.display(s3,n);
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
c1 char To extract the character public void
display(String
str,char ch)
c2 char To extract the character public void
display(String
str,char ch)
c3 char To extract the character public void
display(String str,int
n)
s String To take the word from the user main(0
17. Write a program to input two numbers and check whether they are twin prime
numbers or not.
Hint: Twin prime numbers are the prime numbers whose difference is 2.
SOURCE CODE:
import java.util.Scanner;
int a = in.nextInt();
int b = in.nextInt();
if (a % i == 0) {
isAPrime = false;
break;
if (b % i == 0) {
isBPrime = false;
break;
if (isBPrime)
else
else
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
18. Using the switch statement, write a menu driven program for the following:
23
456
7 8 9 10
11 12 13 14 15
IC
ICS
ICSE
SOURCE CODE:
import java.util.Scanner;
class Pattern
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 1;
System.out.println();
break;
case 2:
String s = "ICSE";
System.out.println();
break;
default:
System.out.println("Incorrect Choice");
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
19. An air-conditioned bus charges fare from the passengers based on the distance
travelled as per the tariff given below:
Distance Travelled Fare
11 km to 20 km ₹6/km
21 km to 30 km ₹5/km
Design a program to input distance travelled by the passenger. Calculate and display
the fare to be paid.
SOURCE CODE:
import java.util.Scanner;
int fare = 0;
if (dist <= 0)
fare = 0;
fare = 80;
OUTPUT:
import java.util.Scanner;
class SERIES
int x,n,f=1;
double sum=0.0;
x=sc.nextInt();
n=sc.nextInt();
for(int j=1;j<=i;j++)
f=f*j;
sum=sum+(Math.pow(x,i))/f;
OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
x int To accept value from user main()
INTERNAL EXTERNAL