// GO through the program and discuss What is the difference between "for" and "while-do" loops? //What is the difference between "while-do" and "do-while" loops?
/**
* @author Sumit Srivastava
* class calculate sum and average of number which follow some pattern
* like number divisible by 2 or 7 in this case
*/
public class SumDigitsPattern {
int sum;
int count;
double average;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int oddSum(int lowerbound , int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
if(number % 2 !=0){
sum += number;
count++;
}
}
setCount(count);
return sum;
}
public double oddAverage(int lowerbound , int upperbound){
sum = oddSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
public int multipleSevenSum(int lowerbound, int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
if(number % 7 ==0){
sum += number;
count++;
}
}
setCount(count);
return sum;
}
public double multipleSevenAverage(int lowerbound, int upperbound){
sum = multipleSevenSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
public int sumOfSquares(int lowerbound, int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
sum += number*number;
count++;
}
setCount(count);
return sum;
}
public double averageOfSquares(int lowerbound, int upperbound){
sum = sumOfSquares(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
}
/**
* @author Sumit Srivastava
* class calculate sum and average of number which follow some pattern
* like number divisible by 2 or 7 in this case
*/
public class SumDigitsPattern {
int sum;
int count;
double average;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int oddSum(int lowerbound , int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
if(number % 2 !=0){
sum += number;
count++;
}
}
setCount(count);
return sum;
}
public double oddAverage(int lowerbound , int upperbound){
sum = oddSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
public int multipleSevenSum(int lowerbound, int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
if(number % 7 ==0){
sum += number;
count++;
}
}
setCount(count);
return sum;
}
public double multipleSevenAverage(int lowerbound, int upperbound){
sum = multipleSevenSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
public int sumOfSquares(int lowerbound, int upperbound){
sum=0;
count=0;
for(int number = lowerbound ; number <=upperbound ; number++){
sum += number*number;
count++;
}
setCount(count);
return sum;
}
public double averageOfSquares(int lowerbound, int upperbound){
sum = sumOfSquares(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
}
/**
* @author Sumit Srivastava
* class calculate sum and average of numbers
*/
public class SumAndAverage {
public int sum; //store the accumulated sum
public double average; // store the average
int count; // store how many times loop iterates
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getSumUsingFor(int lowerbound , int upperbound){
for(int number = lowerbound ; number <=upperbound ; number++)
sum += number;
return sum;
}
public int getSumUsingWhile(int lowerbound , int upperbound){
int number ;
number = lowerbound;
sum =0;
while(number <= upperbound){
sum += number;
number++;
}
return sum;
}
public int getSumUsingDoWhile(int lowerbound , int upperbound){
int number ;
number = lowerbound;
sum=0;
do{
sum += number;
number++;
}while(number <= upperbound);
return sum;
}
public double getAverage(int lowerbound , int upperbound){
int totalDigits;
sum=0;
sum =getSumUsingFor(lowerbound, upperbound);
totalDigits =1 +( upperbound - lowerbound );
average =(double) sum / totalDigits;
return average;
}
public int modifiedSum(int lowerbound , int upperbound){
sum=0;
count=0;
lowerbound =111; // Overriding values of lowerbound
upperbound =8899; // Overriding values of upperbound
for(int number = lowerbound ; number <=upperbound ; number++){
sum += number;
count++;
}
setCount(count);
return sum;
}
public double modifiedAverage(int lowerbound , int upperbound){
lowerbound =111;
upperbound =8899;
sum = modifiedSum(lowerbound, upperbound);
average =(double) sum / getCount();
return average;
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
int lowerbound;
int upperbound;
lowerbound =1;
upperbound=100;
SumAndAverage sumAndAverage = new SumAndAverage();
//Printing sum using for loop
System.out.println("The sum using for loop is : "+sumAndAverage.getSumUsingFor(lowerbound, upperbound));
//Printing sum using while-do loop
System.out.println("The sum using while loop is : "+sumAndAverage.getSumUsingWhile(lowerbound, upperbound));
//Printing sum using do-while loop
System.out.println("The sum using do-while loop is : "+sumAndAverage.getSumUsingDoWhile(lowerbound, upperbound));
//Printing average
System.out.println("The average is : "+sumAndAverage.getAverage(lowerbound, upperbound));
//Printing modified Sum
System.out.println("Modified sum is : "+sumAndAverage.modifiedSum(lowerbound, upperbound));
//Printing modified Average
System.out.println("modified Average is : "+sumAndAverage.modifiedAverage(lowerbound, upperbound));
//printing how many times loop iterates to get the sum i.e value of count
System.out.println("Modified loop iterates "+sumAndAverage.getCount() +" times");
SumDigitsPattern sumDigitsPattern = new SumDigitsPattern();
//Printing sum of odd numbers
System.out.println("The sum of odd number is : "+sumDigitsPattern.oddSum(lowerbound, upperbound));
//Printing Average of odd Numbers
System.out.println("Average of odd number is : "+sumDigitsPattern.oddAverage(lowerbound, upperbound));
//Printing sum of multiple of seven
System.out.println("The sum of multiple of seven is : "+sumDigitsPattern.multipleSevenSum(lowerbound, upperbound));
//Printing average of Multiple of seven
System.out.println("The average of multiple of seven is : "+sumDigitsPattern.multipleSevenAverage(lowerbound, upperbound));
//Printing sum of squares of numbers
System.out.println("The sum of squares of number is : "+sumDigitsPattern.sumOfSquares(lowerbound, upperbound));
//Printing average of squares of numbers
System.out.println("The average of squares of number is : "+sumDigitsPattern.averageOfSquares(lowerbound, upperbound));
}
}