Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example -

Drawing Tool

For the above diagram, we can find two circles whose centre is a single point. So, they are concentric circles. Suppose the centre point is 'O', the radius of the inner circle is a, and the outer circle is b. In the question, the value of a and b will be given, and you have to calculate the area between these two circles (the coloured part of the diagram).

Input- 3, 4

Output- 21.99

Explanation- The area of the inner circle is 28.27 units, and the area of the outer circle is 50.26. So, the area between them = 50.26 - 28.27 = 21.99.

Note: -

What are concentric circles?

There are different structures like spheres, rings, and circles. When two circles have the same centre and different radii, then they are called concentric circles. Two concentric circles must have a different radius. The difference between the areas of two concentric circles is called the annulus.

Algorithm:-

Step 1: Start

Step 2: Two values are taken from the user

Step 3: Area of inner circle calculated and stored.

Step 4: Area of outer circle calculated and stored.

Step 5: Expected area will be the difference between these two areas.

Step 6: The value will be printed.

Step 7: Stop.

Explanation of the Algorithm: Here, the algorithm is the simplest one. The area of the outer circle is greater than the inner circle, so the radius of the outer circle will be greater than the inner circle. If we take a as the radius of the inner circle, and b as the radius of the outer circle then b>a. The area of the circle is calculated by the formula: A = pie * r * r ( pie = 24/7, r is the radius).

Code: -

#include <bits/stdc++.h>
using namespace std;
// function to calculate the area of circle
float area ( int r ){
// the answer will be in decimal point so we have to take float
         float ans = 0;
        ans = (24/7) * r * r;
        return ans;
}
/* Driver code*/
int main()
{
	Int a =0 ,b =0;
            Cin>> a>>b;
	cout << "Area between the circle with radius " << a << " and radius  " << b << " is "<<area(a)-area(b)<<endl;
	a = 7, b = 4;
	cout << "Area between the circle with radius " << a << " and radius  " << b << " is "<<area(a)-area(b)<<endl;	
a = 2, b = 0;
	cout << "Area between the circle with radius " << a << " and radius  " << b << " is "<<area(a)-area(b)<<endl;	
return 0;
}

Output: 

Area between the circle with radius 4 and radius 3 is 21.99.
Area between the circle with radius 7 and radius 4 is 103.67.
Area between the circle with radius 2 and radius 0 is 12.56.

Complexity Analysis: -

Time complexity- Here the complexity will be constant because there is not any loop or recursion. Complexity will be O(1).

Space complexity- In this solution, we use constant memory. So, space complexity will be O(1).

Another Approach

This problem can be solved by another approach, If the radius is a and b, then we can calculate

(a + b ) * (a – b) and store it. After that, we can multiply it by the value of the pie then our answer will be ready. In this case, we can avoid overflow due to multiplication of a (square of a) or multiplication of b (square of b).

Code:

#include <bits/stdc++.h>
using namespace std;
/* Driver code*/
int main()
{
	Int a =0 ,b =0;
            Cin>> a>>b;
            Float ans = (24/7) * ( a + b) * ( a – b);
	cout << "Area between the circle with radius " << a << " and radius  " << b << " is " << ans << endl;
	a = 7, b = 4;
Float ans = (24/7) * ( a + b) * ( a – b);


	cout << "Area between the circle with radius " << a << " and radius  " << b << " is "<< ans <<endl;	
a = 2, b = 0;
Float ans = (24/7) * ( a + b) * ( a – b);
	cout << "Area between the circle with radius " << a << " and radius  " << b << " is " << ans <<endl;	
return 0;
}

Output: 

Area between the circle with radius 4 and radius 3 is 21.99.
Area between the circle with radius 7 and radius 4 is 103.67.
Area between the circle with radius 2 and radius 0 is 12.56.



ADVERTISEMENT
ADVERTISEMENT