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

Computer

Project which contains basic problem statements and solutions

Uploaded by

akshatshah0027
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Computer

Project which contains basic problem statements and solutions

Uploaded by

akshatshah0027
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 68

COMPUTER PROJECT

YEAR - 2019-20

NAME – Akshat Shah


CLASS – X DIVISION – E
ROLL NO. – 2
SUBJECT – Computer Application
Index
SR NO. CONTENT SIGN
1 polygon()

2 check()
3 area()
4 Railway Ticket
5 Income Tax
6 print()
7 series(0
8 Arrange Letters
9 mobike
10 Electricity
11 to arrange characters
12 word from first letter
13 patterns
14 to find number of double
letters
15 start with vowel and end with
consonant
16 start with capital letter and
end with small letter
17 Narcissistic number
18 Amicable and twisted prime
number
19 to remove zero from number
20 emirp number
21 patterns
22 to find frequency of digits
23 Automorphic number and
smallest digit in number
24 array program
25 binary search
26 bubble sort
27 selection sort
28 double dimensional array(sum
of all elements)
29 double dimensional array(sum
of rows and columns)
30 double dimensional array(sum
of left and right diagonal)
Bonafide Certificate

Student’s Name Akshat Shah


Class X Division E Roll No. 2
School Chatrabhuj Narsee Memorial School

This is to certify that all Java Programs written in this book has been
performed by the student satisfactorily.

Internal Examiner External Examiner

Name- Name-

Date- Date-

Signature Signature
PROGRAM 1
Design a class to overload a function polygon() as follows:
(i) void polygon(int n, char ch) : with one integer argument and one character type argument
that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a filled rectangle of length
x and breadth y, using the symbol ‘@’
(iii) void polygon() : with no argument that draws a filled triangle shown below:
*
**
***

class program01
{
void polygon(int n, char ch)
{
for (int i=1;i<=4;i++)
{
for (int j=1;j<=4;j++)
{
System.out.print(ch);
}
System.out.println("");
}
}
void polygon(int x, int y)
{
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
{
System.out.print("@");//to print
}
System.out.println("");
}
}
void polygon()
{
for (int i=1;i<=3;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println("");
}
}
public static void main()
{
program01 obj=new program01();//to create an object of the class
obj.polygon();
obj.polygon(8,10);
obj.polygon(6,'&');
}
}

Output

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i To run outer ‘for’ loop

2. int j To run inner ‘for’ loop


3. int x To store the length of rectangle
4. int y To store breadth of rectangle
5. int n To store side of square
6. char ch To store character of square

PROGRAM 2
Design a class to overload a function check() as follows:
(i) void check(String str, char ch) – to find and print the frequency of a character in a string.
Example: Input : str = “success” ch=’s’
Output : number of s present is =3
(ii) void check(String s1) – to display only vowels from string s1, after converting it to lower
case.
Example: Input : “computer”
Output : oue
(iii) void check(String s1, char ch1, char ch2) – display string s1 after converting ch1
character with ch2.
Example: Input : “Application” ch1=’p’ ch2=’*’
Output : A**lication

class program02
{
void check (String str, char ch)
{
int ctr=0;
for (int i=0;i<str.length();i++)
{
char ch1=str.charAt(i);
if (ch1==ch)
ctr++;
}
System.out.println("Number of "+ch+" present is = "+ctr);
}
void check(String s1)
{
s1=s1.toLowerCase();//coverts the string to lower case
for (int i=0;i<s1.length();i++)
{
char ch=s1.charAt(i);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
System.out.print(ch);
}
System.out.println();
}
void check(String s1, char ch1, char ch2)
{
System.out.println(s1.replace(ch1,ch2));
}
public static void main()
{
program02 obj=new program02();//to create an object of the class
obj.check("success",'s');
obj.check("COMPUTER");
obj.check("Application",'p','*');
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i To run ‘for’ loop


2. String str To store sentence
3. char ch To store the character accepted
4. char ch1 To store the character of sentence
5. String s1 To store word
6. int ctr To count the frequency of character
PROGRAM 3
Design a class to overload a function area( ) as follows:
i. double area(double a, double b, double c) with three double arguments, returns
the area of a scalene triangle using the formula: area =

where s =
ii. double area(int a, int b, int height) with three integer arguments, returns the area

of a trapezium using the formula: area =


iii. double area(double diagonal1, double diagonal2) with two double arguments,
returns the area of a rhombus using the formula:

area =

class program03
{
double area(double a, double b, double c)
{
double s=(a+b+c)/3;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
double area(int a, int b, int height)
{
double area=0.5*height*(a+b);
return area;
}
double area(double diagonal1, double diagonal2)
{
double area=0.5*(diagonal1*diagonal2);//to calculate the area
return area;
}
public void main()
{
program03 obj=new program03();//to create an object of the class
System.out.println("The Area is : "+area(3.0,4.0,5.0));
System.out.println("The Area is : "+area(4,6,10));
System.out.println("The Area is : "+area(8.0,10.0));
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description


1. double a To store side 1 of triangle
2. double b To store side 2 of triangle
3. double c To store side 3 of triangle
4. double s To store s of formula
5. int a To store parallel side 1 of trapezium
6. int b To store parallel side 2 of trapezium
7. int height To store height of trapezium
8. double diagonal1 To store diagonal 1 of rhombus
9. double diagonal2 To store diagonal 2 of rhombus
10. double area To store Area
PROGRAM 4
Design a class RailwayTicket with following description:
Instance variables/data members :
String name : To store the name of the customer
String coach : To store the type of coach customer wants to travel
longmobno : To store customer’s mobile number
intamt : To store basic amount of ticket
inttotalamt : To store the amount to be paid after updating the original amount
Member methods :
void accept() : To take input for name, coach, mobile number and amount.
void update() : To update the amount as per the coach selected
(extra amount to be added in the amount as follows)
Type of Coaches Amount
First_AC 700
Second_AC 500
Third_AC 250
Sleeper None
void display() :To display all details of a customer such as name, coach, total amount and mobile
number.
Write a main method to create an object of a class and call the above member methods.

import java.util.*;
class RailwayTicket
{
String name="";
String coach="";
long mobno=0;
int amt=0;
int totalamt=0;
Scanner sc=new Scanner (System.in);
void accept()
{
System.out.println("Enter your Name:");
name =sc.nextLine();
System.out.println("Enter your Coach:");
coach =sc.next();
System.out.println("Enter your Mobile Number:");
mobno=sc.nextLong();
System.out.println("Enter the Basic Amount of Ticket:");
amt=sc.nextInt();
}
void update()
{
System.out.println("Enter 1 to select First AC:");
System.out.println("Enter 2 to select Second AC:");
System.out.println("Enter 3 to select Third AC:");
System.out.println("Enter 4 to select Sleeper:");
System.out.println("Enter your choice:");//to accept user’s choice
int coach =sc.nextInt();
if (coach==1)
totalamt=amt+700;
if (coach==12)
totalamt=amt+500;
if (coach==3)
totalamt=amt+250;
if (coach==4)
totalamt=amt;
}
void display()
{
System.out.println("Name : "+name);//to print the name
System.out.println("Coach : "+coach);
System.out.println("Total Amount : Rs"+totalamt);
System.out.println("Mobile Number : "+mobno);
}
public static void main()
{
RailwayTicket obj=new RailwayTicket();//to create an object of the class
obj.accept();
obj.update();
obj.display();
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. String name To store the name of the customer

2. String coach To store the type of coach customer


wants to travel
3. long mobno To store customer’s mobile number

4. int amt To store basic amount of ticket

5. int totamt To store the amount to be paid after


updating the original amount
PROGRAM 5
Given below is a hypothetical table showing rates of income tax for male citizens below the age
of 65 yrs:
Does not exceed Rs. 1,60,000 -nil
Is >Rs. 1,60,000&<=Rs. 5,00,000 – (TI- 1,60,000)*10%
Is >Rs.5,00,000 &<=Rs. 8,00,000 - [(TI-5,00,000)*20%]+34,000
Is >Rs. 8,00,000 – [(TI-8,00,000)*30%]+94,000
Write a program to input the age, gender(male or female) and taxable income(TI) of a person.
If the age is more than 65 yrs or the gender is female, display “wrong category”.
If the age is less than or equal to 65 yrs and the gender is male, compute and display the income
tax payable as per the table given above.
import java.util.*;
class program05
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter Age :");
int age =sc.nextInt();
System.out.println("Enter Gender :");
String gender=sc.next();
System.out.println("Enter Taxable Income :");
double TI=sc.nextDouble();
double tax=0.0d;
if (age<=65 && gender.equalsIgnoreCase("male"))
{
if (TI<160000)
tax=0;
if (TI>160000 && TI<=500000)
tax=(TI-160000)*0.1;
if (TI>500000 && TI<=800000)
tax=((TI-500000)*0.2)+34000;
if (TI>800000)
tax=((TI-800000)*0.3)+94000;//to calculate taxable icome
System.out.println("Income tax : "+tax);
}
else
System.out.println("Wrong Choice");//to print wrong choice if the choise entered is wrong
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description


1. int age To store the age of the user
2. String gender To store the gender of the user
3. double TI To store taxable income of user
4. double tax To store tax
PROGRAM 6
Design class to overload a function Print() as follows:
i) void Print(String str) to input a word from the user and remove the duplicate
characters present
in it. Eg. INPUT Mississppi OUTPUT Misp
ii) void Print(char c int n) to print the char ch n times.
iii) void Print(int n) to print the series: 1, 11, 111, 1111, …. upto 'n' terms

import java.util.*;
class program06
{
void Print(String str)
{
String w=””;
for (int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if (ch!=’ ’)
w=w+ch;
str=str.replace(‘ch’,’ ’);
}
System.out.println(“Word after removing all duplicate characters:”+w);
}
void Print(char c , int n)
{
for (int i=0;i<n;i++)
System.out.print(c);
System.out.println();
}
void Print(int n)
{
int num=0;
for (int i=0;i<n;i++)
{
num=num*10+1;to calculate the number
System.out.print(num+",");
}
}
public static void main()
{
program06 obj=new program06();//to create an object of the class
obj.Print("Missippi");
obj.Print('@',10);
obj.Print(10);
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i To store ‘for’ loop

2. String str To store the word from the user

3. char c To store character from user

4. int n To store number from user

5. int num To store sum in series


PROGRAM 7
Design a class to overload a function series( ) as follows:
i) int series(int n) with one int argument and returns the factorial of the number n.
ii) double series(int x, int a) with two int argument and returns the sum of the series
sum = x+x2+x3………..xa
2 3 a
iii) with two double arguments and returns the sum of the series :
sum=1 + 4 + 7 + 10 ……… n terms
a2 a5 a8 a11

class program07
{
int series(int n)
{
int fact=1;
for (int i=1;i<n;i++)
fact = fact*i;
return fact;
}
double series(int x, int a)
{
double sum=0;
for (int i=1;i<=a;i++)
sum=sum+(Math.pow(x,a)/a);
return sum;
}
double series(double a, double n)
{
double sum=1.0;
double no=4.0;
for (double i=1;i<n;i++)
{
sum=sum+(n/Math.pow(a,n-2));//to calculate sum
no=no+3.0;
}
return sum;
}
public void main()
{
program07 obj=new program07();//to create an object of the class
System.out.println("The Sum of Series is : "+series(6));
System.out.println("The Sum of Series is : "+series(3,4));
System.out.println("The Sum of Series is : "+series(4.0,3.0));
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i To store ‘for’ loop

2. int x To store number 1 from user

3. int fact To store factorial

4. int n To store number from user

5. double sum To store sum in series

6. int a To store number 2 from user

7. double a To store number from user

8. double n To store number from user

9. double no To store number for series


PROGRAM 8
Write a program to create class ArrangeLetters and convert the word into uppercase form.
Arrange each letter of the word in alphabetical order and print the word before and after arrange
letters in A- Z order. Example:
Input : LanGuAge
Output
Original word : LanGuAge
Words in Capitals : LANGUAGE
Words after sorting : AAEGGLNU

import java.util.*;
class ArrangeLetters
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the Word :");
String word=sc.next();
System.out.println("Original Word : "+word);
word=word.toUpperCase();
System.out.println("Words in Capitals : "+word);
String w="";
for (int i=1;i<=200;i++)
{
char ch=(char)i;
for (int j=0;j<word.length();j++)
{
char ch1=word.charAt(j);//to extract the character at that position
if (ch==ch1)
w=w+ch;
}
}
System.out.println("Words after sorting : "+w);//to print
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i To store outer ‘for’ loop

2. int j To store inner ‘for’ loop

3. char ch To store character of ASCII value

4. char ch1 To store character of string

5. String word To store word by user

6. String w To store sorted word


PROGRAM 9
Define a class calledmobike with the following description:
Instance variables/data members:
intbno – to store the bike’s number
intphno – to store the phone number of the customer
String name – to store the name of the customer
int days – to store the number of days the bike is taken on rent
int charge – to calculate and store the rental charge
Member methods:
void input( ) – to input and store the detail of the customer.
void computer( ) – to compute the rental charge
The rent for a mobike is charged on the following basis.
First five days Rs 500 per day;
Next five days Rs 400 per day
Rest of the days Rs 200 per day
void display ( ) – to display the details in the following format:
Bike No. PhoneNo. No. of days Charge
Write a main method to create an object of the class and call the above member methods.

import java.util.*;
class mobike
{
int bno,phno,days,charge;
String name;
void input()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Name : ");too accept name
name=sc.nextLine();
System.out.println("Enter Phone Number : ");
phno=sc.nextInt();
System.out.println("Enter Bike Number : ");
bno=sc.nextInt();
System.out.println("Enter Number of Days the Bike is Taken on rent : ");
days=sc.nextInt();
}
void computer()
{
if (days<=5)
charge=500*days;
if (days>5 && days<=10)
charge=(500*5)+((days-5)*400);//to calculate charge
if (days>10)
charge=(500*5)+(400*5)+((days-10)*200);
}
void display()
{
System.out.println("Bike No. Phone No. No. of Days Charge");
System.out.println(bno+" "+phno+" "+days+" "+charge);
}
public static void main()
{
mobike obj=new mobike();//to create an object of the class
obj.input();
obj.computer();
obj.display();
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int bno To store the bike’s number

2. int phno To store the phone number of the


customer
3. int days To store the number of days the bike is
taken on rent
4. int charge To calculate and store the rental charge

5. String name To store the name of the customer


PROGRAM 10
Write a program using the following specification:
Class Name Electricity
Data members
intomr Old Meter Reading
intnmr New Meter Reading
intcr Current Meter Reading (nmr-omr)
int rent Monthly Rent
double cost Billing Cost
double sc Surcharge
Data Members
Electricity( ) Initialized all data members empty.
input( ) Method to get the old meter reading and new meter reading
calculate( ) Method to calculate the total billing cost on the basic of unit consumed.
display( ) Method to display the total billing cost.
main( ) Invoke all methods.
Note :
 Monthly Rent is Rs. 200/-.
 Surcharge is 5% on the total billing cost.
 Units are to be charged on the following basis.
Units consumed Charges
First 100 Re. 0.80 P
Next 101 to 250 Re. 1.20 P
251 and above Rs. 2.00 P

import java.util.*;
class Electricity
{
int omr,nmr,cr,rent;//local variables
double cost, sc;
Electricity()//constructor
{
omr=0;
nmr=0;
cr=0;
rent=0;
cost=0.0d;
sc=0.0d;
}
void input()
{
Scanner sa =new Scanner (System.in);
System.out.println("Enter Old Meter Reading : ");//to accept old meter readings
omr=sa.nextInt();
System.out.println("Enter New Meter Reading : ");
nmr=sa.nextInt();
}
void calculate()
{
cr=nmr-omr;
if (cr<=100)
cost=0.80*cr;
if (cr>100 && cr<=250)
cost=80+((cr-100)*1.20);
if (cr>250)
cost=80+180+((cr-250)*2.00);
cost=cost+200;
sc=0.05*cost;
}
void display()
{
System.out.println("Total Billing Cost : "+cost);
System.out.println("Surcharge : "+sc);
System.out.println("Net Cost : "+(cost+sc));
}
public static void main()
{
Electricity obj=new Electricity();//to create an object of the class
obj.input();
obj.calculate();
obj.display();
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int omr Old Meter Reading

2. int nmr New Meter Reading

3. int cr Current Meter Reading (nmr-omr)

4. int rent Monthly Rent

5. double cost Billing Cost

6. double sc Surcharge
PROGRAM 11
WAP to accept the String and print all capital letters followed by small letter and followed by
digits. If
Input: MuMbAi-53
Output : MMAubi53

import java.util.*;
class program11
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sentence=sc.nextLine();
for (int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if (Character.isUpperCase(ch))
System.out.print(ch);
}
for (int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if (Character.isLowerCase(ch))
System.out.print(ch);//to print the word
}
for (int i=0;i<sentence.length();i++)
{
char ch=sentence.charAt(i);
if (Character.isDigit(ch))//to check if the character entered is digit
System.out.print(ch);
}
}
}

OUTPUT
Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i For ‘for’ loop

2. char ch To store extracted character


PROGRAM 12
Write a program to accept a string from user and create a new word formed out of the first letter
of each word and convert the new word into Uppercase.

Example: Input : Mangoes are delivered after Midday


Output : MADAM

import java.util.*;
class program12
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();
sent=" "+sent;
String word="";
for (int i=0;i<sent.length()-1;i++)
{
if (sent.charAt(i)==' ')
word=word+sent.charAt(i+1);
}
word=word.toUpperCase();//to convert the word to upper case
System.out.println(word);//to print the word
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i For ‘for’ loop

2. String w To store extracted word

3. String word To store sentence from the user


PROGRAM 13
Using switch statement write a menu driven program:
(i) To print the following pattern
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
(ii) To display the following pattern
A
BC
DEF
GHIJ
KLMNO
For an incorrect option an appropriate error message should be displayed.

import java.util.*;
class program13
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("1. Number Pattern");
System.out.println("2. Letter Pattern");
System.out.println("Enter choice");
int ch=sc.nextInt();//to accept user’s choice
switch (ch)
{
case 1:
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
if (j%2!=0)
System.out.print("1");
else
System.out.print("0");
}
System.out.println("");
}
}
break;
case 2:
int a=65;
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
System.out.print((char)a);
a++;//to increament
}
System.out.println("");
}
}
break;
default:System.out.println("Wrong Choice");//to print wrong choice if choice is wrong
}
}
}

OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i For outer ‘for’ loop

2. int j For inner ‘for’ loop

3. int a To store ASCII value

4. int ch To store choice of user

PROGRAM 14
Write a program to accept a string. Convert it to uppercase. Count and output the number of
double letter that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4

import java.util.*;
class program14
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();
sent=sent.toUpperCase();
int ctr=0;
for (int i=0;i<sent.length()-1;i++)
{
if (sent.charAt(i)==sent.charAt(i+1))
ctr++;
}
System.out.println(ctr);
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i For ‘for’ loop

2. String sent To store sentence from user

3. int ctr To store number of double letter

PROGRAM 15
Write a program to accept a sentence. Print all the words that start with a vowel and end with a
consonant.
For example : Input :“the purpose of education is to replace an empty mind with an open one”
Output : of
education
is
an
empty
an
open

import java.util.*;
class program15
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();//to Accept the sentence
String w="";
sent=sent+" ";
for (int i=0;i<sent.length();i++)
{
char ch=sent.charAt(i);
if (ch==' ')
{
char c1=w.charAt(0);
char c2=w.charAt(w.length()-1);
if((c1=='A'||c1=='E'||c1=='I'||c1=='O'||c1=='U'||c1=='a'||c1=='e'||c1=='i'||c1=='o'||
c1=='u') && (c2!='A'||c2!='E'||c2!='I'||c2!='O'||c2!='U'||c2!='a'||c2!='e'||c2!='i'||c2!='o'||c2!
='u'))
System.out.println(w);
w="";
}
else
w=w+ch;//to store the word
}
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. String sent To store sentence from the user

