Print the following series using scanner class in java :-
S= 1-3+5-7+9-11.......................n
Answers
Answered by
2
mport java.util.Scanner;
public class OddSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int i = 1, c, f = 1;
for (c = 1; c <= n; c++) {
if (f % 2 == 0) {
System.out.print(-i + " ");
} else {
System.out.print(i + " ");
}
i += 2;
f++;
}
}
}
public class OddSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int i = 1, c, f = 1;
for (c = 1; c <= n; c++) {
if (f % 2 == 0) {
System.out.print(-i + " ");
} else {
System.out.print(i + " ");
}
i += 2;
f++;
}
}
}
akshisaha06:
Thanks a Lot !!!
Similar questions
Social Sciences,
8 months ago
Biology,
8 months ago
English,
1 year ago
Physics,
1 year ago
India Languages,
1 year ago