(e) 5+ 8 + 12 + 15+-------n terms.Write this program in java language.
Answers
Answered by
2
Answer:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int start = 5;
for(int i = 0; i <= n; i++) {
if(i%2 == 0) {
System.out.print(start + " ");
start = start + 3;
} else {
System.out.print(start + " ");
start = start + 4;
}
}
}
}
Explanation:
First you have to initialize start variable from 5, and then start the variable i from 0 and run the loop till n, and inside the loop you have check if the value of i is multiple of 2 or not. Which will also tell you that if the current value of i is even or odd. If it is an even number then add 3 and print or add 4 and print.
Happy Coding!
Similar questions
Computer Science,
5 months ago
Math,
5 months ago
Chemistry,
5 months ago
Math,
10 months ago
Computer Science,
10 months ago
CBSE BOARD X,
1 year ago
Science,
1 year ago