Program 1
Write a program to accept a number and check and display whether it is a spy number or not. (A
number is spy if the sum of its digits equals the product of its digits.)
Example: consider the number 1124,
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 * 1 * 2 * 4 = 8
import java.util.Scanner;
class SpyNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:");
int n = sc.nextInt();
int m = n;
int sum=0, prod=1;
while(n>0){
int r = n % 10;
sum = sum + r;
prod = prod * r;
n = n / 10;
}
if(sum == prod){
System.out.println(m+" is a spy number");
}
else{
System.out.println(m+" is not a spy number");
}
}
}
Program 2
Write a program to input forty words in an array. Arrange these words in descending order of
alphabets, using selection sort technique. Print the sorted array.
import java.util.Scanner;
public class SelectionSort{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String names[] = new String[40];
int n = names.length;
System.out.println("Enter 40 Names: ");
for (int i = 0; i < n; i++) {
names[i] = in.nextLine();
}
for (int i = 0; i < n - 1; i++) {
int max = i;
for (int j = i + 1; j < n; j++) {
if (names[j].compareTo(names[max]) < 0) {
max = j;
}
}
String t = names[max];
names[max] = names[i];
names[i] = t;
}
System.out.println("Sorted Names");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}
Program 3
Write a program to input integer elements into an array of size 20 and perform the following
operations:
(i) Display largest number from the array.
(ii) Display smallest number from the array.
(iii) Display sum of all the elements of the array.
import java.util.Scanner;
public class LargestSmallestSum{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a[] = new int[20];
int n = a.length;
System.out.println("Enter 20 integers:");
for(int i=0; i<n; i++){
a[i] = sc.nextInt();
}
int sum=0, min=a[0], max=a[0];
for(int i=1; i<n; i++){
if(a[i]<min){
min = a[i];
}
if(a[i]>max){
max = a[i];
}
sum = sum + a[i];
}
System.out.println("Largest = "+max);
System.out.println("Smallest = "+min);
System.out.println("Sum = "+sum);
}
}
Program 4
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: s1 ="computer"
Output : o u e
class Overload{
public void check(String str, char ch){
int n = 0;
for(int i=0; i<str.length(); i++){
if(str.charAt(i) == ch){
n++;
}
}
System.out.println("Number of "+ch+
" present is = "+n);
}
public void check(String s1){
s1 = s1.toLowerCase();
for(int i=0; i<s1.length(); i++){
char c = s1.charAt(i);
if(c=='a' || c=='e' || c=='i' ||
c=='o' || c=='u'){
System.out.print(c+" ");
}
}
}
public static void main(String args[]){
Overload ob = new Overload();
ob.check("success", 's');
ob.check("computer");
}
}
Program 5
Using switch statement, write a menu driven program for the following:
(i) To find and display the sum of the series given below:
𝑆 = 𝑥1 − 𝑥2 + 𝑥3 − 𝑥4 + 𝑥5 … − 𝑥20 (where x = 2)
(ii) To display the following series:
1 11 111 1111 11111
For an incorrect option, an appropriate error message should be displayed.
import java.util.Scanner;
class SwitchDemo{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("1. Sum of series\n"+
"2. Display series\n"+
"Enter your choice (1-2):");
int ch = sc.nextInt();
switch(ch){
case 1:
double sum=0, x=2;
for(int i=1; i<=3; i++){
sum = sum + Math.pow(-1, i+1)*Math.pow(x, i);
}
System.out.println("Sum = "+sum);
break;
case 2:
for(int i=1; i<=5; i++){
for(int j=1; j<=i; j++){
System.out.print("1");
}
System.out.print(" ");
}
break;
default:
System.out.println("Invalid choice:"+ch);
}
}
}
Program 6
Define a class ElectricBill with the following specifications:
class : ElectricBill
Instance variables / data member:
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Member methods:
void accept( ) – to accept the name of the customer and number of units consumed
void calculate( ) – to calculate the bill as per the following tariff:
Number of units Rate per unit
First 100 units Rs.2.00
Next 200 units Rs.3.00
Above 300 units Rs.5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void print ( ) - To print the details as follows:
Name of the customer: ………………………
Number of units consumed: ………………………
Bill amount: ………………………
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
class ElectricBill{
private String n;
private int units;
private double bill;
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter customer name: ");
n = in.nextLine();
System.out.print("Enter units consumed: ");
units = in.nextInt();
}
public void calculate() {
if (units <= 100)
bill = units * 2;
else if (units <= 300)
bill = 200 + (units - 100) * 3;
else {
double amt = 200 + 600 + (units - 300) * 5;
double surcharge = (amt * 2.5) / 100.0;
bill = amt + surcharge;
}
}
public void print() {
System.out.println("Name of the customer:" + n);
System.out.println("Number of units consumed: " + units);
System.out.println("Bill amount:" + bill);
}
public static void main(String args[]) {
ElectricBill ob = new ElectricBill();
ob.accept();
ob.calculate();
ob.print();
}
}