Challenge to programmers.
Write a program in Java or any other language to display the following series.
2 4 1 3 6 8 5 7 10 12 9 11.....N terms.
Restrictions:-
1. You can use only one print statement inside for loop.
2. Nested loops are not allowed.
3. There can be a maximum of five variables.
Warning:-
Don't give irrelevant/wrong ❌ answers otherwise it will be deleted.
Answers
Answered by
8
2 4 1 3 6 8 5 7 10 12 9 11.....N terms.
-1+3=2
2+2=4
4-3=1
1+2=3
3+3=6
6+2=8
8-3=5
5+2=7
7+3=10
.............As Repetition of .....n+3,n+2,n-3,n+2
___________________________
CODE➾
import java. util.*;
class Series
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int j=1;
for(int k=-1;n>0;n--)
{
if(j==1)
{
k+=3;
j++;
}
else if(j==2)
{
k+=2;
j++;
}
else if(j==3)
{
k-=3;
j++;
}
else if(j==4)
{
k+=2j=1;
}
System.out.print(k+"\t");}//end of for loop
}//end of main()}// end of class
______________________________
(Variable used=3)
input:-6
Output:-2 4 1 3 6 8
______________________________
- 1. You can use only one print statement inside for loop. checked
- Nested loops are not allowed. checked
- There can be a maximum of five variables. checked
____________________________
Important Point:-
- I used Scanner class here to insure that we ask user for value of n through keyboard.
- Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings through keyboard. It is the easiest way to read input in a Java program.
- Always divide double data type variable with integer or integer data type from double data type variable.To ensure 100% result.
Similar questions