2. String w To store extracted word

3. int i For ‘for’ loop

4. char ch To store extracted character from


sentence
5. char c1 To store extracted character from word

6. char c2 To store extracted character from word


PROGRAM 16
Write a program to accept a string and display all those words of the string which begins with a
capital letter and end with a small letter.
Input : We all lovEJavA for School Students because of its Uniqueness
Output : We School Students Uniqueness

import java.util.*;
class program16
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter Sentence : ");
String sent=sc.nextLine();//to accept the sentence
String w="";
sent=sent+" ";
for (int i=0;i<sent.length();i++)
{
char ch=sent.charAt(i);
if (ch==' ')
{
char c1=w.charAt(0);
char c2=w.charAt(w.length()-1);
if (Character.isUpperCase(c1) && Character.isLowerCase(c2))
System.out.print(w+" ");
w="";
}
else
w=w+ch;//to store the word
}
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. String sent To store sentence from the user

2. String w To store extracted word

3. int i For ‘for’ loop

4. char ch To store extracted character from


sentence
5. char c1 To store extracted character from word

6. char c2 To store extracted character from word


PROGRAM 17
Write a program to accept the number from user to check for Narcissistic number.
A Narcissistic number is one which is equal to the sum of each of its individual digits raised to
the number of digits in that number. For example, 153 has 3 digits, so 13 + 53 + 33 = 1 + 125 + 27
= 153; and hence 153 is a Narcissistic number

