Posts

Showing posts from July, 2020

Print all pairs with given sum

Find all pairs of an integer array whose sum is equal to a given number Solution: //Java implementation of // simple method to find // print pairs with given sum. class FindPairsN{ // Returns number of pairs // in arr[0..n-1] with sum // equal to 'sum' static void getPairs(int arr[], int n, int sum) { // int count = 0; // Consider all possible pairs // and check their sums for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (arr[i] + arr[j] == sum) System.out.println("(" + arr[i] + ", " + arr[j] + ")"); } // Main public static void main(String[] arg) { int arr[] = { 1, 5, 7, -1, 5 }; int n = arr.length; int sum = 6; getPairs(arr, n, sum); } }

SE- OOP Practical Lab -Implement a class Complex which represents the Complex Number data type.

Implement a class Complex which represents the Complex Number data type. Implement the following operations:  1. Constructor (including a default constructor which creates the complex number 0+0i).  2. Overloaded operator+ to add two complex numbers.  3. Overloaded operator* to multiply two complex numbers.  4. Overloaded << and >> to print and read Complex Numbers. Solution: #include<iostream> using namespace std; class complex { float x; float y; public:  complex() { x=0; y=0; } complex operator+(complex); complex operator*(complex); friend istream &operator >>(istream &input,complex &t) { cout<<"Enter the real part"; input>>t.x; cout<<"Enter the imaginary part"; input>>t.y; } friend ostream &operator <<(ostream &output,complex &t) { output<<t.x<<"+"<<t.y<<"i\n"; } }; complex complex::operator+(complex c) { ...

Find missing number.

Find missing number from given range of 1 to n. Solution: Algorithm: Calculate the sum of first n natural numbers as  total= n*(n+1)/2 create a variable sum to store the sum of array elements. Traverse the array from start to end. Update the value of sum as  sum = sum + array[i] print the missing number as  total – sum // Java program to find missing Number class Main { // Function to find missing number static int findMissingNo(int a[], int n) { int i, total; total = (n + 1) * (n + 2) / 2;//calculates total for original range for (i = 0; i < n; i++) total -= a[i];//this will yield the missing number by continuously subtracting elements return total; // from total } /* program to test above function */ public static void main(String args[]) { int a[] = { 1, 2, 4, 5, 6, 7, 8 }; int miss = findMissingNo(a, 7 ); System.out.println(miss); } }

Convert Decimal to Binary. (using recursion)

Image
Convert Decimal to Binary. (using recursion) Solution: import java.util.*; public class DecimalToBinary { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc= new Scanner(System.in); System.out.println("Enter a valid integer:"); while(sc.hasNextInt()) { int a=sc.nextInt(); printbinary(a); System.out.println("\nEnter a bumber, press q to exit!"); } sc.close(); } public static void printbinary(int b) { if(b>0) { printbinary(b/2); System.out.printf("%d", b%2); } } }

Reverse a linked list. (Using recursion)

Reverse a linked list using recursion using Java. Solution: public class Node { int data; Node next; public Node(int a, Node next) { this.data= a; this.next = next; } } public class Linkedlist { public static void main(String[] args) { // TODO Auto-generated method stub Node node1= new Node(1,null); Node node2= new Node(2, null); Node node3= new Node(3, null); node1.next=node2; node2.next=node3; node3.next=null; printlist(node1); System.out.println("\n*********"); printreverse(node1); } public static void printlist(Node n)  { if(n!=null) { System.out.print(" " + n.data);//will print in order printlist(n.next); } } public static void printreverse(Node n) { if(n!=null) { printreverse(n.next);                          // will stack the nested calls and return the output in reverse...