Posts

Search Product Code in Binary File

A binary file named "ABC.DAT" contains the product code (pc), unit price (up) and quantity (q) for number of items. Write a method to accept a product code 'p' and check the availability of the product and display with an appropriate message. The method declaration is as follows: void findpro(int p) import java.io.*; import java.util.Scanner; class ReadData{     public static void main(String[] args){         Scanner in = new Scanner(System.in);         System.out.print("Product code to be searched: ");         int p = Integer.parseInt(in.nextLine());         findpro(p);     }     public static void findpro(int p){         boolean eof = false;         boolean found = false;         try{             FileInputStream fis = new FileInputStream("ABC.DAT");             DataInputStream dis = new DataInputStream(fis);             while(!eof){                 int pc = dis.readInt();                 double up = dis.readDouble();                 int q = dis.r

Matrix Multiplication in Java

Write a Java program to perform matrix multiplication. Multiplication of two matrices is possible only if number of columns in matrix A = number of rows in matrix B. The dimensions of the resulting matrix will be: Number of rows of matrix A × Number of columns of matrix B. import java.util.Scanner; class Multiply{     public static void main(String[] args){         Scanner in = new Scanner(System.in);         System.out.print("Rows of matrix 1: ");         int r1 = Integer.parseInt(in.nextLine());         System.out.print("Columns of matrix 1: ");         int c1 = Integer.parseInt(in.nextLine());         System.out.print("Rows of matrix 2: ");         int r2 = Integer.parseInt(in.nextLine());         System.out.print("Columns of matrix 2: ");         int c2 = Integer.parseInt(in.nextLine());         if(c1 != r2){             System.out.println("Can't multiply!");             return;         }         int a[][] = new int[r1][c1];    

Highest Row-wise and Lowest Column-wise Elements in a Matrix in Java

Write a Java program to declare a matrix a[][] of order m × n, where 'm' is the number of rows and 'n' is the number of columns such that m and n are positive integers. Accept the elements i.e. positive integers into this matrix. Now check and display the highest elements row-wise and the lowest elements column-wise. Test the program for the following data and some random data. import java.util.Scanner; class Matrix{     public static void main(String args[]){         Scanner in = new Scanner(System.in);         System.out.print("m = ");         int m = Math.abs(Integer.parseInt(in.nextLine()));         System.out.print("n = ");         int n = Math.abs(Integer.parseInt(in.nextLine()));         int a[][] = new int[m][n];         int i, j;         System.out.println("Enter matrix elements:");         for(i = 0; i < m; i++){             for(j = 0; j < n; j++){                 a[i][j] = Math.abs(Integer.parseInt(in.nextLine()));        

Unique Digit Number in Java

A unique digit number is a positive integer (without leading zeroes) with no duplicate digits. For example: 7, 135, 214 are all unique digit numbers, whereas 33, 3121, 300 are not unique digit numbers. Given two positive integers m and n, where m < n, write a Java program to determine how many unique digit numbers are there in the range between m and n (both inclusive) and display them. The input consists of two positive integers m and n where both m and n should be < 30000. You are required to display the number of unique digit numbers in the specified range along with their values in the format specified below: Test your program for the following data and some random data. Example 1: INPUT: m = 100 n = 120 OUTPUT: THE UNIQUE DIGIT NUMBERS ARE: 102, 103, 104, 105, 106, 107, 108, 109, 120 FREQUENCY OF UNIQUE DIGIT NUMBERS: 9 Example 2: INPUT: m = 2505 n = 2525 OUTPUT: THE UNIQUE DIGIT NUMBERS ARE: 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2519 FREQUENCY OF UNIQU

Generating Lucky Numbers Program in Java

Consider the sequence of the natural numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... Removing every second number produces: 1, 3, 5, 7, 9, ... Removing every third number produces: 1, 3, 7, 9, ... The process continues indefinitely by removing every 4th number, then 5th number and so on till after a fixed number of steps, certain natural numbers remain and these numbers are known as the lucky numbers. Write a program to generate and print the lucky numbers within the range of N natural numbers. import java.util.Scanner; public class Lucky{     public static void main(String args[]){         Scanner in = new Scanner(System.in);         System.out.print("N = ");         int n = Integer.parseInt(in.nextLine());         int arr[] = new int[n];         for(int i = 0; i < n; i++)             arr[i] = i + 1;          int del = 1;         while(del < n){             for(int i = del; i < n; i += del){                 for(int j = i; j < n - 1; j++){                     arr[j] =

Add Two Binary Numbers in Java

Write a Java program to input two binary numbers from the user, and find and display their sum. Following are the rules for adding binary digits: 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 with carry 1 1 + 1 + 1 = 1 with carry 1 Example: INPUT: 1011 101 OUTPUT: 10000 import java.util.Scanner; class BinaryAddition{     public static void main(String[] args){         Scanner in = new Scanner(System.in);         System.out.print("First binary number: ");         String b1 = in.nextLine();         System.out.print("Second binary number: ");         String b2 = in.nextLine();         while(b1.length() < b2.length())             b1 = "0" + b1;         while(b2.length() < b1.length())             b2 = "0" + b2;         String sum = "";         char carry = '0';         for(int i = b1.length() - 1; i >= 0; i--){             char ch1 = b1.charAt(i);             char ch2 = b2.charAt(i);             if(ch1 == '0' && c

Find the Longest and the Shortest Words in a Sentence in Java

Write a Java Program to input a sentence and find and display the longest and the shortest words from that sentence along with their lengths. import java.util.Scanner; class Find{     public static void main(String[] args){         Scanner in = new Scanner(System.in);         System.out.print("Sentence: ");         String s = in.nextLine().trim().toUpperCase();         char last = s.charAt(s.length() - 1);         if(".!?".indexOf(last) == -1){             System.out.println("Invalid sentence!");             return;         }         String word = new String();         String longest = new String();         String shortest = new String(s);         for(int i = 0; i < s.length(); i++){             char ch = s.charAt(i);             if(Character.isLetterOrDigit(ch))                 word += ch;             else{                 if(longest.length() < word.length())                     longest = new String(word);                 if(shortest.length() > w