import java.util.*;
class program17
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number : ");
int n=sc.nextInt();//to accept the number from user
int copy=n;
int ctr=0;
while (copy>0)
{
copy=copy/10;
ctr++;
}
copy=n;
double sum=0.0d;
while (n>0)
{
int rem=n%10;
n=n/10;
sum=sum+Math.pow(rem,ctr);
}
if (copy==sum)
System.out.println("It is a Narcissistic Number");
else
System.out.println("It is Not a Narcissistic Number");//to print if the condition is false
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int n To store number from the user

2. int copy To store copy of number

3. int ctr To count number of digits

4. int rem To store remainder

5. double sum To store sum


PROGRAM 18
Write a menu driven program to do following task as per user choice:
1. Check for Amicable number(Note:Amicable number are pair of numbers such that
one is the sum of proper factors of the other. For example : 220 and 284 , since
sum of factors of 220 = 1+2+4+5+10+11+20+22+44+55+110=284
sum of factors of 284=1+2+7+71+142=220)
2. Check for Twisted Primenumber (Note: A prime number is said to be ‘Twisted Prime’,
if the new number obtained after reversing the digits is also a prime number. Example:
167)

import java.util.*;
class program18
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("1. Amicable number");
System.out.println("2. Twisted Prime Number");
System.out.println("Enter Choice");
int ch=sc.nextInt();
switch (ch)
{
case 1:
{
System.out.println("Enter First Number : ");
int n1=sc.nextInt();//to accept the number
System.out.println("Enter Second Number : ");
int n2=sc.nextInt();
int sum1=0;
int sum2=0;
for (int i=1;i<n1;i++)
{
if (n1%i==0)
sum1=sum1+i;
}
for (int i=1;i<n2;i++)
{
if (n2%i==0)
sum2=sum2+i;
}
if (sum1==n2 && sum2==n1)
System.out.println("They are Amicable number");
else
System.out.println("They are Not Amicable number");
}
break;
case 2:
{
System.out.println("Enter Number:");
int n=sc.nextInt();
int ctr=0,rev=0,ctr2=0;
for (int i=1;i<=n;i++)
{
if (n%i==0)
ctr++;
}
while (n>0)
{
int rem=n%10;
n=n/10;
rev=rev*10+rem;//to reverse the number
}
for (int i=1;i<=rev;i++)
{
if (rev%i==0)
ctr2++;
}
if (ctr==2&&ctr2==2)
System.out.println("It is an Twisted Prime Number");
else
System.out.println("It is Not a Twisted Prime Number");
}
break;
default:System.out.println("Wrong Choice");
}
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int ch To store user’s choice


