Challenge to programmers.
Write a program in java to display the series.
2 4 1 3 6 8 5 7 10 12 9 11.....N terms.
Restrictions:-
1. You cannot use more than one loop.
2. You can use only one print statement inside for loop.
3. There can be a Maximum of five variables.
4. You can use only one if statement inside the loop(not if-else or if-else-elseif)
This is similar to my previous question but I have added more restrictions in this question.
All the best.
Warning ⚠⚠
Don't give irrelevant /wrong ❌answers or it will be deleted
Legends don't do different things, they do things differently.
Answers
Write a program in java to display the series.
2 4 1 3 6 8 5 7 10 12 9 11.....N terms.
_____________________________________
m. k. => Output.
1. 2 => 2
2. 2+2 => 4
3. 4-3 => 1
4. 1+2 => 3
5. 3+3 => 6
6. 6+2 => 8
7. 8-3 => 5
8. 5+2 => 7
9. 7+3 => 10
10. 10+2 => 12
As Repetition of .....
n+2,n-3,n+2,n+3.............
___________________________________
1.class Series
2.{
3.public static void main(int n)
4.{
5.int m=1;
6.for(int k=2;n>0;n--,m++)
7.{
8.System.out.print(k+"\t");
9.if( m % 2 != 0 )
10.{
11.k+=2;
12.continue;
13.}
14.k-=(k%2==0)?(3):(-3);
15.} //End of for loop
16.}//End of main()
17.}//End of class
_________________________________
Input:-
10
Output:-
2 4 1 3 6 8 5 7 10 12
________________________________
Method 2nd:-
class Series
{
public static void main(int n)
{
int m=1;
for(int k=2;n>0;n--)
{
System.out.print(k+"\t");
switch (m)
{
case 1:
k=k+2;
break;
case 2:
k=k-3;
break;
case 3:
k=k+2;
break;
case 4:
k=k+3;
m=0
break;
}//end of switch statement
m++;
}//end of for loop
}//end of main()
}//end of class
________________________________
1. You cannot use more than one loop.
(Use only once in giving code )
Done✔️
2. You can use only one print statement inside for loop.
(Used once only to print value of k)
Done✔️
3. There can be a Maximum of five variables.
(Used only 3 variables.... namely n,m,k)
Done✔️
4. You can use only one if statement inside the loop(not if-else or if-else-elseif)
(Used only once to check the condition m%2!=0)
Done✔️
- GIVEN:−
Write a program in java to display the series.
2 4 1 3 6 8 5 7 10 12 9 11.....N terms.
_____________________________________
- LOGIC:−
m. k. => Output.
1. 2 => 2
2. 2+2 => 4
3. 4-3 => 1
4. 1+2 => 3
5. 3+3 => 6
6. 6+2 => 8
7. 8-3 => 5
8. 5+2 => 7
9. 7+3 => 10
10. 10+2 => 12
As Repetition of .....
n+2,n-3,n+2,n+3.............
___________________________________
- CODE:−
1.class Series
2.{
3.public static void main(int n)
4.{
5.int m=1;
6.for(int k=2;n>0;n--,m++)
7.{
8.System.out.print(k+"\t");
9.if( m % 2 != 0 )
10.{
11.k+=2;
12.continue;
13.}
14.k-=(k%2==0)?(3):(-3);
15.} //End of for loop
16.}//End of main()
17.}//End of class
_________________________________
Input:-
10
Output:-
2 4 1 3 6 8 5 7 10 12
________________________________
Method 2nd:-
class Series
public static void main(int n)
int m=1;
for(int k=2;n>0;n--)
System.out.print(k+"\t");
switch (m)
case 1:
k=k+2;
break;
case 2:
k=k-3;
break;
case 3:
k=k+2;
break;
case 4:
k=k+3;
m=0
break;
end of switch statement
m++;
end of for loop
end of main()
end of class
________________________________
- RESTRICTIONS:−
1. You cannot use more than one loop.
(Use only once in giving code )
Done✔️
2. You can use only one print statement inside for loop.
(Used once only to print value of k)
Done✔️
3. There can be a Maximum of five variables.
(Used only 3 variables.... namely n,m,k)
Done✔️
4. You can use only one if statement inside the loop(not if-else or if-else-elseif)
(Used only once to check the condition m%2!=0)
Done✔️