Shubhankar2 PDF
Shubhankar2 PDF
2. Duck Number. 06
import java.util.*;
public class Superspy{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int c=n,s=0,r,a=0;
while(n>0)
{
n=n/10;
a++;
}
while(c>0)
{
r=c%10;
s=s+r;
c=c/10;
} //While ends
if(s==a)
System.out.println("SUPERSPY Number"+" "+"[Sum of the digits="+s+", Number of
digits="+a+"]");
else
System.out.println("Not an SUPERSPY Number"+" "+"[Sum of the digits="+s+", Number of
digits="+a+"]");
} //Main ends
} //Class ends
Variable Descriptions
int n To store the number entered by the user
int c to store the copy of n
int s to find and store the sum
int r To extract and store the digit from the number
entered Question 2: Define a
int a to count and store the number of digits class to accept a 3-
digit number and check whether it is a Duck Number or
not.
import java.util.*;
public class DuckNmber
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a 3 digit Number");
int n=sc.nextInt();
int r=0;
if(n>=100&&n<=999)
{
while(n>0)
{
r=n%10;
if(r==0)
break;
n=n/10;
} //While ends
if(r==0)
System.out.println("Duck number");
else
System.out.println("Not a Duck number");
} //If ends
else
System.out.println("Invalid");
} //Main ends
}//Class ends
Variable Descriptions
int To store the number entered by the
n user
int To extract the last digit from the
r number entered
Question 3: Write a menu driven class (using switch case)
to perform the following:
(i) Input a Binary number (base 2) and convert it into its Decimal
equivalent (base 10)
(ii) Input a Decimal number (base 10) and convert it into its
Binary equivalent (base 2)
import java.util.Scanner;
public class Conversion
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 for Binary to Decimal Conversion");
System.out.println("Enter 2 for Decimal to Binary Conversion");
int ch=sc.nextInt();
switch(ch)
{
case 1:
{
System.out.println("Enter the number");
int n1=sc.nextInt();
int p=0,c=0,r;
while(n1>0)
{
r=n1%10;
c=c+r*(int)Math.pow(2,p);
p++;
n1=n1/10;
}
System.out.println("The Converted number is: "+c);
}
break;
case 2:
{
System.out.println("Enter the number");
int n2=sc.nextInt();
int p;
String s=" ";
while(n2>0)
{
p=n2%2;
s=String.valueOf(p)+s;
n2=n2/2;
}
System.out.println("The Converted number is: "+s);
}
break;
default:
System.out.println("Invalid choice");
}//Switch ends
}//Main ends
}//Class ends
Variable Descriptions
int ch To store the choice entered by the user
int n1 To store the number entered by the user
int p It is the power
int c To convert the number in the asked form and store it
int d To extract and store the last digit from the number
int n2 To store the number entered by the user
int p To calculate and store the remainder of the number ,divided by 2
int s To convert the number in the asked form and store it
Question 4: Write a program to generate and print all the
Special Armstrong numbers from 500 and 2000.
Variable Descriptions
int i To work out the loop
int c To store the copy of the number
int d To store another copy of the number
int s To calculate and find the sum
int a To count the number of digits
int r To extract the last digit from the number
Question 5: Design a class to overload a function
sumseries() as follows:
(2) void sumseries(int start, int end)- to calculate the sum of all
prime numbers in the range between start and end.
Variable Descriptions
double sum To calculate and store the sum of the series
int s To change the sign
int s To calculate and store the sum
int c Used as a check mark
int i To workout the loop
Question 6:Design a class to overload a function joystring()
as follows:
(1) void joystring(String s)-with one string argument in lowercase, change the first letter of
every word to uppercase. Display the new string.
Sample Input: cloud computing means internet based computing
Output: Cloud Computing Means Internet Based Computing
(2) void joystring(String str, int p)- with one string argument and one integer argument. It
displays all the uppercase character if p is 1(one) otherwise it displays all the lowercase
characters.
Variable Descriptions
String r To store a string
booleancond To check if the condition is true or false
int i To workout the loop
char c To store and check the character from the string entered
String r To store a string
char c To store and check the character from the string entered
int i To workoutthe another loop
char c To store and check the character from the string entered
Question 7:Write a menu driven program to print the given
pattern according to the user’s choice.
(i) 1(ii) A B C D E
** ABCD
23 ABC
*** AB
456 A
import java.util.*;
public class my_program
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 for Numerical Pattern");
System.out.println("Enter 2 for Alphabetical Pattern");
System.out.println("Enter your choice");
int ch=sc.nextInt();
switch(ch)
{
case 1:
{
int k=1;
for(int i=1;i<=5;i++)
{
if(i%2==1)
{
for(int j=1;j<=(i+1)/2;j++)
{
System.out.print(k+"");
k++;
}
}
else
{
for(int j=1;j<=(i+2)/2;j++)
{
System.out.print("*"+ "");
}
}
System.out.println();
}
}
break;
case 2:
{
for(int i=69;i>=65;i--)
{
for(int j=65;j<=i;j++)
{
System.out.print((char)j+"");
}
System.out.println();
}
}
break;
default:
System.out.println("Invalid choice");
}//Switch ends
}//Main ends
}//Class ends
Variable Descriptions
int ch To ask the choice from the user
int i To workout the outer loop
int j To workout the inner loop
int k To print the pattern
int i To workout the outer loop
int j To workout the inner loop
Question 8:Write a program to input a sentence and
display the word in the sentence that contains the
maximum number of characters.
import java.util.Scanner;
public class Mnc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Sentence:");
String s=sc.nextLine();
String[]w=s.split(" ");
String l=" ";
int f=0;
for(int i=0;i<w.length;i++)
{
if(w[i].length()>f)
{
f=w[i].length();
l=w[i];
}
}
System.out.println("The word that contains the maximum number of characters is: " +l);
}//Main ends
}//Class ends
Variable Descriptions
String To store the string entered by the user
s
String[ To split the whole string and store the particular string
]w
String To store the final resulted string
l
int f To check and store the length of the string
int i To workout the loop
Question 9:Define a class to accept a string and convert
the same to uppercase, create and display the new
string by replacing each vowel by immediate next
character and every consonant by the previous
character. The other characters remain the same.
import java.util.Scanner;
public class acc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a String(Sentence): ");
String str=sc.nextLine();
String con="";
for(int i=0;i<str.length();i++)
{
char c=Character.toUpperCase(str.charAt(i));
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{
con+=(char)(c+1);
}
else if(Character.isLetter(c))
{
con+=(char)(c-1);
}
else
{
con+=c;
}
} System.out.println("The Transformed String is: " +con);
}//Main ends
}//Class ends
Variable Descriptions
String str To store the string entered by the user
String con To store the final resulting string
char c To store and check the character of the string
int i To workout the loop
import java.util.Scanner;
public class frequency
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a sentence:");
String str=sc.nextLine();
System.out.print("Enter the word whose frequency is to be searched:");
String w=sc.nextLine();
String word[]=str.split(" ");
int f=0;
for(int i=0;i<word.length;i++)
{
if(word[i].equals(w))
{
f++;
}
}
System.out.println("The frequency of the word "+w+" is: "+f);
}//Main ends
}//Class ends
Variable Descriptions
String To store the string entered by the user
str
String w To store the word entered by the user whose frequency is to
be searched
String To store the main string and store its constituent words
word
int f To store the frequency
int i To workout the loop
Sample Input: Enter the sentence: Stars shine bright at night sometimes
Output: Stars, sometimes
import java.util.Scanner;
public class SameCharacterWords
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
String s=sc.nextLine();
s=s+" ";
s=s.toUpperCase();
String w="";
int l=s.length(); char c;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{
w+=c;
}
else
{
if(w.charAt(0)==w.charAt(w.length()-1))
{
System.out.print(w+" ");
}
w="";
}
}
}//Main ends
}//Class ends
Variable Descriptions
String To store the string entered by the user
s
String To store the final resulting string
w
int l To store the length of the string
char c To store the character of the string
int i To workout the loop
Question 12:Define a class to accept the names of 20
students in a one-dimensional array.Sort the names in
ascending order using the Bubble sorttechnique, and display
the students' names in sorted order.
import java.util.Scanner;
public class Sort
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String names[]=new String[20];
System.out.println("Enter the names of 20 students:");
for (int i=0;i<20;i++)
{
names[i]=sc.nextLine();
}
for(int i=0;i<names.length-1;i++)
{
for(int j=0;j<names.length-1- i;j++)
{
if(names[j].compareTo(names[j + 1])>0)
{
String sort=names[j];
names[j]=names[j+1];
names[j+1]=sort;
}
}
}
System.out.println("Names of students in ascending order:");
for (int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}//Main ends
}//Class ends
Variable Descriptions
String To declare an array of size 20
names[]
String sort To swap the values(student names) and arrange them in
ascending order
int i To ask the user to input values in the array
int i To workout the outer loop
int j To workout the inner loop
int i To print the names in ascending order
Question 13:Define a class to accept the marks secured in
“Computer Applications” by 20 students of a class in a
single dimensional array. Sort the marks obtained in
descending order usingSelection sort technique, and display
the marks of students in sorted order.
import java.util.Scanner;
public class Marks_comp_app
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m[]=new int[20];
System.out.println("Enter the marks of 20 students in Computer Applications:");
for(int i=0;i<20;i++)
{
m[i]=sc.nextInt(); }
for(int i=0;i<m.length-1;i++)
{
int des=i;
for(int j=i+1;j<m.length;j++)
{
if(m[j]>m[des])
{
des=j;
}
}
int sort=m[des];
m[des]=m[i];
m[i]=sort;
}
System.out.println("Marks of students in descending order:");
for(int i=0;i<m.length;i++)
{
System.out.println(m[i]);
}
}//Main ends
}//Class ends
Variable Descriptions
int m[] To declare an array of size 20
int i To ask the user to input values(marks of students) in the array
int i To workout the outer loop
int des To store the value of i and then compare it with with j
int j To workout the inner loop
int sort To swap the values(student names) and arrange them in
descending order
Question 14: Write a program to search for an integer value
input by the user in the sorted list given below
usingBinary search technique. If found display “Search
successful” and the element, otherwise display “Search
Unsuccessful”
{31, 36, 45, 48, 50, 51, 67, 78, 89, 90}
import java.util.Scanner;
public class BinarySearch
{
public static int binarySearch(int arr[],int x)
{
int low=0;
int high=arr.length-1;
while(low<=high)
{
int mid=(low+high)/2;
if(arr[mid]==x)
{
return mid;
}
else if(arr[mid]<x)
{
low=mid + 1;
}
else
{
high=mid-1;
}
}
return -1;
}
public static void main(String[] args)
{
int arr[]={31,36,45,48,50,51,67,78,89,90};
Scanner sc=new Scanner(System.in);
System.out.print("Enter the integer value to search: ");
int x=sc.nextInt();
sc.close();
int result=binarySearch(arr,x);
if(result!=-1)
System.out.println("Search Successfull Element found at index: " + result + ".");
else
System.out.println("Search Unsuccessful.");
}//Main ends
}//Class ends
Variable Descriptions
int To initialize an array of 10 terms
arr
int x To store the integer value i.e. to be searched
int It stores the first term of the array and then goes on increasing
low its value one by one until the desired term is found
int It stores the last term of the array and then goes on decreasing
high its value one by one until the desired term is found
int To find and store the middle term of the array
mid
Question 15:Define a class to accept values into 4×4 array
and find and display the sum of each row.
1 2 3 4 The Sum of row 1 :10
5 6 7 8 The Sum of row 2 :26
9 10 11 12 The Sum of row 3: 42
13 14 15 16 The Sum of row 4: 85
import java.util.*;
public class array_4x4
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][]=new int[4][4];
System.out.println("Enter the values into the 4x4 array:");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}
for (int i = 0; i< 4; i++)
{
int sum = 0;
for (int j = 0; j < 4; j++)
{
sum += a[i][j];
}
System.out.println("The Sum of row " + (i + 1) + " :" + sum);
}
}//Main ends
}//Class ends
Variable Descriptions
int To declare a two dimensional array of size 4x4
a[][]
int i To workout the external loop to give values in the array
int j To workout the internal loop to give values in the array
int i To workout the outer loop to find the sum
int To calculate and store the sum of the rows
sum
int j To workout the inner loop to find the sum
Question 16:Define a class to accept the names of 10
students in an array and check for the existence of the
given name in the array using “Linear search
technique”, if found print the position of the name, if
not found print the appropriate message. Also print the
names which begins with the word“Mr”.
import java.util.Scanner;
public class hell
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String n[]=new String[10];
System.out.println("Enter the names of 10 students:");
for (int i=0;i<10;i++)
{
n[i]=sc.nextLine();
}
System.out.print("Enter the name to search: ");
String s=sc.nextLine();
int a=-1;
for(int i=0;i<10;i++)
{
if(n[i].equals(s))
{
a=i;
break;
}
}
if(a==-1)
{
System.out.println("Search Unsuccessful: " + s + " not found");
}
else
{
System.out.println("Search successful: " + s + " found at position " + (a+1));
}
System.out.println("Names beginning with 'Mr':");
for(int i=0;i<10;i++)
{
if(n[i].startsWith("Mr"))
{
System.out.println(n[i]);
}
}
}//Main ends
}//Class ends
VariableDescri
ptions
String n[] To declare an array of size 10
int i To accept values in the array
String s To store the name to be searched
int a It is used as a flag
int i To workout the loop
int i To print the array element which starts with Mr
Question 17: Define a class to accept values into a 3×3
array and check if it is a special array. An array is a
special array if the sum of the even elements = sum of
the odd elements.
import java.util.*;
public class Spl_array
{
public static void main(String args[])
{
int a[][]=new int[3][3];
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values in the 3x3 array");
int se=0,so=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
if(a[i][j]%2==0)
se=se+a[i][j];
else
so=so+a[i][j];
}
}
if(se==so)
System.out.println("Special Array");
else
System.out.println("Special Array");
}//Main ends
}//Class ends
Variable Descriptions
int a[][] To accept and store values in 3x3 array
int se To find and store the sum of the even terms
int so To find and store the sum of the odd terms
int i To workout the outer loop
int j To workout the inner loop
Question 18: Write a program to store the numbers in 4×4
array in double dimensional array and display the
largest and the smallest value within the array.
Example: Input: Output:
2 23 4 The highestvalue in the array is :120
13
The smallest value in the array is :2
61 71 18
52
10 11 12
95 0 0 0
41 15 16 import java.util.*;
13
public class aray
{
public static void main(String args[])
{
int arr[][]=new int[4][4];
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values in the 4x4 array");
int a,b;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
arr[i][j]=sc.nextInt();
}
}
int l=arr[0][0],s=arr[0][0];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
a=arr[i][j];
b=arr[i][j];
if(a>l)
l=a;
else if(b<s)
s=b;
}
}
System.out.println("The largest value within the array: "+l);
System.out.println("The smallest value within the array: "+s);
}//Main ends
}//Class ends
Variable Descriptions
int a[][] To declare an array of size 4x4
int a To calcultate and store the highest value
of the array
int b To calculate and store the lowest value of
the array
int i To workout the outer loop
int j To workout the inner loop
int l To compare the values and print them
Question 19: A book seller maintains records of books
belonging to the various publishers. He uses a class
with the specifications given below:
import java.util.Scanner;
public class Stock
{
String title;
String author;
String pub;
int noc;
Stock()
{
title="";
author="";
pub="";
noc=0;
}
public void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Title of the book: ");
title=sc.nextLine();
System.out.println("Enter the Author's name: ");
author=sc.nextLine();
System.out.println("Enter the Publisher's name: ");
pub=sc.nextLine();
System.out.println("Enter the Number of copies: ");
noc=sc.nextInt();
}
public void purchase(String t,Stringa,Stringp,int n)
{
if(title.equals(t)&&author.equals(a)&&pub.equals(p))
{
if(noc>=n)
{
noc-=n;
System.out.println("Purchase successful Remaining copies: " + noc);
}
else
System.out.println("Book is not available or Stock is under flowing.");
}
}
public static void main(String args[])
{
Stock ob=new Stock();
ob.getdata();
ob.purchase("Halloween Havoc", "Shaurya Gupta ", "P.T. Publications ", 18);
}//Main ends
}//Class ends
Variable Descriptions
String To store the title of the book
title
String To store the author name of the book
author
String To store the publisher of the book
pub
int noc To store the publisher of the book
Define a class SmartShop with the above specifications and write the
main method to create an object of the class and call the above member
methods.
import java.util.*;
public class SmartShop
{
String name;
double price,netamount;
SmartShop()
{
name="";
price=0.0;
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Name: ");
name=sc.nextLine();
System.out.println("Enter the Price of the item: ");
price=sc.nextDouble();
}
void calculate()
{
if(price<=15000)
netamount=price-(0.05*price);
else if(price>=15001&&price<=25000)
netamount=price-(0.08*price);
else if(price>=25001&&price<=50000)
netamount=price-(0.10*price);
else
netamount=price-(0.12*price);
}
void display()
{
System.out.println("Item Name"+"\t"+"Item Price"+"\t"+"Net Amount");
System.out.println(name +"\t"+ price +"\t"+ netamount);
}
public static void main(String args[])
{
SmartShopob=new SmartShop();
ob.accept();
ob.calculate();
ob.display();
}//Main ends
}//Class ends
Variable Descriptions
String name To store the name of the item purchased
Double price To store the price of the item purchased
Doublenetam To store the price of the item purchased
ount
REFRENCES
~ TOTAL COMPUTER APPLICATIONS 10 ICSE [MORNING
STAR]
~ COPILOT BING
~ CHATGPT-4