2. int n1 To store First Number from user
3. int n2 To store Second Number from user
4. int sum1 To store sum of factors of First number
5. int sum2 To store sum of factors of First number
6. int i To run ‘for’ loop
7. int n To store number from the user
8. int ctr To count number of factors of original
number
9. int ctr2 To count number of factors of reversed
number
10. int rem To store remainder
11. int rev To store reversed number
PROGRAM 19
Write a program to accept the number. Display the accepted number after removing all zeros (0)
from the number. Example: Input 40809 Output 489

import java.util.*;
class program19
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter Number");
int n=sc.nextInt();//to accept the number
int n1=0;
while (n>0)
{
int rem=n%10;
n=n/10;
if (rem!=0)
n1=n1*10+rem;
}
while (n1>0)
{
int rem=n1%10;
n1=n1/10;//to extract the number’s digits
if (rem!=0)
n=n*10+rem;
}
System.out.println("The Number is : "+n);
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int n To store number from user

2. int n1 To store reversed number

3. int rem To store remainder


PROGRAM 20
An emirp number is a number which is prime backwards and forwards.
Example: 13 and 31 are both prime numbers. Thus, 13 is an emirp number. Design a class
Emirp to check if a given number is Emirp number or not.

import java.util.*;
class Emrip
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number:");
int n=sc.nextInt();//to accept the number
int ctr=0,rev=0,ctr2=0;
for (int i=1;i<=n;i++)
{
if (n%i==0)
ctr++;
}
while (n>0)
{
int rem=n%10;
n=n/10;
rev=rev*10+rem;//to reverse the number
}
for (int i=1;i<=rev;i++)
{
if (rev%i==0)
ctr2++;
}
if (ctr==2&&ctr2==2)
System.out.println("It is an Emrip Number");
else
System.out.println("It is Not a Emrip Number");
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int n To store number from the user

2. int ctr To count number of factors of original


number
3. int ctr2 To count number of factors of reversed
number
4. int rem To store remainder

5. int rev To store reversed number

6. int i For ‘for’ loop


PROGRAM 21
1. Write menu driven program to print the following patterns using iteration(loop)
statements :
1. 2.

import java.util.*;
class program21
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("1. Special Characters Pattern");
System.out.println("2. Number Pattern");
System.out.println("Enter choice");
int ch=sc.nextInt();//to accept the choice
switch (ch)
{
case 1:
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
{
if (j%2!=0)
System.out.print("*");
else
System.out.print("#");
}
System.out.println("");
}
}
break;
case 2:
{
for (int i=1;i<=5;i++)
{
for (int j=5;j>=i;j--)
{
System.out.print(j);
}
System.out.println("");
}
}
break;
default:System.out.println("Wrong Choice");");//to print wrong choice if choice is wrong

}
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int i For outer ‘for’ loop

2. int j For inner ‘for’ loop

3. int ch To store choice of user


PROGRAM 22
Write a program to accept 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

import java.util.*;
class program22
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("Enter Number : ");
int n=sc.nextInt();//to accept the number
System.out.println("Digit Frequency");
System.out.println("===================");
for (int i=0;i<=9;i++)
{
int ctr=0;
int copy=n;
while (n>0)
{
int rem=n%10;
n=n/10;
if (i==rem)
ctr++;
}
if (ctr>0)
System.out.println(i+" "+ctr);
n=copy;//to create a copy of the number
}
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description


