Write a c program to subtract two numbers without using subtraction operator
- #include<stdio.h>
- int a,b;
- int sum;
- scanf("%d%d",&a,&b);
- sum = a + ~b + 1;
- printf("Difference of two integers: %d",sum);
- return 0; }
To add two numbers without using arithmetic operators first create method addNumber() which return sum of two integers. In addNumber() method we will not be using any arithmetic operators. Instead we are using bitwise operator XOR(^) of two bits.
Binary minus operator: This is the subtraction operator, which takes two operands, one on the left and one on the right. The result of the expression is the value of the second operand subtracted from the first operand.
- int multiply(int firstnum, int secondnum){ // If second number is zero then the product is zero.
- if(secondnum == 0){ return 0;
- // Add first num, until second num is equal to zero.
- int firstnum, secondnum, prod;
- scanf("%d %d",&firstnum,&secondnum);
- prod = multiply(firstnum,secondnum);
If you are asked to work out the product of two or more numbers, then you need to multiply the numbers together. If you are asked to find the sum of two or more numbers, then you need to add the numbers together.
There are no official bitwise sum operations in the Math (Bitwise operation - Wikipedia). I think bitwise sum is just an addition operation by bits. So with your question, the results are: 0 + 0 = 0. 1 + 1 = 10 //(the sum is 0 and the carry bit is 1)
What happens if you have two values with no operator and a space in between them and why? Statement: >>> 22 Output: 22 Explanation: If two values are placed together without space and operator between them, then the output would render the same value as the statement.
Swap two numbers without using third variable in java
- class demo {
- public static void main(string arg[]) {
- System.out.println("Before swapping");
- int x = 10;
- int y = 20;
- System.out.println("value of x:" + x);
- System.out.println("value of y:" + y);
- system.out.println("After swapping");
If x and y don't have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.
The expression ((x | y) – (x & y)) is equivalent to x ^ y (finding XOR of two numbers x and y). XOR works by setting the bits which are set in either of one of the given numbers (0 ^ 1 = 1, 1 ^ 0 = 1) and finally taking out the common bits present in both numbers (1 ^ 1 = 0).
Let's see a simple c example to print "hello world" using if statement and without using semicolon.
- #include<stdio.h>
- int main()
- {
- if(printf("hello world")){}
- return 0;
- }
There are several ways to square a number in Python: The ** (power) operator can raise a value to the power of 2. For example, we code 5 squared as 5 ** 2 . The built-in pow() function can also multiply a value with itself.
round() function in Python
Python provides an inbuilt function round() which rounds off to the given number of digits and returns the floating point number, if no number of digits is provided for round off , it rounds off the number to the nearest integer. round() parameters: ..1) number - number to be rounded ..Using split() method :
This function helps in getting a multiple inputs from user . It breaks the given input by the specified separator. If separator is not provided then any white space is a separator. Generally, user use a split() method to split a Python string but one can used it in taking multiple input.Approach :
- Read input number asking for the length of the list using input() or raw_input() .
- Initialize an empty list lst = [] .
- Read each number using a for loop .
- In the for loop append each number to the list.
- Now we use a predefined function sum() to find the sum of all the elements in a list.
- Print the result.
In Python programming, a list is created by placing all the items (elements) inside square brackets [] , separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.). A list can also have another list as an item. This is called a nested list.
Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive. There are 33 keywords in Python 3.7.
Exponentiation is a mathematical operation where a value is multiplied a certain number of times with itself. Let's see how we perform that task in Python. IN THIS ARTICLE: Calculate exponents in the Python programming language. Calculate Python exponents with the ** operator.
The python != ( not equal operator ) return True, if the values of the two Python operands given on each side of the operator are not equal, otherwise false . So if the two variables have the same values but they are of different type, then not equal operator will return True.
Python Arithmetic Operators
| Operator | Description |
|---|
| - Subtraction | Subtracts right hand operand from left hand operand. |
| * Multiplication | Multiplies values on either side of the operator |
| / Division | Divides left hand operand by right hand operand |
| % Modulus | Divides left hand operand by right hand operand and returns remainder |
Logo has a number of other drawing commands, some of which are given below.
- pu − penup.
- pd − pendown.
- ht − hideturtle.
- dt − showturtle.
- setpensize.
When Logo looks at a command that uses arithmetic, it does the arithmetic in the standard mathematical order: multiplication and division followed by addition and subtraction.
Logo - Arithmetic Operations. Logo provides the usual arithmetic operations of addition, subtraction, multiplication and division, denoted by the symbols +, -, *, /. Each of these operations produces a result. If you don't do something with the result, such as print it, Logo will show an error.
The <PRINT> command in LOGO is used to print a text on the screen. The short form of PRINT is PR. Message to be printed can consist of one or more words or even a sentence. If you want to print a single word, the primitive name should be followed by an opening quotation mark before the word to be printed.
LOGO stands for language for graphics oriented. LOGO is a programming language specifically designed for children. It has graphic capabilities. It gives instruction to the computer to create graphics. It is also used to create geometric shapes on the computer.
Basic Maths Symbols Names With Meaning and Examples
| Symbol | Symbol Name | Meaning or Definition |
|---|
| − | minus sign | subtraction |
| + | plus sign | addition |
| ∓ | minus – plus | both minus and plus operations |
| ± | plus – minus | both plus and minus operations |