Write a program in Java to print the following series with sum:
S=1+5+2+4+3+3+4+2+5+1=30
Answers
Answered by
2
Question:-
- To print the following series:-
S=1+5+2+4+3+3+4+2+5+1=30
__________
Code:-
package Coder;
public class Series {
public static void main(String[] args) {
System.out.print("Sum=");
int c=5,d=1,s=0;
for(int i=1;i<=10;i++)
{
if (i%2==0)
{
System.out.print("+"+c);
s+=c;
c--;
}
else
{
System.out.print("+"+d);
s+=d;
d++;
}
}
System.out.print("="+s);
}
}
______
Variable description:-
- i :- Loop variable
- s :- To store the sum of series
- d and c :- to find the series of odd and even terms
______
•Output attached
Attachments:
Similar questions