1. int i For ‘for’ loop
2. int ctr To count the frequency
3. int copy To store copy of number from user
4. int rem To store remainder
5. int n To store accepted number from user
PROGRAM 23
Using switch write a menu driven program for the following task:
1. Automorphic number: An automorphic number is the number which is contained in
the last digit(s) of its square. Example: 25 is an automorphic number as its square is
625 and 25 is present as the last two digits.
2. To find the smallest digit of an integer that is that.
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect an appropriate error message should be displayed.

import java.util.*;
class program23
{
public static void main()
{
Scanner sc= new Scanner (System.in);
System.out.println("1. Automorphic Number");
System.out.println("2. Smallest Digit of an Integer");
System.out.println("Enter choice");
int ch=sc.nextInt();//to accept the number
System.out.println("Enter Number");
int n=sc.nextInt();
switch (ch)
{
case 1:
{
int rem=(n*n)%100;
if (n==rem)
System.out.println("It is an Automorphic Number");
else
System.out.println("It is Not an Automorphic Number");
}
break;
case 2:
{
int min=9;
while (n>0)
{
int rem=n%10;
n=n/10;
if (rem<min)
min=rem;
}
System.out.println("Smallest Digit is : "+min);
}
break;
default:System.out.println("Wrong Choice");//to print wrong choice if choice is wrong
}
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description


1. int ch To store user’s choice
2. int min To store lowest digit
3. int rem To store remainder
4. int n To store accepted number from user
PROGRAM 24
Write a program to accept the names of 10 countries in a single dimension string array and their
famous place in another single dimension array. Search for a name of a country input by the user
in the list. If found, display “search successful” and print the name of the country along with
famous place, or else display the message” search unsuccessful, no such country in the list”.

import java.util.*;
class program24
{
public static void main()
{
Scanner sc=new Scanner (System.in);
String con[]=new String[10];
String pac[]=new String[10];
for (int i=0;i<con.length;i++)
{
System.out.println("Enter Name of the Country : ");
con[i]=sc.nextLine();//to accept the name of country
System.out.println("Enter the Name of Famous Place : ");
pac[i]=sc.nextLine();
}
System.out.println("Enter the Name of Country you want to search : ");
String ser=sc.nextLine();
int pos=-1;
for (int i=0;i<con.length;i++)
{
if (ser.equalsIgnoreCase(con[i]))
pos=i;
}
if (pos!=-1)
{
System.out.println("Search Successful ");
System.out.println("Name of the Country : "+con[pos]);//to print the name of country
System.out.println("Name of Famous Place : "+pac[pos]);
}
else
System.out.println("Search Unsuccessful ");
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description

1. int con[] To store user’s choice of Country

2. int pac[] To store user’s choice of Famous Place

3. int i For ‘for’ loop

4. int pos To store position of searched element


PROGRAM 25
Write a program to accept the year of graduation from school as an integer value from the user.
Using the Binary Search technique on the sorted array of integers given below, output the
message “Record exists” if the value input is located in the array. If not, output the message
“Record does not exist”.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}

import java.util.*;
class program25
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int year[]={1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
System.out.println("Enter the year of graduation from school : ");
int target=sc.nextInt();//to accept year to be searched
int left=0;
int right=year.length-1;
int result=-1;
while (left<=right)
{
int middle=(left+right)/2;
if (year[middle]==target)
{
result=middle;
break;
}
else if (year[middle]>target)
{
right=middle-1;
}
else
{
left=middle+1;
}
}
if (result!=-1)
System.out.println("Record exists");//to print if the year is found
else
System.out.println("Record does not exists");
}
}
OUTPUT

Variable Description

Sr. No. Variable Type Variable Name Variable Description


1. int year[] To store array of years
2. int target To store the year to be searched
3. int left To store starting limit
4. int right To store ending limit
5. int result To store result of search
6. int middle To store middle element
PROGRAM 26
Define a class and accept 10 students’ names in a single dimensional array. Sort these names in
alphabetical order using the Bubble Sort technique only.

import java.util.*;
class program26
{
public static void main()
{
Scanner sc=new Scanner (System.in);
String name[]=new String[10];
String temp="";
for (int i=0;i<name.length;i++)
{
System.out.println("Enter Name of the Student : ");
name[i]=sc.nextLine();//to accept name
}
for (int i=0;i<name.length;i++)
{
for (int j=0;j<name.length-1;j++)
{
if (name[j].compareTo(name[j+1])>0)
{
temp=name[j];
name[j]=name[j+1];
name[j+1]=temp;
}
}
}
System.out.println("Names of the Students in alphabetical order is : ");
for (int i=0;i<name.length;i++)
{
System.out.println(name[i]);//to print name
}
}
}
OUTPUT

Variable Description
Sr. No. Variable Type Variable Name Variable Description

1. String name[] To store array of names

2. String temp To store the temporary name

3. int i For ‘for’ loop

4. int j For ‘for’ loop


PROGRAM 27
Write a program to accept 35 integers from the keyboard. Perform selection sort on the integers
and then print them in descending order.

import java.util.*;
class program27
{
public static void main()
{
Scanner sc=new Scanner (System.in);
int n[]=new int[35];
int temp,max,pos;
for (int i=0;i<n.length;i++)
{
System.out.println("Enter Integer : ");
n[i]=sc.nextInt();//to accept an integer
}
for (int i=0;i<n.length;i++)
{
max=n[i];
pos=i;
for (int j=i+1;j<n.length;j++)
{
if (n[j]>max)
{
max=n[j];//to find the maximum if the array
pos=j;
}
}
temp=n[i];
n[i]=n[pos];
n[pos]=temp;
}
System.out.println("Array of Integer in decending order is : ");//to print the array
for (int i=0;i<n.length;i++)
{
System.out.println(n[i]);//to print the array
}
}
}
OUTPUT
Variable Description
Sr. No. Variable Type Variable Name Variable Description

1. int n[] To store array of Integers

2. int temp To store the temporary Integer

3. int i For ‘for’ loop

4. int j For ‘for’ loop

5. int max To store maximum value for sorting

6. int pos To store position of maximum value

PROGRAM 28
Write a program to accept integer elements in a double dimensional array of size 4x4 and find
the sum of the elements.
7345
5461
6942
3275
Sum of the elements is :73

import java.util.*;
class sum
{
static void main()//to create main method
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];
int sum=0;//a variable to store the sum of all elements
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.println("ENTER AN INTEGER");
a[i][j]=sc.nextInt();//accepting the elements
sum+=a[i][j];//calculate the sum
}
}
System.out.println();
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
System.out.print(a[i][j]+" ");//to print the array in matrix form
}
System.out.println();
}
System.out.println();
System.out.println("THE SUM OF ALL ELEMENTS IS :"+sum);//to print the sum
}
}
VARIABLE DESCRIPTION

