Change/add only one character and print '*' exactly 20 times - GeeksforGeeks

20 Apr, 2022
In the below code, change/add only one character and print ‘*’ exactly 20 times. 
int main()
{
    int i, n = 20;
    for (i = 0; i < n; i--)
        printf("*");             
    getchar();
    return 0;
}
Solutions:1. Replace i by n in for loop’s third expression  
C++
C
Java
Python3
C#
Javascript
#include <iostream>using namespace std;int main(){    int i, n = 20;    for (i = 0; i < n; n--)        cout << "*";    getchar();    return 0;}
#include <stdio.h>int main(){    int i, n = 20;    for (i = 0; i < n; n--)        printf("*");    getchar();       return 0;}
// Java codeclass GfG {public static void main(String[] args){    int i, n = 20;    for (i = 0; i < n; n--)        System.out.print("*");}}
# Python3 program to implement # the above approachif __name__ == '__main__':  n = 20;  for i in range(0, n):    print("*"); n -= 1; # This code is contributed by gauravrajput1
// C# codeusing 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
<script> // Javascript codevar i, n = 20;for(i = 0; i < n; n--)    document.write("*");     // This code is contributed by Ankita Saini </script>
2. Put ‘-‘ before i in for loop’s second expression 
C++
C
Java
Python3
C#
Javascript
#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.
#include <stdio.h>int main(){    int i, n = 20;    for (i = 0; -i < n; i--)        printf("*");              getchar();       return 0;}
// Java codeimport 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 program to implement # the above approachif __name__ == '__main__':  n = 20  for i in range(0,n):    print("*", end="") # This code is contributed by shivanisinghss2110
// C# codeusing 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.
<script> let i, n = 20;for (i = 0; -i < n; i--)      document.write("*"); // This code is contributed by patel2127</script>
3. Replace < by + in for loop’s second expression  
C++
C
Python3
Javascript
#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
#include <stdio.h>int main(){    int i, n = 20;    for (i = 0; i + n; i--)       printf("*");    getchar();    return 0;}
i,n = 0,20 while(i+n):   print("*",end="")   i -= 1 # This code is contributed by shinjanpatra
<script> let i, n = 20; for(i = 0; i + n; i--)        document.write("*"); // This code is contributed by unknown2108</script>
Let’s extend the problem little.Change/add only one character and print ‘*’ exactly 21 times.Solution: Put negation operator before i in for loop’s second expression.Explanation: Negation operator converts the number into its one’s complement.  
       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
Java
C#
Javascript
// 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
#include <stdio.h>int main(){    int i, n = 20;    for (i = 0; ~i < n; i--)        printf("*");    getchar();    return 0;}
// Java program for the above approachimport 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
// C# program for the above approachusing 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
<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>
Please comment if you find more solutions to the above problems.
prerna saini
SoumikMondal
SHUBHAMSINGH10
GauravRajput1
divyeshrabadiya07
divyesh072019
rutvik_56
shivanisinghss2110
ankita_saini
patel2127
unknown2108
khushboogoyal499
shinjanpatra
c-puzzle
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
rand() and srand() in C/C++
Substring in C++
Command line arguments in C/C++
fork() in C
Function Pointer in C
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects