AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939091
up-rate
4
down-rate

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
profile Rishi Kumar - anyforum.in Rishi Kumar
523188249150
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in