0% found this document useful (0 votes)
135 views26 pages

Program 1

The document contains 9 Java programs. Program 1 calculates electricity bill based on units consumed. Program 2 calculates total ticket amount for different train coach classes. Program 3 checks if a number is disarium or happy number. Program 4 validates ISBN codes. Program 5 contains patterns. Program 6 overloads a method. Program 7 contains Fibonacci, series and power series calculations. Program 8 overloads a method for number calculations. Program 9 counts repeated characters in a string.

Uploaded by

Shweta S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views26 pages

Program 1

The document contains 9 Java programs. Program 1 calculates electricity bill based on units consumed. Program 2 calculates total ticket amount for different train coach classes. Program 3 checks if a number is disarium or happy number. Program 4 validates ISBN codes. Program 5 contains patterns. Program 6 overloads a method. Program 7 contains Fibonacci, series and power series calculations. Program 8 overloads a method for number calculations. Program 9 counts repeated characters in a string.

Uploaded by

Shweta S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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;

              System.out.println("Bill to pay : " + billpay);


   }
}

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;

public class Example11 {


public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Input a number : ");

int num = sc.nextInt();

int copy = num, d = 0, sum = 0;

String s = Integer.toString(num);

int len = s.length();

while(copy>0)

d = copy % 10;

sum = sum + (int)Math.pow(d,len);

len--;

copy = copy / 10;

if(sum == num)

System.out.println("Disarium Number.");

else

System.out.println("Not a Disarium Number.");

Program 3b)

import java.util.HashSet;
import java.util.Scanner;

import java.util.Set;

public class Example10 {

public static boolean isHappy_number(int num)

Set<Integer> unique_num = new HashSet<Integer>();

while (unique_num.add(num))

int value = 0;

while (num > 0)

value += Math.pow(num % 10, 2);

num /= 10;

num = value;

return num == 1;

public static void main(String[] args)

{
System.out.print("Input a number: ");

int num = new Scanner(System.in).nextInt();

System.out.println(isHappy_number(num) ? "Its a Happy Number" : "Its an Unhappy Number");

Program 4)

import java.util.Scanner;

public class ISBN {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter ISBN code: ");

        long isbnInteger = scanner.nextLong();

        String isbn = isbnInteger + "";

        if (isbn.length() != 10) {

            System.out.println("Ilegal ISBN");

        } else {
            int sum = 0;

            for (int i = 0; i < 10; i++) {

                int digit = Integer.parseInt(isbn.charAt(i) + "");

                sum = sum + (digit * (i + 1));

            }

            if (sum % 11 == 0) {

                System.out.println("Legal ISBN");

            } else {

                System.out.println("Illegal ISBN");

            }

        }

    }

Program 5 )

Pattern 1)

public class MainClass

{
public static void main(String[] args)

int rows = 9;

//Printing upper half of the pattern

for (int i = 1; i <= rows; i++)

//Printing i to rows value at the end of each row

for (int j = i; j <= rows; j++)

System.out.print(j);

//Printing i spaces at the beginning of each row

for (int j = 9; j > i; j--)

System.out.print(" ");

System.out.println();

//Printing lower half of the pattern


for (int i = rows-1; i >= 1; i--)

//Printing i to rows value at the end of each row

for (int j = i; j <= rows; j++)

System.out.print(j);

//Printing i spaces at the beginning of each row

for (int j = 9; j > i; 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;

for(int i = 1; i <= m; i++)

for(int j = 1; j <=i; j++)

System.out.print(j);

for(int k = i - 1; k >=1; k--)

System.out.print(k);

System.out.println();

m = n - 1;

for(int i = m; i >=1; i--)

for(int j = 1; j <= i; j++)

System.out.print(j);

for(int k = i - 1; k >= 1; k--)

System.out.print(k);

System.out.println();

}
}

Pattern 3)

public class AlphabeatTriangle4

public static void main(String[] args)

for(int i = 5; i >= 1; i--) //for 4 loops

{for(int j = 1; j <= 5-i; j++)

System.out.print(" "); //for spaces

for(int j = 1; j <= 2*i-1; j++)

if(j <= i)

System.out.print((char)(char)(j+64)+" "); //for printing values

else

System.out.print((char)(char)(2*i-j+64)+" "); //for printing values

System.out.println(); //for line break

Program 6)

