Skip to content

Commit 5b78cf2

Browse files
Add Identation
2 parents 5a50c43 + ec6e1a2 commit 5b78cf2

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
int find(int a, int parent[]) // recursive function to find parent of a
4+
{
5+
if (parent[a] == a) // if root itself is a parent
6+
return a;
7+
else
8+
return find(parent[a], parent); // to find the parent of the parent
9+
}
10+
11+
void unionn(int a, int b, int parent[], int rank[]) // function to do unionn of a and b
12+
{
13+
int a1 = find(a, parent); // to find parent of a
14+
int b1 = find(b, parent); // to find parent of b
15+
if (a1 == b1) // if both have same parent that is both are in same set
16+
{
17+
printf("both the elements are in same set\n");
18+
return;
19+
}
20+
if (rank[a1] > rank[b1]) // if height of parent of a is greater than height of parent of b
21+
{
22+
parent[b1] = a1;
23+
}
24+
else if (rank[a1] < rank[b1]) // if height of parent of b is greater than height of parent of a
25+
{
26+
parent[a1] = b1;
27+
}
28+
else // if height of both parent is same
29+
{
30+
parent[a1] = b1;
31+
rank[b1]++;
32+
}
33+
}
34+
int main()
35+
{
36+
int n = 10;
37+
int parent[n];
38+
int rank[n];
39+
40+
for (int i = 0; i < 10; i++)
41+
parent[i] = i; // in starting the node is itself the parent
42+
for (int i = 0; i < 10; i++)
43+
rank[i] = 1; // by default the rank of all nodes is 1
44+
45+
unionn(3, 8, parent, rank); // 3 is friend of 8
46+
unionn(3, 6, parent, rank); // 3 is friend of 6
47+
printf("the parent of 8 is %d\n", find(8, parent));
48+
49+
unionn(1, 9, parent, rank); // 1 is friend of 9
50+
unionn(2, 8, parent, rank); // 2 is friend of 8
51+
unionn(5, 8, parent, rank); // 5 is friend of 8
52+
53+
unionn(2, 9, parent, rank); // 2 is friend of 9
54+
printf("the parent of 2 is %d\n", find(2, parent));
55+
printf("the parent of 8 is%d\n", find(8, parent));
56+
57+
if (find(5, parent) == find(7, parent)) // to check whether 5 is friend of 7 or not
58+
printf("5 is a friend of 7\n");
59+
else
60+
printf("5 is not a friend of 7\n");
61+
if (find(3, parent) == find(1, parent)) // to check whether 3 is friend of 1 or not
62+
printf("3 is a friend of 1\n");
63+
else
64+
printf("3 is not a friend of 1\n");
65+
66+
return 0;
67+
}

fibonacci_number/FibonacciNumber.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
#include<stdio.h>
1+
#include <stdio.h>
22

33
long long int FibonacciNumber(long long int n)
44
{
55
//Because first fibonacci number is 0 and second fibonacci number is 1.
6-
if(n==1 || n==2)
6+
if (n == 1 || n == 2)
77
{
8-
return (n-1);
8+
return (n - 1);
99
}
1010
else
1111
{
1212
//store last fibonacci number
13-
long long int a=1;
14-
//store second last fibonacci number
15-
long long int b=0;
13+
long long int a = 1;
14+
//store second last fibonacci number
15+
long long int b = 0;
1616
//store current fibonacci number
1717
long long int nth_Fib;
18-
for(long long int i=3;i<=n;i++)
18+
for (long long int i = 3; i <= n; i++)
1919
{
20-
nth_Fib = a+b;
20+
nth_Fib = a + b;
2121
b = a;
2222
a = nth_Fib;
2323
}
@@ -29,15 +29,15 @@ int main()
2929
{
3030
long long int n;
3131
printf("Enter a Number : ");
32-
scanf("%lli",&n);
33-
if(n<1)
32+
scanf("%lli", &n);
33+
if (n < 1)
3434
{
3535
printf("Number must be greater than 0");
3636
}
3737
else
3838
{
3939
long long int nth_Fib = FibonacciNumber(n);
40-
printf("Fibonacci Number is %lli",nth_Fib);
40+
printf("Fibonacci Number is %lli", nth_Fib);
4141
}
4242
return 0;
43-
}
43+
}

0 commit comments

Comments
 (0)