Problem Description This problem is inspired by the books Pr
Problem Description
This problem is inspired by the book\'s Problem R6.2, parts a, b, c and d on page 293.
You are going to write program that takes 3 numbers from the command line (i.e. the input is in String args[]) and then does some fun sums. For part a, add the sum of all the even numbers from 2 to 100. For part b, add all the squares for the numbers from 1 to 100. For part c, add all the numbers from a to b, which are the first two arguments in String args[]. Finally, for part d, add all the odd digits for a number given in args[2].
Getting Started
There is no code to copy for the assignment. You get to do it all! Don\'t forget to provide proper Javadoc documentation.
We are going to do this exercise by writing the object that solves the problem first (in a source file called TicketSeller.java) and then testing it using code we write into SumFun.java. Using the techniques shown on the web page titled How to Start Every Project in this Class to create a source file called SumFun.java. This is where your code will go.
Testing Your Code
Your SumFun.java should contain code with your solution for SumFun.
Your output should look something like this for the arguments 1 10 123456789:
Once you\'ve written your code run the code by single clicking on SumFun.java in the package explorer and selecting Run->Run from the menu or using the keyboard shortcut. Remember to update the run configuration so you can pass the word on the command line (See the MontyHallParadox assignment if you forget). Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
public class SumFun {
public static void main(String[] args) {
// part 1 : sum of all even numbers from 2 to 100
int sum = 0;
for(int i=2; i<=100; i=i+2)
sum += i;
System.out.println(\"Sum of even = \"+sum);
// part 2 : add all the squares for the numbers from 1 to 100
int squareSum = 0;
for(int i=1; i<=100; i++)
squareSum += (i*i);
System.out.println(\"Sum of squares = \"+squareSum);
// part 3: add all the numbers from a to b
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int rangeSum = 0;
for(int i=a; i<=b; i++)
rangeSum += i;
System.out.println(\"Sum of odd numbers from \"+a+\" to \"+b+ \" = \"+rangeSum);
// part 4: add all the odd digits for a number given in args[2]
int n = Integer.parseInt(args[2]);
int c = n;
int oddSum = 0;
while(c > 0){
int d = c%10;
if(d%2 == 1)
oddSum += d;
c = c/10;
}
System.out.println(\"Sum of odd digits in \"+n+\" = \"+oddSum);
}
}
/*
Sample run:
Sum of even = 2550
Sum of squares = 338350
Sum of odd numbers from 12 to 56 = 1530
Sum of odd digits in 123456789 = 25
*/


