Write a program to display the program upto 10 terms:
1,6,11.........................
Answers
Answer:Input: N = 4
Output: 2, 1, 4, 3
Explanation:
Nth Term = (N % 2 == 0) ? (N - 1) : (N + 1)
1st term = (1 % 2 == 0) ? (1 - 1) : (1 + 1)
= (1 + 1)
= 2
2nd term = (2 % 2 == 0) ? (2 - 1) : (2 + 1)
= (2 - 1)
= 1
3rd term = (3 % 2 == 0) ? (3 - 1) : (3 + 1)
= (3 + 1)
= 4
4th term = (4 % 2 == 0) ? (4 - 1) : (4 + 1)
= (4 - 1)
= 3
Therefore, Series = 2, 1, 4, 3
Input: N = 7
Output: 2, 1, 4, 3, 6, 5, 8
Explanation:
/*Since here is not such case of taking input so we should avoid the Scanner class but scanner class is mandatory while there is any such case of taking input. And moreover it is better that how much shortly we can complete the programming. And there is another aspect of less using of the space of memory. */
/*Before doing the program it is necessary to know about the sequence of the following for better understanding. Follow the series, we can find here the multiplication table of 5 but the multiples are subtracted by 4. Such as,
5*1-4=5-4=1
5*2-4=10-4=6
5*3-4=15-11=16 and so on*/
//now come to the program
//using the utilizing process
import java.util.*;
public class Series
{
public static void main(String args[])
{
//Scanner class is not necessary so directly declairing the variables
int a=5,i;
//using for loop to terminate the program
for(i=1;i<=10;i++)
{
//the mathematical expression
a=a*i-4;
/*displaying the output and for visualising the output the paranthesis 'system.out.println' is mandatory otherwise syntax error will occur and the program will not be compiled*/
System.out.println("The series is"+a);
}
}
}