Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Sunday, May 12, 2019

how do you write a program to find out total number of square in a chessboard?

Problem statement:
How to find out total number of square in chessboard / any other board of n x n.
  1. class A {
  2.     public static void main(String args[]) {
  3.         int n = 8; // here we can give any value of n
  4.         int n2 = n * (n + 1) * (2 * n + 1) / 6;
  5.         System.out.println(n2);
  6.     }
  7. }
Output:
204
Explanation - for n = 8, it will be 
1 unit - 8 x 8 = 64
2 unit - 7 x 7 = 49
3 unit - 6 x 6 = 36
4 unit - 5 x 5 = 25
5 unit - 4 x 4 = 16
6 unit - 3 x 3 = 9
7 unit - 2 x 2 = 4
8 unit - 1 x 1 = 1
---------------------
total              = 204
Asked in - Synechron technologies

Tuesday, April 23, 2019

Java Loop


  1. import jaa.util.Scanner;

  2. public class LoopAlgorithm {
  3.     public static void main(String[] args) {
  4.         Scanner in = new Scanner(System.in);
  5.         int t = in.nextInt();
  6.         for (int i = 0; i < t; i++) {
  7.             int a = in.nextInt();
  8.             int b = in.nextInt();
  9.             int n = in.nextInt();
  10.             int result = 0;
  11.             for (int j = 0; j < n; j++) {
  12.                 if (j == 0) {
  13.                     result = (result +a + (int) (Math.pow(2, j) * b));
  14.                 } else {
  15.                     result = (int) Math.pow(2, j) * b;
  16.                     System.out.println(result+" ");
  17.                 }
  18.             }
  19.             System.out.print("");
  20.         }
  21.         in.close();
  22.     }
  23. }








How do you print table using loop?

Problem Statement: 
Given an integer, N, print its first 10 multiples. Each multiple N x i (where 1<=i<=10) should be printed on a new line in the form: N x i = result.

Input Format
A single integer, N

Constraints
2<=N<=20

Output Format
Print 10 lines of output; each line i (where 1<=i<=10) contains the result of N x i in the form: 
N x i = result.

Sample Input
2

Sample Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

  1. public class PrintTable {
  2.     public static void main(String[] args) {
  3.         int N = 2;
  4.         for (int i = 1; i <= 10; i++) {
  5.             System.out.println(N + " x " + i + " = " + N * i);
  6.         }
  7.     }
  8. }

Wednesday, April 3, 2019

How do you add two numbers represented by linked lists ??

Problem statement:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.


Example:


Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

class Node {
    int data;
    Node next;

    Node(int data) {

        this.data = data;
    }

    Node() {

    }

    @Override

    public String toString() {
        return "Node{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}

public class AddTwoNumbers {

    static Node head = null;

    public static void main(String[] args) {

        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);

        n1.next = n2;

        n2.next = n3;

        Node n4 = new Node(1);

        Node n5 = new Node(2);
        Node n6 = new Node(3);

        n4.next = n5;

        n5.next = n6;

        Node result = calculate(n1, n4);

        System.out.println(result);
    }

    static Node calculate(Node a, Node b) {

        //        //FIXME: Write your logic
        //  1, 2, 3
        //  1, 2, 3
        int carry = 0;

        while (a != null && b != null) {

            addLast(a.data + b.data);

            a = a.next;

            b = b.next;
        }
        return head;
    }

    public static void addLast(int data) {


        if (head == null) {

            head = new Node(data);
            return;
        }
        Node last = head;
        while (last.next != null) {
            last = last.next;
        }
        last.next = new Node(data);
    }
}

Output:
Node{data=2, next=Node{data=4, next=Node{data=6, next=null}}}

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...