Escriba un programa para encontrar la suma de enteros positivos sin usar ningún operador. Solo se permite el uso de printf(). No se puede utilizar ninguna otra función de biblioteca.
Solución
Es una pregunta capciosa. Podemos usar printf() para encontrar la suma de dos números ya que printf() devuelve el número de caracteres impresos. El ancho del campo en printf()se puede utilizar para encontrar la suma de dos números. Podemos usar ‘*’ que indica el ancho mínimo de salida. Por ejemplo, en la sentencia “printf(“%*d”, ancho, num);”, el ‘ancho’ especificado se sustituye en lugar de *, y ‘num’ se imprime dentro del ancho mínimo especificado. Si el número de dígitos en ‘num’ es menor que el ‘ancho’ especificado, la salida se rellena con espacios en blanco. Si el número de dígitos es mayor, la salida se imprime tal cual (no truncada). En el siguiente programa, add() devuelve la suma de x e y. Imprime 2 espacios dentro del ancho especificado usando x e y. Entonces, el total de caracteres impresos es igual a la suma de x e y. Es por eso que add() devuelve x+y.
C++
#include <iostream> using namespace std; int add(int x, int y) { return printf("%*c%*c", x, ' ', y, ' '); } // Driver code int main() { printf("Sum = %d", add(3, 4)); return 0; } // This code is contributed by shubhamsingh10
C
#include <stdio.h> int add(int x, int y) { return printf("%*c%*c", x, ' ', y, ' '); } // Driver code int main() { printf("Sum = %d", add(3, 4)); return 0; }
Python3
# Python code for the above approach def add(x, y) : return (x + y); # Driver code if __name__ == "__main__": print("Sum = ", add(3, 4)) # This code is contributed by splvel62.
C++
#include <iostream> using namespace std; int add(int x, int y) { return printf("%*c%*c", x, '\r', y, '\r'); } // Driver code int main() { printf("Sum = %d", add(3, 4)); return 0; } // This code is contributed by shubhamsingh10
C
#include <stdio.h> int add(int x, int y) { return printf("%*c%*c", x, '\r', y, '\r'); } // Driver code int main() { printf("Sum = %d", add(3, 4)); return 0; }
Java
class GFG { static int add(int x, int y) { return (x + y); } // Driver code public static void main(String[] args) { System.out.printf("Sum = %d", add(3, 4)); } } // This code is contributed by Rajput-Ji
Python3
# Python program for the above approach def add(x, y) : return (x + y); # driver code print("Sum = ", add(3, 4)); # This code is contributed by sanjoy_62
C#
// C# program for the above approach using System; public class GFG { static int add(int x, int y) { return (x + y); } // Driver Code public static void Main(String[] args) { Console.WriteLine("Sum = " + add(3, 4)); } } // This code is contributed by code_hunt.
Javascript
<script> // JavaScript code for the above approach function add(x, y) { return (x + y); } // Driver Code document.write("Sum = " + add(3, 4)); // This code is contributed by avijitmondal1998. </script>
C++
#include <iostream> using namespace std; int main() { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } cout << "Sum = " << a; return 0; } // This code is contributed by SHUBHAMSINGH10 // This code is improved & fixed by Abhijeet Soni.
C
#include <stdio.h> int main() { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } printf("Sum = %d", a); return 0; } // This code is contributed by Abhijeet Soni
Java
// Java code class GfG { public static void main(String[] args) { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } System.out.println("Sum is: " + a); } } // This code is contributed by Abhijeet Soni
Python3
# Python 3 Code if __name__ == '__main__': a = 10 b = 5 if b > 0: while b > 0: a = a + 1 b = b - 1 if b < 0: while b < 0: a = a - 1 b = b + 1 print("Sum is: ", a) # This code is contributed by Akanksha Rai # This code is improved & fixed by Abhijeet Soni
C#
// C# code using System; class GFG { static public void Main() { int a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } Console.Write("Sum is: " + a); } } // This code is contributed by Tushil // This code is improved & fixed by Abhijeet Soni.
PHP
<?php // PHP Code $a = 10; $b = 5; if ($b > 0) { while($b > 0) { $a++; $b--; } } if ($b < 0) { while($b < 0) { $a--; $b++; } } echo "Sum is: ", $a; // This code is contributed by Dinesh // This code is improved & fixed by Abhijeet Soni. ?>
Javascript
<script> // Javascript program for the above approach // Driver Code let a = 10, b = 5; if (b > 0) { while (b > 0) { a++; b--; } } if (b < 0) { // when 'b' is negative while (b < 0) { a--; b++; } } document.write("Sum = " + a); </script>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA