Terms of AP
Write a program to print first x terms of the series 3N + 2 which are not multiples of 4.
Sample Input 1 :
Sample Output 1 :
10
5 11 14 17 23 26 29 35 38 41
import java.util.*; public class TermsOfAP { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int sum=0; int p=n; for (int i=1; i<=p; i++) { sum=3*i+2; if (sum % 4 != 0) { System.out.print(sum+ " "); } else { //Increasing p in case sum is divisible by 4 so that we get n terms p++; } } } }
Comments
Post a Comment