Program 1
Program 1
Program 1
import java.util.*;
class ComputeElectricityBill
{
public static void main(String args[])
{
long units;
Scanner sc=new Scanner(System.in);
System.out.println("enter number of units");
units=sc.nextLong();
double billpay=0;
if(units<100)
billpay=units*1.20;
else if(units<300)
billpay=100*1.20+(units-100)*2;
else if(units>300)
billpay=100*1.20+200 *2+(units-300)*3;
Program 2)
import java.util.Scanner;
class RailwayTicket {
String name;
String coach;
long mobno;
int amt;
int totalamt;
void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
name = scanner.nextLine();
System.out.print("Enter coach:(Type 1A for first class, 2A for second class, 3A for third class &
SL for Sleeper class) ");
coach = scanner.nextLine();
System.out.print("Enter mobno: ");
mobno = scanner.nextLong();
System.out.print("Enter amt: ");
amt = scanner.nextInt();
}
void update() {
if (coach.equals("1A")) {
totalamt = amt + 700;
} else if (coach.equals("2A")) {
totalamt = amt + 500;
} else if (coach.equals("3A")) {
totalamt = amt + 250;
} else if (coach.equals("SL")) {
totalamt = amt;
}
}
void display() {
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Mobile Number: " + mobno);
System.out.println("Amount: " + amt);
System.out.println("Total Amount: " + totalamt);
}
}
class Driver{
public static void main(String args[]) {
RailwayTicket rt = new RailwayTicket();
rt.accept();
rt.update();
rt.display();
}
Program 3a)
import java.util.Scanner;
String s = Integer.toString(num);
while(copy>0)
d = copy % 10;
len--;
if(sum == num)
System.out.println("Disarium Number.");
else
Program 3b)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
while (unique_num.add(num))
int value = 0;
num /= 10;
num = value;
return num == 1;
{
System.out.print("Input a number: ");
Program 4)
import java.util.Scanner;
public class ISBN {
public static void main(String[] args) {
long isbnInteger = scanner.nextLong();
if (isbn.length() != 10) {
System.out.println("Ilegal ISBN");
} else {
int sum = 0;
}
if (sum % 11 == 0) {
System.out.println("Legal ISBN");
} else {
System.out.println("Illegal ISBN");
}
}
}
Program 5 )
Pattern 1)
{
public static void main(String[] args)
int rows = 9;
System.out.print(j);
System.out.print(" ");
System.out.println();
System.out.print(j);
System.out.print(" ");
System.out.println();
Pattern 2)
class Pat_1_121_12321
{
public static void main(String args[])
int n = 4;
int m = n;
System.out.print(j);
System.out.print(k);
System.out.println();
m = n - 1;
System.out.print(j);
System.out.print(k);
System.out.println();
}
}
Pattern 3)
if(j <= i)
else
Program 6)
int tsum=0;
for(int i=a;i<=b;i++){
if(i%2==0)
tsum=tsum+i;
else
tsum=tsum;
return tsum;
double pro=1.0;
for(double i=1.0;i<=n;i+=0.2){
pro=pro*i;
return pro;
int s=0;
int d;
while(n>0)
d=n%10;
s=s+d;
n=n/10;
return s;
System.out.println(m.sum(123));
System.out.println(m.sum(1345518264));
Program 7i)
import java.util.Scanner;
int val=0,val1=1,sum=1;
int n= sc.nextInt();
for(int i=0;i<n-1;i++)
int k=val+val1;
sum+=k;
val=val1;
val1=k;
}
System.out.println(sum);
}}
Program 7ii)
import java.util.Scanner;
int n= sc.nextInt();
for(int i=2;i<=n;i=i+2)
if(i%4==0){ sum=sum-i;}
else{ sum=sum+i;}
System.out.println(sum);
}
}
Program 7iii)
double a = sc.nextDouble();
int n = sc.nextInt();
System.out.println(Sum);
}
}
Program 8)
if (ch == 's') {
} else {
}
public void num_calc(int a, int b, char ch) {
if (ch == 'p') {
int product = a * b;
} else {
int sum = a + b;
if (s1.equals(s2)) {
System.out.println("Strings are same");
} else {
Program 9)
import java.util.Scanner;
public class StringOperations {
public static void main(String[] args) {
input = input.toUpperCase();
int count = 0;
count++;
}
}
System.out.println(count);
}
Program 10)
import java.util.*;
public class PiglatinWord
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the word to be converted.");
String word=ob.next();
word=word.toUpperCase();
String piglatin="";
int flag=0;
for(int i=0;i<word.length();i++)
{
char x=word.charAt(i);
if(x=='A' || x=='E' || x=='I' || x=='O' ||x=='U')
{
piglatin=word.substring(i)+word.substring(0,i)+"AY";
flag=1;
break;
}
}
if(flag==0)
{
piglatin=word+"AY";
}
Program 12)
import java.util.Scanner;
char ch = input.charAt(i);
frequency[index]++;
System.out.println("Character - Frequency");
Program 13)
import java.util.Scanner;
class SearchMenu
System.out.println("Enter 10 integers");
//Loop to store each numbers in array
array[counter] = sc.nextInt();
choice = sc.nextInt();
switch (choice)
item = sc.nextInt();
if (array[counter] == item)
break;
if (counter == num)
int first = 0;
first = middle + 1;
break;
else
last = middle - 1;
}
Default : System.out.println ("Invalid option");
Program 14)
import java.util.Scanner;
int n = 50;
String temp;
names[i] = s1.nextLine();
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
System.out.print(names[i] + ",");
System.out.print(names[n - 1]);
Program 15)
import java.util.Scanner;
//Initialize matrix n
M[ i ] [ j ]= sc.nextInt ();
rows = M.length;
cols = M[0].length;
if(rows != cols){
else {
//Performs required operation to convert given matrix into upper triangular matrix
if(i > j)
System.out.print(" ");
else
System.out.println();