AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-programming: How to calculate the sum of odd numbers in a series?
Write a program that calculates the sum of odd terms of a given series. The input to your program is three integers a, d and l. They stand for the first, difference and the last term respectively of the series. Your program should output an integer that corresponds to the sum of alternating terms starting from the first term.
Sample input
1 1 10
Sample output
25
corejava x 353
programming x 169
Posted On :
2016-06-07 22:31:27.0
Saksham Kumar
734
339
39091
4
Answers
import java.util.Scanner;
public class OddSum {
public static void main(String[] args) {
System.out.println("Please enter the starting no. Of the series");
Scanner input =new Scanner(System.in);
int a=input.nextInt();
System.out.println("Please enter the difference");
int d=input.nextInt();
System.out.println("Please enter the last element");
int l=input.nextInt();
int sum=0;
System.out.println("Given series is as follows");
for(int i=a;i<=l;i=i+d){
System.out.print(i+" ");
if(i%2!=0){
sum=sum+i;
}
}
System.out.println("\nTotal Sum of odd numbers is: "+sum);
}
}
Posted On :
2016-06-07 23:07:36
Satisfied :
2 Yes
3 No
Rishi Kumar
523
1882
49150
Reply This Thread
5
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in