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++
#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
class GfG {
public static void main(String[] args)
{
int i, n = 20 ;
for (i = 0 ; i < n; n--)
System.out.print( "*" );
}
}
|
Python3
if __name__ = = '__main__' :
n = 20
for i in range ( 0 , n):
print ( "*" ,end = '')
n - = 1
|
C#
using System;
class GfG
{
public static void Main()
{
int i, n = 20;
for (i = 0; i < n; n--)
Console.Write( "*" );
}
}
|
Javascript
<script>
var i, n = 20;
for (i = 0; i < n; n--)
document.write( "*" );
</script>
|
Output
********************
2. Put ‘-‘ before i in for loop’s second expression
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i, n = 20;
for (i = 0; -i < n; i--)
cout<< "*" ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; -i < n; i--)
printf ( "*" );
getchar ();
return 0;
}
|
Java
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( "*" );
}
}
|
Python3
if __name__ = = '__main__' :
n = 20
for i in range ( 0 ,n):
print ( "*" , end = "")
|
C#
using System;
class GfG
{
public static void Main()
{
int i, n = 20;
for (i = 0; -i < n; i--)
Console.Write( "*" );
}
}
|
Javascript
<script>
let i, n = 20;
for (i = 0; -i < n; i--)
document.write( "*" );
</script>
|
Output
********************
3. Replace < by + in for loop’s second expression
C++
#include <iostream>
using namespace std;
int main()
{
int i, n = 20;
for (i = 0; i + n; i--)
cout << "*" ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; i + n; i--)
printf ( "*" );
getchar ();
return 0;
}
|
Java
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( "*" );
}
}
|
Python3
i,n = 0 , 20
while (i + n):
print ( "*" ,end = "")
i - = 1
|
C#
using System;
class GFG
{
static void Main( string [] args)
{
int i, n = 20;
for (i = 0; i + n > 0; i--)
Console.Write( "*" );
}
}
|
Javascript
<script>
let i, n = 20;
for (i = 0; i + n; i--)
document.write( "*" );
</script>
|
Output
********************
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++
#include <iostream>
using namespace std;
int main()
{
int i, n = 20;
for (i = 0; ~i < n; i--)
printf ( "*" );
getchar ();
return 0;
}
|
C
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; ~i < n; i--)
printf ( "*" );
getchar ();
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
int i, n = 20 ;
for (i = 0 ; ~i < n; i--)
System.out.print( "*" );
}
}
|
Python3
n = 20
i = 0
while (~i<n):
print ( "*" ,end = "")
i - = 1
|
C#
using System;
class GFG{
public static void Main(String[] args)
{
int i, n = 20;
for (i = 0; ~i < n; i--)
Console.Write( "*" );
}
}
|
Javascript
<script>
{
var i, n = 20;
for (i = 0; ~i < n; i--)
document.write( "*" );
}
</script>
|
Output
*********************
Since only a single loop is used to print the n stars, the time complexity of this approach is O(1).
No extra space is used in this approach and hence the space complexity is O(1).
Please comment if you find more solutions to the above problems.
Contact Us