Answer:
a.The sum of all even numbers between 2 and 100 (inclusive).
public class SumofEvenNumbers
{ public static void main(String[] args) {
int sum=0; // stores the sum of even numbers
/* loop starts from 2 and terminates when value of i becomes greater than 100 At each iteration the value of i is incremented by 2 and added to the sum variable to add the even numbers */
for(int i=2; i<=100; i+=2){
sum = sum + i; }
System.out.println("Sum of all even numbers between 2 and 100 (inclusive) is " + sum);} }
//displays the sum of even nos from 2 to 100
Output:
Sum of all even numbers between 2 and 100 (inclusive) is 2550
b. The sum of all squares between 1 and 100 (inclusive).
Sum of square from 1 to 100 is 1+4+9+16+25+36+49+64+81+100
public class SumofSquares
{ public static void main(String[] args) {
int square;
int sum = 0;
for(int i = 1; i<=10; i++) {
square= (i*i);
sum += square; }
System.out.println("The sum of all squares between 1 and 100 (inclusive) is " + sum);}}
Output:
The sum of all squares between 1 and 100 (inclusive) is 385
c. All powers of 2 from 2^0 up to 2^20.
The program if you want to display all the the powers of 2 from 2^0 to 2^20:
import java.lang.Math;
public class Powers
{ public static void main(String[] args) {
double power = 0; // stores the powers
for(int i = 0; i <= 20; i++) // loop starts from i=0 and terminates at i>20
{power = Math.pow(2, i); // computes power of 2 from 2^0 to 2^20
System.out.println("2 ^ " + i + " = " + (int) power);}
//displays all powers of 2 by converting each power of 2 to integer
} }
Output:
2 ^ 0 = 1
2 ^ 1 = 2
2 ^ 2 = 4
2 ^ 3 = 8
2 ^ 4 = 16
2 ^ 5 = 32
2 ^ 6 = 64
2 ^ 7 = 128
2 ^ 8 = 256
2 ^ 9 = 512
2 ^ 10 = 1024
2 ^ 11 = 2048
2 ^ 12 = 4096
2 ^ 13 = 8192
2 ^ 14 = 16384
2 ^ 15 = 32768
2 ^ 16 = 65536
2 ^ 17 = 131072
2 ^ 18 = 262144
2 ^ 19 = 524288
2 ^ 20 = 1048576
The following program is to find the sum of all powers of 2 from 2^0 to 2^20
import java.lang.Math;
public class Powers
{ public static void main(String[] args) {
double power = 0;
double sum=0;
for(int i = 0; i <= 20; i++){
power = Math.pow(2, i);// calculates all powers of 2 from 2^0 to 2^20
sum = sum + power;} // calculates sum of all powers from 2^0 to 2^20
System.out.println("sum of all powers of 2 from 2^0 to 2^20 is " + sum);}}
Output:
sum of all powers of 2 from 2^0 to 2^20 is 2097151.0
d. The sum of all odd numbers between a and b (inclusive), where a and b are inputs.
import java.util.Scanner; //to take input from user
public class SumOddNumbers
{public static void main(String[] args) { // main() function body
Scanner input = new Scanner(System.in); //create object of Scanner class
System.out.print("Enter two numbers: ");
//prompts user to enter two numbers
int a = input.nextInt(); // scans and reads first number input by user
int b = input.nextInt();//scans and reads 2nd number entered by user
int sum = 0; // stores the sum of odd numbers
/* loop starts from i = a which is the first input number and continues till the value of i gets greater than b which is the 2nd input number */
for (int i = a; i <= b; i++) {
if (i % 2 == 1) //checks if the number in the range a to b is odd
sum += i; } // adds the odd numbers
System.out.println("The sum of all odd numbers between " + a + " and "+ b + " (inclusive) is " + sum); } }
// displays sum of odd numbers in the given range
Output:
The sum of all odd numbers between 1 and 10 (inclusive) is 25
e. The sum of all odd digits of an input.
import java.util.Scanner; // to take input from user
public class SumOfOddDigits
{ public static void main(String[] args) { // main() function body
Scanner input = new Scanner(System.in); // create object of class Scanner
//prompts user to enter a number
System.out.println("Enter a number: ");
//scans and reads numbers from user
int number = input.nextInt();
int sum=0; //stores the sum of odd digits of input number
while(number>0){
// loop executes till value of number variable is greater than 0
int remainder = number % 10;
/*takes mode of input number to calculate the remainder of the number, this means each digit of the number is separated. For example if the number is 32677 then after taking mod we get 7 in first iteration */
if(remainder % 2!=0){
/*checks if the value in remainder is odd which means the digit is not completely divisible by 2 and the mod of the digit with 2 is not equal to 0 */
sum = sum + remainder; } // adds the odd digits of input number
number = number / 10; }
// divides the number by 10 for example 32677/10 is 3267 in first iteration
System.out.println("sum of all odd digits is " + sum); } }
// displays sum of the odd digits of input number
Output:
Enter a number:
32677
sum of all odd digits is 17