..il AT&T; 4:01 PM Ca 47% + Documents Homework 229 P4.1Write programs with loops
ID: 3878206 • Letter: #
Question
..il AT&T; 4:01 PM Ca 47% + Documents Homework 229 P4.1Write programs with loops that compute a. The sum of all even numbers between 2 and 100 (inclusive). b. The sum of all squares between 1 and 100 (inclusive). c. All powers of 2 from 20 up to 220. d. The sum of all odd numbers between a and b (inclusive), where a and b are inputs. e. The sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 +7+7=17.) P4.3Write programs that read a line of input as a string and print a. Only the uppercase letters in the string. b. Every second letter of the string. c. The string, with all vowels replaced by an underscore. d. The number of digits in the string. e. The positions of all vowels in the string. P4.8Write a program that reads a word and prints each character of the word on a separate line. For example, if the user provides the input "Harry", the program prints 172 wordsExplanation / Answer
import java.util.Scanner;
public class Number_Operation
{
public static void main(String[] args)
{
double even_sum = 0; //The sum of all even numbers between 2 and 100 (inclusive).
for (int i = 0; i < 101; i += 2) {
even_sum += i;
}
System.out.println("Evens sum: " + even_sum);
double square_sum = 0; //The sum of all squares between 1 and 100 (inclusive).
for (int i = 1; i < 101; i++)
{
square_sum += i * i;
}
System.out.println("Squares sum: " + square_sum);
double powers_sum = 0; //All powers of 2 from 20 up to 220 .
for (int i = 0; i < 21; i++)
{
powers_sum += Math.pow(2, i);
}
System.out.println("Powers of 2 sum: " + powers_sum);
// The sum of all odd numbers between a and b (inclusive), where a and b are inputs.
Scanner input = new Scanner(System.in);
System.out.print("Range a: ");
int a_range = input.nextInt();
System.out.print("Range b: ");
int b_range = input.nextInt();
double odd_sum = 0;
for (int i = a_range; i <= b_range; i++)
{
if (i % 2 == 0)
{
odd_sum += i;
}
}
System.out.println("Odd numbers in range a-b sum: " + odd_sum);
// The sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)
System.out.print("Number: ");
int number = input.nextInt();
input.close();
int odd_digits_sum = 0;
while (number > 0)
{
int digit = number % 10;
if (digit % 2 != 0)
{
odd_digits_sum += digit;
}
number /= 10;
}
System.out.printf("Sum of %s's odd digits: %f", number, odd_digits_sum);
}
}
String Programme
import java.util.Scanner;
public class String_Operation
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("String: ");
String user_input = input.next();
input.close();
String output = "";
for (int i = 0; i < user_input.length(); i++) // Only the uppercase letters in the string.
{
char current_letter = user_input.charAt(i);
if (Character.isUpperCase(current_letter))
{
output += current_letter;
}
}
System.out.println(output);
for (int i = 0; i < input_string.length(); i += 2) // Every second letter of the string.
{
output += input_string.charAt(i);
}
System.out.println(output);
String input_string = input.next(); // The string, with all vowels replaced by an underscore.
for (int i = 0; i < input_string.length(); i++)
{
char current_letter = input_string.charAt(i);
char lower_letter = Character.toLowerCase(current_letter);
if (lower_letter == 'a' || lower_letter == 'e' || lower_letter == 'o' || lower_letter == 'u' || lower_letter == 'i')
{
StringBuilder modified_string = new StringBuilder(input_string);
modified_string.setCharAt(i, '_');
input_string = modified_string.toString();
}
}
input.close();
System.out.println(input_string);
int string_digits = 0; // The number of digits in the string.
for (int i = 0; i < user_input.length(); i++)
{
char current_char = user_input.charAt(i);
if (Character.isDigit(current_char))
{
string_digits += 1;
}
}
System.out.println("Number of digits: " + string_digits);
for (int i = 0; i < input_string.length(); i++) // The positions of all vowels in the string.
{
char current_letter = Character.toLowerCase(input_string.charAt(i));
if (current_letter == 'a' || current_letter == 'e' || current_letter == 'o'
|| current_letter == 'i' || current_letter == 'u')
{
output += String.format("%d ", i);
}
}
System.out.println("Positions of vowels: " + output);
}
//program that reads a word and prints each character of the word on a separate line.
import java.util.Scanner; .
public class Each_Chaeacter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Word: ");
String word = input.next();
input.close();
for (int i = 0; i < word.length(); i++)
{
System.out.println(word.charAt(i));
}
}
}
// Write loop that print all square < n
int square = 0;;
while(squared < n)
{
squared = square * square;
System.out.print(square * square + " ");
square++;
}
Output: 0 1 4 9 16 25 36 49 64 81
// All positive numbers that are divisible by 10 and less than n. For example, if n is 100
int x = 0;
while (x < n)
{
if(x % 10 == 0)
{
out.print(x + “ “)
}
x++
Output : -10 20 30 40 50 60 70 80 90
// All powers of two less thann. For example, ifnis 100, print 1 2 4 8 16 32 64.
int i = 0;
while (x < n)
{
int m = Math.pow(2, i);
out.print(m + “ “);
i++;
}
Outout:- 1 2 4 8 16 32 64
// while loop that compute the sum of all even numbers
sum = 0
for i in range(0, 101):
if i%2 == 0:
sum = sum + i;
// while loop that compute the sum of all square between 1 and 100
sum = 0
for i in range(0, 101):
sum = sum + i*i
// while loop that compute the sum of all odd number between a and b
a = int(input("Input a"))
b = int(input("Input b"))
sum = 0
for i in range(a, b+1):
sum = sum + i
// while loop that compute the sum of all odd digit of input n
word = input("Input a number")
sum = 0
for letter in word:
if isdigit(letter):
sum = sum + int(letter)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.