SR NO. VARIABLE DATA TYPE DESCRIPTION


1. a[][] int double dimensional array to store the
numbers

2. sum int to store the sum of all elements

PROGRAM 29
Write a program to accept integer elements in a double dimensional array of size 3x4 and find
the sum of the each row and each column elements.
Example:
Input :
7345
5461
6942
Output :
Sum of row 1 is : 19
Sum of row 2 is : 16
Sum of row 3 is : 21

Sum of column 1 is : 18
Sum of column 2 is : 16
Sum of column 3 is : 14
Sum of column 4 is : 8

import java.util.*;
class row_column
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][4];//to declare an array of 3 x 4
int s1=0;
int s2=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.println("ENTER AN INTEGER");//to accept the elements of array
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");//to print the array
}
System.out.println();
}
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
s1=s1+a[i][j];//to calculate the sum of numbers in a row
}
System.out.println("SUM OF ROW "+(i+1)+" IS :"+s1);
s1=0;
}
System.out.println();
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
{
s2=s2+a[j][i];//to calculate the sum of numbers in a column
}
System.out.println("SUM OF COLUMN "+(i+1)+" IS :"+s2);
s2=0;
}
}
}

VARIABLE DESCRIPTION

SR NO. VARIABLE DATA TYPE DESCRIPTION


1. a[][] int double dimensional array to store the
numbers

2. s1 int to store the sum of rows

3. s2 int to store the sum of columns

PROGRAM 30
WAP in java to find out the sum of the two main(Left and Right) diagonals of the given multi-
dimensional array x[][]={{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}}.

class diagonal
{
public static void main()
{
int x[][]={{6,9,0,8},{3,2,1,5},{2,9,0,1},{4,1,9,6}};//to declare a 2D array;
int s3=0;
int s4=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
System.out.print(x[i][j]+" ");//to print the array
}
System.out.println();
}
for(int i=0;i<4;i++)
{
s3=s3+x[i][i];//to calculate the sum numbers in left diagonal
}
System.out.println("SUM OF LEFT DIAGONAL IS :"+s3);//to print the sum of left
diagonal
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if((i+j)==3)
s4=s4+x[i][j];//to calculate the sum numbers in right diagonal
}
}
System.out.println();
System.out.println("SUM OF RIGHT DIAGONAL IS :"+s4);//to print the sum of right
diagonal
}
}

VARIABLE DESCRIPTION

SR NO. VARIABLE DATA TYPE DESCRIPTION


1. x[][] int double dimensional array to store the
numbers

2. s3 int to store the sum of left diagonal

3. s4 int to store the sum of right diagonal

You might also like