Posts

Showing posts from August, 2022

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

Adders in Digital Logic

Image
Definition of Half Adder A half adder is a logic circuit that adds two bits. It generates two outputs: SUM and CARRY. The boolean equations for SUM and CARRY are: Equation SUM = A  ⊕ B = A'B + AB' CARRY = A . B Truth Table A B CARRY SUM 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 Following is the logic circuit diagram for half adder: Circuit Diagram Definition of Full Adder A full adder is a logic circuit that adds three bits. It generates two outputs: SUM and CARRY. Equation SUM = A  ⊕   B  ⊕ C = (A'B'C + A'BC' + AB'C' + ABC) CARRY = AB + BC + CA Truth Table A B C CARRY SUM 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1 Circuit Diagram

Merge Sort Program in Java

The Merge Sort is a sorting algorithm which is based on the divide-and conquer approach. It works as follows: It divides a list into two halves. Sort the two halves recursively using merge sort. Merge the two sorted halves to form the final sorted list. Below is the Java program that implements merge sort on a list of integers stored in an array: import java.util.Scanner; class MergeSort{     int arr[];     int capacity;     public MergeSort(int size){         capacity = size;         arr = new int[capacity];     }     public void input(){         Scanner in = new Scanner(System.in);         System.out.println("Enter " + capacity + " elements:");         for(int i = 0; i < arr.length; i++)             arr[i] = Integer.parseInt(in.nextLine());     }     public void recur(int a[], int lower, int upper){         if(lower == upper)             return;         else{             int mid = (lower + upper) / 2;             recur(a, lower, mid);             recur(a, mid

Binary Search Tree Program in Java

A binary tree  is a non-linear data structure. In a binary tree, each node can have a maximum of two children. A binary tree in which its information is sorted in inorder way is a binary search tree (BST), meaning, the value of each node is greater than or equal to every value in its left subtree, and is less than every value in its right subtree. The depth of a node is the number of edges in the path from the root to that node. The height of a node is the number of edges in the longest path from that node to a leaf node. The size of a tree is the count of the total number of nodes in it. A binary tree is of degree 2 because it can have a maximum of two children. A node with no successor is a leaf node. The internal nodes are the ones with at least one successive node. A line connecting a node to its successor is an edge . A sequence of continuous edges make a path . Two nodes with the same parent are siblings . A node with no parent is the root . Every node other than the root c