Answer to Question #254528 in Java | JSP | JSF for ding

Question #254528

9. Write a program that uses while loops to perform the following steps:

a. Prompt the user to input two integers: firstNum and secondNum.

(firstNum must be less than secondNum.)

b. Output all the odd numbers between firstNum and secondNum

inclusive.

c. Output the sum of all the even numbers between firstNum and

secondNum inclusive.

d. Output all the numbers and their squares between 1 and 10.

e. Output the sum of the squares of all the odd numbers between

firstNum and secondNum inclusive.

f. Output all the uppercase letters.

10. Redo Exercise 9 using for loops.

11. Redo Exercise 9 using do...while loops.


1
Expert's answer
2021-10-21T14:24:18-0400

Question 9

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		//a. Prompt the user to input two integers: firstNum and secondNum.
		//(firstNum must be less than secondNum.)
		int firstNum;
		System.out.println("Enter first number: ");
		firstNum=in.nextInt();
		int secondNum;
		while(true){
		    System.out.println("Enter second number (must be greater than "+firstNum+"): ");
		    secondNum=in.nextInt();
		    if(secondNum>firstNum)
		        break;
		}
		//b. Output all the odd numbers between firstNum and secondNum inclusive.
		System.out.println("Odd numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		int i=firstNum;
		while(i<=secondNum){
		    if (i%2 !=0){
		        System.out.print(i+" ");
		    }
		    i++;
		}
		//c. Output the sum of all the even numbers between firstNum and secondNum inclusive
		System.out.println("\nEven numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		int j=firstNum;
		while(j<=secondNum){
		    if (j%2==0){
		        System.out.print(j+" ");
		    }
		    j++;
		}
		//d. Output all the numbers and their squares between 1 and 10.
		System.out.println("\nNumber\tSquare of the number");
		int n=1;
		while (n<=10){
		    System.out.println(n+"\t"+n*n);
		    n++;
		}
		//e. Output the sum of the squares of all the odd numbers between
		//firstNum and secondNum inclusive.
		System.out.println("Sum of the squares of all the odd numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		int m=firstNum;
		int sum=0;
		while(m<=secondNum){
		    if (m%2 !=0){
		        sum+=(m*m);
		    }
		    m++;
		}
		System.out.println(sum);
		//f. Output all the uppercase letters
        System.out.println("Uppercase Alphabets are: \n");
        char ch='A';
        while(ch<='Z'){
            System.out.print(ch+" ");
            ch++;
        }
	}
}

Question 10

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		//a. Prompt the user to input two integers: firstNum and secondNum.
		//(firstNum must be less than secondNum.)
		int firstNum;
		System.out.println("Enter first number: ");
		firstNum=in.nextInt();
		int secondNum;
		while(true){
		    System.out.println("Enter second number (must be greater than "+firstNum+"): ");
		    secondNum=in.nextInt();
		    if(secondNum>firstNum)
		        break;
		}
		//b. Output all the odd numbers between firstNum and secondNum inclusive.
		System.out.println("Odd numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		for(int i=firstNum;i<=secondNum;i++){
		    if (i%2 !=0){
		        System.out.print(i+" ");
		    }
		}
		//c. Output the sum of all the even numbers between firstNum and secondNum inclusive
		System.out.println("\nEven numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		for(int j=firstNum;j<=secondNum;j++){
		    if (j%2==0){
		        System.out.print(j+" ");
		    }
		}
		//d. Output all the numbers and their squares between 1 and 10.
		System.out.println("\nNumber\tSquare of the number");
		for(int n=1;n<=10;n++){
		    System.out.println(n+"\t"+n*n);
		}
		//e. Output the sum of the squares of all the odd numbers between
		//firstNum and secondNum inclusive.
		System.out.println("Sum of the squares of all the odd numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		int sum=0;
		for(int m=firstNum;m<=secondNum;m++){
		    if (m%2 !=0){
		        sum+=(m*m);
		    }
		}
		System.out.println(sum);
		//f. Output all the uppercase letters
        System.out.println("Uppercase Alphabets are: \n");
        for(char ch='A'; ch<='Z';ch++){
            System.out.print(ch+" ");
        }
	}
}

Question 11

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		//a. Prompt the user to input two integers: firstNum and secondNum.
		//(firstNum must be less than secondNum.)
		int firstNum;
		System.out.println("Enter first number: ");
		firstNum=in.nextInt();
		int secondNum;
		while(true){
		    System.out.println("Enter second number (must be greater than "+firstNum+"): ");
		    secondNum=in.nextInt();
		    if(secondNum>firstNum)
		        break;
		}
		//b. Output all the odd numbers between firstNum and secondNum inclusive.
		System.out.println("Odd numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		int i=firstNum;
		do{
		    if (i%2 !=0){
		        System.out.print(i+" ");
		    }
		    i++;
		}
		while(i<=secondNum);
		//c. Output the sum of all the even numbers between firstNum and secondNum inclusive
		System.out.println("\nEven numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		int j=firstNum;
		do{
		    if (j%2==0){
		        System.out.print(j+" ");
		    }
		    j++;
		}
		while(j<=secondNum);
		//d. Output all the numbers and their squares between 1 and 10.
		System.out.println("\nNumber\tSquare of the number");
		int n=1;
		do{
		    System.out.println(n+"\t"+n*n);
		    n++;
		}
		while (n<=10);
		//e. Output the sum of the squares of all the odd numbers between
		//firstNum and secondNum inclusive.
		System.out.println("Sum of the squares of all the odd numbers between "+firstNum+" and "+secondNum+" inclusive are:");
		int m=firstNum;
		int sum=0;
		do{
		    if (m%2 !=0){
		        sum+=(m*m);
		    }
		    m++;
		}
		while(m<=secondNum);
		System.out.println(sum);
		//f. Output all the uppercase letters
        System.out.println("Uppercase Alphabets are: \n");
        char ch='A';
        do{
            System.out.print(ch+" ");
            ch++;
        }
        while(ch<='Z');
	}
}

Output


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS