Increment a number without using ++ or +
Last Updated :
17 Apr, 2023
The task is to Increment a number without using ++ and + operators.
Examples:
Input : 3
Output : 4
Input : 9
Output : 10
The idea is based on the fact that the negative numbers are stored using 2’s complement form. 2’s complement form is obtained by inverting bits and then adding one. So if we invert all bits of given number and apply negative sign, we get the number plus 1.
C++
#include <bits/stdc++.h>
using namespace std;
int increment(unsigned int i)
{
i = -(~i);
return i;
}
int main()
{
int n = 3;
cout << increment(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long increment( long i)
{
i = -(~i);
return i;
}
public static void main (String[] args)
{
long n = 3 ;
System.out.print( increment(n));
}
}
|
Python3
def increment(i):
i = - (~i);
return i;
if __name__ = = "__main__" :
n = 3 ;
print (increment(n));
|
C#
using System;
class GFG
{
static long increment( long i)
{
i = -(~i);
return i;
}
public static void Main ()
{
long n = 3;
Console.WriteLine(increment(n));
}
}
|
PHP
<?php
function increment( $i )
{
$i = -(~ $i );
return $i ;
}
$n = 3;
echo increment( $n );
?>
|
Javascript
<script>
function increment(i) {
i = -(~i);
return i;
}
let n = 3;
document.write(increment(n));
</script>
|
It also works for characters.
Example:
Input : a
Output : b
Input : o
Output : p
Time Complexity: O(1)
Auxiliary Space: O(1)
C++
#include <bits/stdc++.h>
using namespace std;
char increment(unsigned char i)
{
i = -(~i);
return i;
}
int main()
{
char n = 'a' ;
cout << increment(n);
return 0;
}
|
Java
class GFG
{
static char increment( char i)
{
int i1 = -(~( int )(i));
return ( char )(i1);
}
public static void main(String[] args)
{
char n = 'a' ;
System.out.println(increment(n));
}
}
|
Python3
def increment(i):
i = - (~ ord (i));
return chr (i);
n = 'a' ;
print (increment(n));
|
C#
class GFG
{
static char increment( char i)
{
int i1 = -(~( int )(i));
return ( char )(i1);
}
static void Main()
{
char n = 'a' ;
System.Console.WriteLine(increment(n));
}
}
|
PHP
<?php
function increment( $i )
{
$i = -(~ord( $i ));
return chr ( $i );
}
$n = 'a' ;
echo increment( $n );
?>
|
Javascript
<script>
function increment(i)
{
let i1 = -(~(i).charCodeAt());
return String.fromCharCode(i1);
}
let n = 'a' ;
document.write(increment(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar to the approach of using complement, we can use double negation to increment a number, as a -(-1) is equivalent to a + 1.
C++
#include <bits/stdc++.h>
using namespace std;
int increment(unsigned int i)
{
i = i - (-1);
return i;
}
int main()
{
int n = 3;
cout << increment(n);
return 0;
}
|
Python3
def increment(i):
i = i - ( - 1 );
return i;
n = 3 ;
print (increment(n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int increment( int i)
{
i = i - (-1);
return i;
}
public static void Main( string [] args)
{
int n = 3;
Console.WriteLine(increment(n));
}
}
|
Javascript
function increment(i)
{
i = i - (-1);
return i;
}
let n = 3;
console.log(increment(n));
|
Java
import java.util.*;
class GFG
{
static int increment( int i)
{
i = i - (- 1 );
return i;
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(increment(n));
}
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...