27/03/2023

C++ Program to Find Largest of Three Numbers Using IF-ELSE
C++ Program to Find Largest of Three Numbers Using If-Else Statements. Write a program to find the largest of three numbers in C++. Largest of 3 numbers. Logic Is very simple just follow the below simple steps. as we know there are 3 numbers which means one number is greater than the rest of the two numbers so for this problem, we can take an example and with help of an example, we can find the greatest or biggest and largest number among three numbers.

Note: This program may misbehave if you change the data type of number, For running and executing this program all data types should be the same. Ex float, long and double.

C++ Program to Find Largest of Three Numbers Using IF-ELSE


#include <iostream>
using namespace std;
int main()
{
	int a, b, c;

	cout << "Enter The 3 Numbers :\n\n";
	cin >> a >> b >> c;

	if (a > b && a > c)
	{
		cout << "First Number is Largest \n";
	}
	else if (b > a && b > c)
	{
		cout << "Second Number is Largest \n";
	}
	else if (c > a && c > b)
	{
		cout << "Third Number is Largest \n";
	}
	else
		cout << "All Numbers Are Equal \n";
	return 0;

}

Find the Largest of Three Numbers


Step 1

So for this problem, we are taking three numbers 10, 20, and 30. If the first number is greater than the remaining two then print the first one in greater, so according to steps 1 - 10 is not greater than 20 and 30 so the first step is not working now follow step 2.

Step 2

If the Second number is greater than the remaining two then print the Second one in greater, so according to step 2 - 20 is greater than 10 but not greater than 30 so the step fails now follow step 3.

Step 3

If the Third number is greater than the remaining two then print the Third one in greater, so according to steps 1 and step 2 - 30 is greater than 20 and 10 here step 3 working now no need to perform step 4, we get our answer and program will print "Third Number is Greatest".

Step 4

If step 1 to step 3 is not true then all three numbers are equal. This is a special case if all numbers are equal. For this step, if all three numbers are equal like 10, 10, and 10 then the program will print "All Numbers Are Equal".

The Output of the Largest of Three Numbers in C++


The Output of the Largest of Three Numbers in C++

Similar to the Largest of Three Numbers


Write a Program to Print Fibonacci Series in C++ | Example
Program to Print Fibonacci Series in C++. As we know that Fibonacci Series is started with Zero (0) and the next Element is One Then we add the previous two elements and print the next element of the Fibonacci Series. This process repeats up to n times, suppose you want to print a Fibonacci Series in C++ for up to 5 terms then you need to repeat this process up to 5 times and so on.

Fibonacci Series start with a Zero and the next element is one then first we print 0 and 1. Now add two previous elements and print the next element as 0+1=1. repeat the process again and again until you get your answer.

0+1=1
1+1=2
1+2=3
2+3=5
3+5=8
5+8=13
8+13=21
13+21=34. . . .

Fibonacci Series in C++

Fibonacci Series in C++ 


#include <iostream>
using namespace std;
int main()
{
	int a = 0, b = 1, i, n, c;

	cout << "Enter The Number \n\n";	//Enter the number of terms
	cin >> n;

	cout << "\nFibonacci Series Is \n\n";	//Fibonacci Series Is given below
	cout << a << " " << b << " ";

	for (i = 0; i < n; i++)
	{
		c = a + b;	//here adding two previous value
		cout << c << " ";
		a = b;	//swap the value
		b = c;	//here first two value results are stored in 'b' }	//Loop end.
	cout << "\n";
	return 0;

}

Fibonacci Series


Fibonacci Series in C++: In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterized by the fact that every number after the first two is the sum of the two preceding ones.

Fibonacci Series: 1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34.

Often, especially in modern usage, the sequence is extended by one more initial term.

Fibonacci Series: 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34.

The Fibonacci numbers occur in the sums of "shallow" diagonals in Pascal's triangle seen in the given picture

Important Fact about the Fibonacci Series: 'The 'Golden Ratio' or Phi' of any two consequent numbers of the sequence in the Fibonacci Series in C++ is 1.618 or 1.6 (approx) Except for starting 4 terms.

Example: The first 21 Fibonacci numbers Fn for n = 0, 1, 2, …, 20 are given below.

Fibonacci Series: 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765.

3 / 2 = 1.500
5 / 3 = 1.667
8 / 5 = 1.600
13 / 8 = 1.625
21 / 13 = 1.615
34 / 21 = 1.619
55 / 34 = 1.617
89 / 55 = 1.618
144 / 89 = 1.618
233 / 144 = 1.618
377 / 233 = 1.618
610 / 377 = 1.618
987 / 610 = 1.618
1597 / 987 = 1.618
2584 / 1597 = 1.618
4181 / 2584 = 1.618
6765 / 4181 = 1.618

The Output of the Fibonacci Series


The Output of the Fibonacci Series

Similar to Fibonacci Series


26/03/2023

Write a C++ Program to Find Min And Max Elements of a BST
Write a C++ Program to Find the Min And Max Elements of a BST. Height of BST tree, and traverse tree by preorder, inorder and postorder of the binary search tree.

C++ Program to Find Min And Max Elements of a BST


#include <iostream>
using namespace std;
struct BSTNode
{
	int data;
	BSTNode * left;
	BSTNode * right;
};

BSTNode* getnewnode(int data)
{
	BSTNode *temp = new BSTNode();
	temp->data = data;
	temp->left = temp->right = NULL;
	return temp;
}

BSTNode* insertnode(struct BSTNode *root, int data)
{
	if (root == NULL)
	{
		root = getnewnode(data);
	}
	else if (data <= root->data)
	{
		root->left = insertnode(root->left, data);
	}
	else
	{
		root->right = insertnode(root->right, data);
	}

	return root;
}

bool search(BSTNode *root, int data)
{
	if (root == NULL) return false;
	else if (root->data == data) return true;
	else if (data < root->data)
		return search(root->left, data);
	else
		return search(root->right, data);
}

void preorder(BSTNode *root)
{
	if (root == NULL)
		return;
	else
	{
		cout << root->data << " ";
		preorder(root->left);
		preorder(root->right);
	}
}

void inorder(BSTNode *root)
{
	if (root == NULL)
		return;
	else
	{
		inorder(root->left);
		cout << root->data << " ";
		inorder(root->right);
	}
}

void postorder(BSTNode *root)
{
	if (root == NULL)
		return;
	else
	{
		postorder(root->left);
		postorder(root->right);
		cout << root->data << " ";
	}
}

int findmin(BSTNode *root)	//itterative method
{
	if (root == NULL)
	{
		cout << "\nerror: tree is empty ";
		return -1;
	}

	BSTNode *current = root;
	while (current->left)
		current = current->left;
	return current->data;
}

int findmax(BSTNode *root)	//itterative method find the maximum element
{
	if (root == NULL)
	{
		cout << "\n error: tree is empty ";
		return -1;
	}

	BSTNode *current = root;
	while (current->right)
		current = current->right;
	return current->data;
}

int recursive_findMin(BSTNode *root)
{
	if (root == NULL)
	{
		cout << "\n error: tree is empty ";
		return -1;
	}
	else if (root->left == NULL)
		return root->data;
	return recursive_findMin(root->left);
}

int recursive_findMax(BSTNode *root)
{
	if (root == NULL)
	{
		cout << "\n error: tree is empty ";
		return -1;
	}
	else if (root->right == NULL)
		return root->data;
	return recursive_findMax(root->right);
}

int findHeight(BSTNode *root)
{
	if (root == NULL)
		return -1;
	return max(findHeight(root->left), findHeight(root->right)) + 1;
}

int main()
{
	int x;
	struct BSTNode *root = NULL;
	root = insertnode(root, 10);
	root = insertnode(root, 15);
	root = insertnode(root, 9);
	root = insertnode(root, 8);
	root = insertnode(root, 7);
	root = insertnode(root, 6);
	cout << "enter the data to search ";
	cin >> x;
	if (search(root, x) == true)
		cout << "\n data is found ";
	else
		cout << "\n data is not found";
	cout << "\npreorder traversal ";
	preorder(root);
	cout << "\ninorder traversal ";
	inorder(root);
	cout << "\npostorder traversal ";
	postorder(root);
	cout << "\n height of tree= " << findHeight(root);
	cout << "\n minimum element by itterative method: " << findmin(root);
	cout << "\n maximum element by itterative method: " << findmax(root);
	cout << "\n minimum element by recursive method : " << recursive_findMin(root);
	cout << "\n maximum element by recursive method : " << recursive_findMax(root);
	return 0;
}

The Output of Min And Max Elements of Binary Search Tree


The Output of Min And Max Elements of Binary Search Tree

Similar to the Min And Max Elements of a BST


It's All About Magic! HackerEarth Solution in C++
Recently Oz has found a magical string consisting of the single digit "1". After experimenting on the string, Oz found a weird magical property of the string that is whenever he touches the string then each digit "1" of the string changed to the digit "0" and each digit "0" of the string changed to "01". Oz found this property interesting and immediately asked a question to RK: "How many 1's and 0's will be in the magical string if he touches the string M times ?"

Input:
 
The first line contains the number of test cases T. Each test case consists of a positive integer - M.

Output:
 
For each test case output two space-separated integers, the number of 1's and number of 0's in the magical string if Oz
touches the string M times.

Constraints:
 
1<= T <=20
1<= M <=90

Sample Input (Plaintext Link)
 
2
1
2
 
Sample Output (Plaintext Link)
 
0 1
1 1

Submit your solution here: Click here

It's All About Magic! HackerEarth Solution in C++


#include <iostream>
using namespace std;
int main()
{
	int t = 0, m = 1, n, i, j, t1;
	int a1 = 1, a2 = 1;

	cout << "Enter the no of times Sample Input\n\n";
	cin >> n;
	cout << t << "  " << m << endl;
	for (i = 0; i < n; i++)
	{
		cout << a1 << "  " << a2 << endl;
		t1 = a1;
		a1 = a2;
		a2 = a2 + t1;
	}
}

The Output of It's All About Magic! HackerEarth Solution


The Output of It's All About Magic! HackerEarth Solution

Similar to It's All About Magic!


20/03/2023

How to Find Length of String in C++ Without Using Strlen()
How to Find Length of String in C++ Without Using Strlen(). C++ program to find the length of a string without using strlen. Write a C++ program to find a number of characters in a given string without using a library function. Program to find the length of a string taken from the user without using an inbuilt function.

How to Find Length of String in C++ Without Using Strlen()


  • First, take a String input from the user and store the string gets() functions.
  • Now, initialize the FOR LOOP, i = 0.
  • Count string characters from start i = 0 to end ( '\0' ) and Increase the array index by 1.
  • At the end print the value of i. Here we can either use a count variable and increase the count by 1 in each iteration.
  • Here no need to use an extra variable, Print the last index of an array. We can calculate a length of a string.

Length of String in C++ Without Using Library Function


#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main()
{
	/*Find Length of String in C++ Without Using Strlen() */

	cout << "=======================================";
	cout << "\nVisit - www.programmingwithbasics.com";
	cout << "\n=====================================";

	char a[50];
	int i;
	cout << "Enter An Any  String:\t";
	gets(a);

	for (i = 0; a[i] != '\0'; ++i) {}

	cout << "\nLenth Of The Given String Given Below Is\n\n";
	cout << i << endl;
	return 0;

}

The Output of Find Length of String in C++ Without Strlen


The Output of Find Length of String in C++ Without Strlen

Similar to the Length of the String