Curly Braces in C++ Best Practices

Revathi Satya Kondra
Updated on 11-Apr-2025 17:32:54

1K+ Views

In C/C++, omitting the curly braces assumes that only the first statement is the block and this leads to quite a few issues during debugging, as code is pretty tough to read and comprehend. Curly braces help us prevent errors and confusion, which also helps with the flow of the program. Proper Use of Curly Braces In C++, we can omit the curly braces after if-else statements, or after any loop. If we do not use curly braces, then only one statement after the if-else or loop will be considered under that block. Syntax The following is the syntax: − ... Read More

C++ Program to Print Odd Numbered Levels of a Tree

Revathi Satya Kondra
Updated on 11-Apr-2025 17:32:01

292 Views

In C++, to print the odd-numbered levels of a binary tree, the levels are numbered from the root as Level 1. The odd-numbered levels are Level 1, Level 3, and so on. This program prints the nodes present at these odd levels. It uses level-order traversal to process the tree level by level and prints the nodes found at these levels. Algorithm to Print only Odd Numbered Levels of a Tree Following is the Algorithm to print odd numbered levels of a tree − Create a structure for tree nodes with data and pointers to left ... Read More

Size of an Empty Class in C++

Samual Sam
Updated on 11-Apr-2025 17:17:28

434 Views

The size of an object of an empty class in C++ is 1 byte as it allocates one unique address to the object in the memory. The size can not be 0, as the two objects can not have same memory allocation. In this article, we will see an example of checking the size of an object of an empty class in C++. Demonstrating Size of an Empty Class In this example, we have two C++ classes. One class is an empty class while other is not an empty class. We have printed the size of objects of both the ... Read More

C++ Program to Convert Fahrenheit to Celsius

Ravi Ranjan
Updated on 11-Apr-2025 17:17:03

5K+ Views

To convert fahrenheit to celsius, use a simple mathematical formula. In this article, we are going to discuss the conversion formula, a simple example, and an easy example code. We have been given a temperature in fahrenheit and our task is to convert the given temperature into celsius using C++. Understanding Fahrenheit to Celsius Formula The formula for converting the given temperature from fahrenheit to celsius is as follows: celsius = (fahrenheit - 32.0) * 5.0 / 9.0; Let's see an example to convert the temperature given in fahrenheit to celsius using the above formula: Input: Temperature in ... Read More

Find GCD and LCM of N Numbers in C++

Ravi Ranjan
Updated on 11-Apr-2025 17:15:30

4K+ Views

The gcd refers to 'Greatest Common Divisor', i.e. greatest common number which can divide all the given numbers. The lcm refers to the 'Least Common Multiple' i.e. the lowest common multiple of all the numbers. To find the gcd and lcm of n numbers in C++, we can use various approaches like iterative approach, or built-in C++ functions. In this article, we are having 'n' number of elements, our task is to find the gcd and lcm of n number of elements using C++. Example Here is an example of GCD and LCM of 4 numbers: Input: Numbers = ... Read More

C++ Program to Generate Random Number

Ravi Ranjan
Updated on 11-Apr-2025 17:14:54

5K+ Views

Random numbers are the numbers that are unpredictable and we do not use any pattern or algorithm for choosing them. They are used to bring randomness such as while shuffling data or used in games. To generate a random number in C++, the built-in functions from different libraries such as or can be used. In this article, we will understand four different approaches for generating a random number. The approaches are listed below. Using rand() with srand() Functions Using rand() with Range Limit Using random_device ... Read More

Convert Double to String in C++

Ravi Ranjan
Updated on 11-Apr-2025 17:13:47

31K+ Views

A double datatype is used to represent a decimal or exponential value and string represents a sequence of characters. In this article, we are having a double value and our task is to convert the given double into string. Here is a list of approaches to convert double to string in C++ that we will be discussing in this article with stepwise explanation and complete example codes. Using to_string() Function Using ostringstream Using stringstream Using sprintf() Function Using ... Read More

Can Python Functions Run in HTML Like JavaScript?

Sarika Singh
Updated on 11-Apr-2025 15:31:27

536 Views

Unlike JavaScript, we cannot run Python functions (or scripts) directly in HTML, but we can use tools to make it work. While creating web pages, we use HTML to structure the content and JavaScript to make the page interactive (directly in the browser). We can also write the JavaScript code within an HTML file using the tag. If you are learning Python, you might wonder if you can run Python code the same way inside HTML. The short answer is no. You can't run Python code directly in HTML like JavaScript. Web browsers are designed only to understand HTML, ... Read More

Use Global Variable in a Python Function

Sarika Singh
Updated on 11-Apr-2025 14:55:53

4K+ Views

We can use a global variable inside a Python function by declaring it using the global keyword. This allows us to read or modify a variable that is defined outside the function's local scope. By default, any variable you create or change inside a function is considered a local variable (only used inside that function). If we try to change a global variable without declaring it as global, Python will create a new local variable with the same name, and the actual global variable will remain unchanged. In this article, we will understand what global variables are, how to use ... Read More

Retrieve Source Code from Python Objects

Sarika Singh
Updated on 11-Apr-2025 14:50:14

16K+ Views

Using the inspect module, you can retrieve the source code of a Python function, method, class, or module. The following example demonstrates how to retrieve a function's source code − Example: Retrieve Function Source Code This code defines a simple function my_function(), that accepts two arguments and returns the sum of those arguments. We then retrieve the source code of the my_function function using the inspect.getsource() function and store it in the source code variable. Finally, the source code is output to the console. The inspect.getsource() function operates by reading the function's source code from the file in which it ... Read More

Advertisements