public class methodoverload{

int sum(int a,int b){

int tsum=0;

for(int i=a;i<=b;i++){

if(i%2==0)

tsum=tsum+i;
else

tsum=tsum;

return tsum;

double sum(double n){

double pro=1.0;

for(double i=1.0;i<=n;i+=0.2){

pro=pro*i;

return pro;

int sum(int n){

int s=0;

int d;

while(n>0)

d=n%10;

s=s+d;

n=n/10;

return s;

public static void main(String args[]){

methodoverload m=new methodoverload();


System.out.println(m.sum(5,12));

System.out.println(m.sum(123));

System.out.println(m.sum(1345518264));

Program 7i)

import java.util.Scanner;

public class series {

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

int val=0,val1=1,sum=1;

System.out.println ("Enter Value For n:");

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;

public class SumOfSeries

public static void main(String args[])

Scanner sc = new Scanner (System.in);

int sum =0;

System.out.println("Enter value for n :");

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)

import java.util.Scanner;//Importing Scanner

public class SeriesSum //Creating Class

public static void main(String[] args) //The main function

Scanner sc = new Scanner(System.in); //Creating Scanner Object

//Take User Input for 'a'

System.out.print("Enter the value of 'a': ");

double a = sc.nextDouble();

//Take User Input for number of terms (n)

System.out.print("Enter the number of terms: ");

int n = sc.nextInt();

double Sum = 0; //Initialise Sum to 0

for(int i=0;i<n;i++) //Run a loop n times

//Add Terms to Sum

Sum += Math.pow(-1,i) * Math.pow(a,(2*i+1));

//Print Final Series Sum

System.out.println(Sum);

}
}

Program 8)

public class Overloading {

public void num_calc(int num, char ch) {

if (ch == 's') {

double square = Math.pow(num, 2);

System.out.println("Square is " + square);

} else {

double cube = Math.pow(num, 3);

System.out.println("Cube is " + cube);

}
public void num_calc(int a, int b, char ch) {

if (ch == 'p') {

int product = a * b;

System.out.println("Product is " + product);

} else {

int sum = a + b;

System.out.println("Sum is " + sum);

public void num_calc(String s1, String s2) {

if (s1.equals(s2)) {
System.out.println("Strings are same");

} else {

System.out.println("Strings are different");

Program 9)

import java.util.Scanner;

public class StringOperations {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a String: ");


        String input = scanner.nextLine();

        input = input.toUpperCase();

        int count = 0;

        for (int i = 1; i < input.length(); i++) {

            if (input.charAt(i) == input.charAt(i - 1)) {

                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";
    }

        System.out.println(word+" in Piglatin format is "+piglatin);


  }
}

Program 12)

import java.util.Scanner;

public class Frequency {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter a String: ");

String input = scanner.next();

int[] frequency = new int[65];

for (int i = 0; i < input.length(); i++) {

char ch = input.charAt(i);

int index = ch - 65;

frequency[index]++;

System.out.println("Character - Frequency");

for (int i = 0; i < 26; i++) {

System.out.println((char) (i + 65) + " " + frequency[i]);

Program 13)

import java.util.Scanner;

class SearchMenu

public static void main( )

int counter, num = 10, item, array[],choice;

//Creating array to store the all the numbers

array = new int[num];

Scanner sc = new Scanner (System.in);

System.out.println("Enter 10 integers");
//Loop to store each numbers in array

for (counter = 0; counter < num; counter++)

array[counter] = sc.nextInt();

System.out.println("Enter type of search: 1. Linear/ 2. Binary");

choice = sc.nextInt();

switch (choice)

case 1: System.out.println("Enter the search value:");

item = sc.nextInt();

for (counter = 0; counter < num; counter++)

if (array[counter] == item)

System.out.println(item+" is present at location "+(counter+1));

/*Item is found so to stop the search and to come out of the

* loop use break statement.*/

break;

if (counter == num)

System.out.println(item + " doesn't exist in array.");

case 2:System.out.println("Enter the search value:");


item = sc.nextInt();

int first = 0;

int last = num - 1;

int middle = (first + last)/2;

while( first <= last )

if ( array[middle] < item )

first = middle + 1;

else if ( array[middle] == item )

System.out.println(item + " found at location " + (middle + 1) + ".");

break;

else

last = middle - 1;

middle = (first + last)/2;

if ( first > last )

System.out.println(item + " is not found.\n");

}
Default : System.out.println ("Invalid option");

Program 14)

import java.util.Scanner;

public class Alphabetical_Order

public static void main( )

int n = 50;

String temp;

String names[] = new String[n];

Scanner s1 = new Scanner(System.in);

System.out.println("Enter all the names and weights:");

for(int i = 0; i < n; i++)

names[i] = s1.nextLine();

for (int i = 0; i < n; i++)

for (int j = i + 1; j < n; j++)

if (names[i].compareTo(names[j])>0)

{
temp = names[i];

names[i] = names[j];

names[j] = temp;

System.out.print("Names in Sorted Order:");

for (int i = 0; i < n - 1; i++)

System.out.print(names[i] + ",");

System.out.print(names[n - 1]);

Program 15)

import java.util.Scanner;

public class UpperTriangular

public static void main(String[] args) {

Scanner sc = new Scanner (System.in);

int M [ ] [ ], rows, cols ;

System.out.println ("enter the matrix size :");

int n= sc.nextInt ();

//Initialize matrix n

M= new int [n] [n];


System.out.println ("Enter "+ (n*n) +" elements");

for (int i=0; i<n; i++)

for (int j=0; j<n; j++)

M[ i ] [ j ]= sc.nextInt ();

//Calculates number of rows and columns present in given matrix

rows = M.length;

cols = M[0].length;

if(rows != cols){

System.out.println("Matrix should be a square matrix");

else {

//Performs required operation to convert given matrix into upper triangular matrix

System.out.println("Upper triangular matrix: ");

for(int i = 0; i < rows; i++){

for(int j = 0; j < cols; j++){

if(i > j)

System.out.print(" ");

else

System.out.print(M[i][j] + " ");


}

System.out.println();

You might also like