Question

In: Computer Science

Use a For loop to compute the sum of all the odd numbers from 1 through...

  1. Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500.
  2. Print your name 7 times using a While loop.
  3. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"};
    Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop.
  4. Reverse the logic for zyBook Challenge activity 4.6.1 (Nested loops: Indent text) so that the first line is indented userNum number of spaces and the subsequent lines are each indented one less space until the last value is not indented at all (‘s’ indicates one space):
    sssss5
    ssss4
    sss3
    ss2
    s1
    0
  5. String method practice:
    • Create a String variable that contains your full name
    • Repeat the following steps 5 times (note you will use predefined String methods to modify your name as indicated):
      • Generate a random number between 1 and 4
      • If number is 1, display your name in all upper case
      • If number is 2, display your name in all lower case
      • If number is 3, replace your first name with “Three” and display your name
      • If number is 4, display the character at index 4

Solutions

Expert Solution

SumOfOdd.java

class SumOfOdd
{
   public static void main(String args[])
   {
       //sum variable which store the sum of odd numbers
       int sum=0;
       //iterate for loop from 1 to 99 inclusively
       for(int i=1;i<=99;i++)
       {
           //If i is odd then add i to sum
           if(i%2==1)
           {
               sum=sum+i;
           }
       }
       //print the reuslt
       System.out.println("Sum of odd numbers between 1 to 99 is: "+sum);
   }
}

output screenshot:

NamePrint.java

class NamePrint
{
   public static void main(String[] args)
   {
       //n denotes number of times to print
       int n=7;
       //name initialization
       String name="ABC DEF GHI";
       //Iterate while loop for 7 times
       while(n>0)
       {
           //Print the result
           System.out.println(name);
           //decrement n by 1
           n--;
       }
   }
}

output screenshot:

ArrayPrint.java

class ArrayPrint
{
   public static void main(String[] args)
   {
       //array of names initialization
       String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"};
       //Iterate over array of names
       for(int i=0;i<dogNames.length;i++)
       {
           //print horizontal manner
           System.out.print(dogNames[i]+" ");
       }
       //New line for printing result in vertical manner
       System.out.println();
       //Iterate over array of names
       for(int i=0;i<dogNames.length;i++)
       {
           //Print vertical manner
           System.out.println(dogNames[i]);
       }      
   }  
}

output screenshot:

IndentText.java

class IndentText
{
   public static void main(String[] args)
   {
       //initialization of userNum
       int userNum=5;
       //iterate userNum times
       for(int i=userNum;i>=0;i--)
       {
           //iterate from 1 to i inclusive
           for(int j=1;j<=i;j++)
           {
               //print s denotes the space
               System.out.print("s");
           }
           //print i
           System.out.println(i);
       }  
   }
}

output screenshot:

StringOperation.java

//importing Random class
import java.util.Random;
class StringOperation
{
   public static void main(String[] args)
   {
       //Initialization of fullName
       String fullName="ABCDEFG HIJK";
       //creating object for Random class
       Random rand = new Random();
       //n denotes the number of time that while loop repeats
       int n=5;
       while(n>0)
       {
           //generate random numebr between 1 to 4
           int i=rand.nextInt(4)+1;
           //If i is 1 then print full name in upper case
           if(i==1)
           {
               System.out.println(fullName.toUpperCase());
           }
           //If i is 2 then print full name in lower case
           else if(i==2)
           {
               System.out.println(fullName.toLowerCase());
           }
           //if i is 3 then replace first name with Three
           else if(i==3)
           {
               //find the index of first space
               int index=fullName.indexOf(' ');
               //repalce first name with Three
               fullName="Three "+fullName.substring(index+1);
               System.out.println(fullName);
           }
           //If i is 4 print the character at 4th index
           else if(i==4)
           {
               System.out.println(fullName.charAt(4));
           }
           //decrement n by 1
           n--;
       }
   }
}

output screenshot:


Related Solutions

Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
Write an application named OddNums that displays all the odd numbers from 1 through 99. All...
Write an application named OddNums that displays all the odd numbers from 1 through 99. All ODD numbers should show all together(Should not need to scroll down), do it in 10 rows. FOR C#
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Write a loop for each of the following: The sum of all even numbers between 2...
Write a loop for each of the following: The sum of all even numbers between 2 and 100 (inclusive). The sum of all squares between 1 and 100 (inclusive). (If you use the pow function the first parameter must be a float or double). The sum of all odd numbers between a and b (inclusive). Where a and b are read in by the user. How often do the following loops execute? Assume that i is not changed in the...
Write down a while loop that calculates and displays the SQUARE of ten ODD numbers (from...
Write down a while loop that calculates and displays the SQUARE of ten ODD numbers (from 1 to 19) in a loop style execution: C++
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write assembly instructions that compute the following: The sum of all even numbers between 2 and...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and 100 (inclusive) -- the answer should be 2550 Prompt the user for 2 values, put them in variables a and b, then find the sum of all odd numbers between a and b. The sum of all the squares between 1 and 100 (inclusive) . -- the answer should be 338350 All of the statements above should print the answers using print_int MUST BE...
(a) Use a direct proof to show that the product of two odd numbers is odd....
(a) Use a direct proof to show that the product of two odd numbers is odd. (b) Prove that there are no solutions in integers x and y to the equation 2x2 + 5y2 = 14. (c) Prove that the square of an even number is an even number using (a) direct proof, (b) an indirect proof, and (c) a proof by contradiction. Q. 2. Maximum score = 25 (parts (a) 9 points, part (b-i) and (b-ii) 8 points) (a)...
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT