En el siguiente código, cambie/agregue solo un carácter e imprima ‘*’ exactamente 20 veces.
int main() { int i, n = 20; for (i = 0; i < n; i--) printf("*"); getchar(); return 0; }
Soluciones:
1. Reemplace i por n en la tercera expresión del bucle for
C++
#include <iostream> using namespace std; int main() { int i, n = 20; for (i = 0; i < n; n--) cout << "*"; getchar(); return 0; }
C
#include <stdio.h> int main() { int i, n = 20; for (i = 0; i < n; n--) printf("*"); getchar(); return 0; }
Java
// Java code class GfG { public static void main(String[] args) { int i, n = 20; for (i = 0; i < n; n--) System.out.print("*"); } }
Python3
# Python3 program to implement # the above approach if __name__ == '__main__': n = 20 for i in range(0, n): print("*",end='') n -= 1 # This code is contributed by gauravrajput1
C#
// C# code using System; class GfG { public static void Main() { int i, n = 20; for (i = 0; i < n; n--) Console.Write("*"); } } // This code is contributed by SoumikMondal
Javascript
<script> // Javascript code var i, n = 20; for(i = 0; i < n; n--) document.write("*"); // This code is contributed by Ankita Saini </script>
********************
2. Ponga ‘-‘ antes de i en la segunda expresión del bucle for
C++
#include<bits/stdc++.h> using namespace std; int main() { int i, n = 20; for (i = 0; -i < n; i--) cout<<"*"; return 0; } // This code is contributed by rutvik_56.
C
#include <stdio.h> int main() { int i, n = 20; for (i = 0; -i < n; i--) printf("*"); getchar(); return 0; }
Java
// Java code import java.util.*; public class GFG { public static void main(String[] args) { int i, n = 20; for (i = 0; -i < n; i--) System.out.print("*"); } } // This code is contributed by divyesh072019.
Python3
# Python3 program to implement # the above approach if __name__ == '__main__': n = 20 for i in range(0,n): print("*", end="") # This code is contributed by shivanisinghss2110
C#
// C# code using System; class GfG { public static void Main() { int i, n = 20; for (i = 0; -i < n; i--) Console.Write("*"); } } // This code is contributed by divyeshrabadiya07.
Javascript
<script> let i, n = 20; for (i = 0; -i < n; i--) document.write("*"); // This code is contributed by patel2127 </script>
********************
3. Reemplace < por + en la segunda expresión del bucle for
C++
#include <iostream> using namespace std; int main() { int i, n = 20; for(i = 0; i + n; i--) cout << "*"; return 0; } // This code is contributed by shivani
C
#include <stdio.h> int main() { int i, n = 20; for (i = 0; i + n; i--) printf("*"); getchar(); return 0; }
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main (String[] args) { int i, n = 20; for(i = 0; i + n > 0; i--) System.out.print("*"); } } // This code is contributed by shinjanpatra.
Python3
i,n = 0,20 while(i+n): print("*",end="") i -= 1 # This code is contributed by shinjanpatra
Javascript
<script> let i, n = 20; for(i = 0; i + n; i--) document.write("*"); // This code is contributed by unknown2108 </script>
********************
Ampliemos un poco el problema.
Cambie/agregue solo un carácter e imprima ‘*’ exactamente 21 veces.
Solución: Coloque el operador de negación antes de i en la segunda expresión del bucle for.
Explicación: El operador de negación convierte el número en su complemento a uno.
No. One's complement 0 (00000..00) -1 (1111..11) -1 (11..1111) 0 (00..0000) -2 (11..1110) 1 (00..0001) -3 (11..1101) 2 (00..0010) ............................................... -20 (11..01100) 19 (00..10011)
C++
// C++ program for the above approach #include <iostream> using namespace std; int main() { int i, n = 20; for (i = 0; ~i < n; i--) printf("*"); getchar(); return 0; } // This code is contributed by shivanisinghss2110
C
#include <stdio.h> int main() { int i, n = 20; for (i = 0; ~i < n; i--) printf("*"); getchar(); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ public static void main(String[] args) { int i, n = 20; for(i = 0; ~i < n; i--) System.out.print( "*" ); } } // This code is contributed by shivani
Python3
# JavaScript program for the above approach n = 20 i = 0 while(~i<n): print("*",end="") i -= 1 # This code is contributed by Shinjanpatra
C#
// C# program for the above approach using System; class GFG{ public static void Main(String[] args) { int i, n = 20; for(i = 0; ~i < n; i--) Console.Write( "*" ); } } // This code is contributed by shivanisinghss2110
Javascript
<script> // JavaScript program for the above approach { var i, n = 20; for(i = 0; ~i < n; i--) document.write("*"); } // This code is contributed by shivanisinghss2110 </script>
*********************
Comente si encuentra más soluciones a los problemas anteriores.
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