Found 33676 Articles for Programming

Python Rational numbers (fractions)

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

2K+ Views

Any number which can be expressed as a quotient or fraction in the form of p/q is called a rational number. The fractions module of Python library provides functionality for rational number arithmetic.This module defines a Fraction class. Its object can be constituted in various ways as below −Fraction(num, denom)The first version of Fraction constructor receives two parameters for numerator and denominator. Default numerator is 0 and default denominator is 1. Value of denominator = 0 throws ZeroDivisionError.>>> from fractions import Fraction >>> n1 = Fraction(2, 5) >>> n1 Fraction(2, 5) >>> n2 = Fraction(6, 15) >>> n2 Fraction(2, 5) ... Read More

Mathematical statistics functions in Python

Samual Sam
Updated on 30-Jun-2020 12:48:26

576 Views

The statistics module of Python library consists of functions to calculate statistical formulae using numeric data types including Fraction and Decimal types.Following import statement is needed to use functions described in this article.>>> from statistics import *Following functions calculate the central tendency of sample data.mean() − This function calculates the arithmetic mean of data in the form of sequence or iterator.>>> from statistics import mean >>> numbers = [12, 34, 21, 7, 56] >>> mean(numbers) 26The sample data may contain Decimal object or Fraction object>>> from decimal import Decimal >>> numbers = [12, 34, 21, Decimal('7'), 56] >>> mean(numbers) Decimal('26') ... Read More

Print leading zeros with C++ output operator

Aman Kumar
Updated on 12-Jun-2025 17:44:24

7K+ Views

Here we will see how to print leading zeros as output in C++. We know that if we directly put some zeros before some numeric values, then all zeros are discarded, and only exact numbers are printed.Printing Leading Zeros with C++ Output Operator We can manipulate the output sequence in C++ by utilizing the iomanip header. In this header, we have two manipulators, setw() and setfill(). The setw() function is used to create space between the previous text and the current text, and then we use setfill(char) to add characters to that field. Example of Printing Leading Zeros In this ... Read More

Ternary operator ?: vs if…else in C/C++

Aman Kumar
Updated on 09-Jun-2025 18:40:42

780 Views

In C/C++, both ternary operator and if-else statements are conditional statements: A condition statement expresses the relationship between two ideas or events. Where one is dependent on the other. If the condition is true, the if statement will return; otherwise, the else statement will return. Ternary Operator We know that the ternary operator is the conditional operator. Using this operator, we can check some conditions and do some tasks according to those conditions. Without using the ternary operator, we can also use the if-else conditions to do the same. Following is The Syntax of the Ternary Operator: condition ? expression_if_true ... Read More

C Program to find sum of two numbers without using any operator

Aman Kumar
Updated on 09-Jun-2025 18:40:20

1K+ Views

In this article, we Implement the C program to find the sum of two numbers without using any Operator. Find Sum of Two Numbers Without Using Any Operator in C This problem is tricky. To solve this problem we are using the minimum width features of the printf() statement. We can use '*' which indicates the minimum width of output. For example, in this statement "printf("%*d", width, num);", the specified 'width' is substituted as *, and 'num' is printed within the minimum width specified. If the number of digits in 'num' is less than the provided 'width', the output will ... Read More

Execution of printf with ++ operators in C

Aman Kumar
Updated on 09-Jun-2025 18:40:02

2K+ Views

In the C programming language, the printf() function is used to print ("character, string, float, integer, octal, and hexadecimal values") to the output screen. To demonstrate the value of an integer variable, we use the printf() function with the %d format specifier. Let's see a C statement and guess the result: printf("%d %d %d", i, ++i, i++); Here, in the above C statement, both 'i' and 'i++' are in the argument list; this statement causes undefined behaviour. The sequence in which the arguments are evaluated is not specified; orders may alter depending on the compiler. At different times, a ... Read More

Stack Unwinding in C++

Aman Kumar
Updated on 06-Jun-2025 14:11:37

2K+ Views

When we call some functions, the call stack stores the address, and after returning, it pops the address to resume work. What is Stack Unwinding Stack unwinding refers to the process of removing function call frames from the function call stack during runtime, wherein local objects are destroyed in the reverse order of their construction. Why Stack Unwinding Occurs? Stack unwinding occurs when an exception occurs in a function and is not handled immediately in that function. The program control goes back through each function step by step, cleaning up any temporary resources used by each function. How Unwinding Works? ... Read More

What happen if we concatenate two string literals in C++?

Aman Kumar
Updated on 06-Jun-2025 14:10:58

152 Views

In this article, we will explore what happens when we concatenate two string literals in C++. There are two important points to remember when concatenating strings in C++. Concatenation is another property of the string and string literals: let's see the following two properties below: If x + y is the expression of string concatenation, where x and y are both strings. Then the result of this expression will be a copy of the characters of string x followed by the characters of string y. Either x or y can be ... Read More

StringStream in C++ for Decimal to Hexadecimal and back

Aman Kumar
Updated on 06-Jun-2025 14:18:31

896 Views

In this article, we will see how to convert a Decimal to a Hexadecimal string and versa in C++. For this conversion, we are using the string stream feature of C++. You can use StringStreams for formatting, parsing, converting a string into numeric values, and more. The Hex is an I/O manipulator. It takes a reference to an I/O stream as a parameter and returns a reference to the string after manipulating it. Implementation of converting Decimal to Hexadecimal In the following example, we will see how to convert decimal numbers or hexadecimal numbers in C++. #include #include ... Read More

Stringize and Token-pasting operator in C

Aman Kumar
Updated on 06-Jun-2025 14:18:52

3K+ Views

In this article we will see what are the Stringize operator and Token Pasting operator in C. The stringize operator (#) and the token pasting operator (##) are preprocessor operators used within macros for text manipulation. It sends commands to compiler to convert a token into string. Stringize Operator (#) The stringize operator (#) is a preprocessor operator that converts the micros parameter into a string literal. The preprocessor encloses the actual argument passed to the macro in double quotes, effectively convert it into a string. Syntax Following is the syntax of Stringize Operator: #define MACRO_NAME(arg) #arg ExampleIn ... Read More

Advertisements