Question

In: Computer Science

Unit 4: Discussion - Part 2 No unread replies.No replies. Question Read the lecture for Chapter...

Unit 4: Discussion - Part 2

No unread replies.No replies.

Question

Read the lecture for Chapter 4, and then answer the following:

This week covered the many different types of loops in Java: while, do, and for. Think of a brief example where you can use loops and write it in correct Java syntax. You may use the loop example you wrote in pseudo code for the discussion thread 2 during week 2 and convert it to Java, if you think it is appropriate. Be mindful that we are not talking about homework here. Do not post homework in this or any other discussion thread. You should show at least 2 of the 3 different types of Java loops in your posting. You may incorporate both kind of loops in the same example, or alternatively, show how the same example can be written using two different kinds of loops. Try to keep your example short, so others can follow it easily. Also, do not forget to show screen captures of your programs running.

Solutions

Expert Solution

//A simple program to ask for an integer and calculate its factorial.

Using a for loop

//////////////////////

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int num;
double factorial = 1;
System.out.print("Enter a number to find factorial: ");
num = sc.nextInt();
for(int i=1;i<=num;i++){
factorial*=i;
}
System.out.print("Factorial of "+num+" is: "+factorial);
}
}

//Using a while loop

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int num;
double factorial = 1;
System.out.print("Enter a number to find factorial: ");
num = sc.nextInt();
int i = num;
while(num>0)
{
factorial*=num;
num-=1;
}
System.out.print("Factorial of "+i+" is: "+factorial);
}
}


Related Solutions

Unit 4: Discussion - Part 1 Question Read the lecture for Chapter 4, and then answer...
Unit 4: Discussion - Part 1 Question Read the lecture for Chapter 4, and then answer the following: Think of a brief example where you can use conditional statements and write it in correct Java syntax. You may use if-else-if statements or the switch statement. You may also use the example you wrote in pseudo code for the discussion thread 2 during week 2 and convert it to Java, if you think it is appropriate. Be mindful that we are...
Lab: Chapter 4 Discussion Question - Documentation and Informatics 55 unread replies.55 replies. The nurse on...
Lab: Chapter 4 Discussion Question - Documentation and Informatics 55 unread replies.55 replies. The nurse on the surgical intensive care unit is in the process of documenting care for her 38-year-old patient using the electronic health record (EHR). The patient was involved in a motor vehicle accident and suffered a crush injury to her right leg, which has been amputated below the knee. The patient is a single mother to a 5-year-old daughter and works as a waitress. The patient...
Discussion Board Week 4 Day 1 No unread replies.No replies. A. Explain what it means to...
Discussion Board Week 4 Day 1 No unread replies.No replies. A. Explain what it means to serve as the patient’s advocate. B. Discuss the importance of advocating for the patient. C. Why is treating patients with dignity and respect important? D. Why is trust a key to preventing lawsuits? E. What may you write about your clinical assigned patient on Facebook? F. How is the HIPAA Privacy Rule different from the Security Rule?
Discussion 4 No unread replies.No replies. Case Study Charlie is a 3-year-old at a community preschool...
Discussion 4 No unread replies.No replies. Case Study Charlie is a 3-year-old at a community preschool facility. The staff reported he tends to put everything in his mouth and is frequently found eating dirt. A home visit by a school staff member to discuss the problems with his parents revealed a generally clean environment in a very old home that was being renovated. Recently Charlie has been falling well behind in academic skills compared to other children in his class;...
Weekly Discussion (Due Today by 11:59PM) No unread replies.No replies. Prepare a comparative chart based on...
Weekly Discussion (Due Today by 11:59PM) No unread replies.No replies. Prepare a comparative chart based on the physical differences you would see when performing a physical survey of a client who had good nutritional status versus a client with poor nutritional status. Grading Rubric to Achieve the Full 5 Points (See Grading Rubric for Complete Grading Breakdown) Demonstrates critical thinking. Student answers question(s) and relates key points to the discussion topic. Student uses Internet sources from acceptable research sites (see...
Chapter 5 Discussion No unread replies. No replies. In previous chapters, we have learned about the...
Chapter 5 Discussion No unread replies. No replies. In previous chapters, we have learned about the role of public health, social determinants and behavior and social theories related to health. This week, we are broadening our discussion to include how law, health policy and ethics impact health, quality of life and health outcomes. In this context, you will gain a better perspective of the role of public health and the overall goal of improving health, including the importance of multi-sectoral...
Trade Wars Through Steel Tariffs No unread replies.No replies. Please read the Trade Related articles under...
Trade Wars Through Steel Tariffs No unread replies.No replies. Please read the Trade Related articles under "Other Readings" for this Week, before you answer the following questions. Remember to use concepts learned in Chapter 3 in your responses. ANSWER ALL PARTS OF THE QUESTION. You will lose points if you do not address all questions in this Discussion Board. NORMATIVE POSTS AND LATE POSTS WILL NOT BE GRADED. Wikipedia, Newspaper Articles, Blogposts and Opinion articles online are NOT VALID REFERENCES....
Hypothetical Telecommunications Discussion 2929 unread replies.2929 replies. Please read the case and answer the questions at...
Hypothetical Telecommunications Discussion 2929 unread replies.2929 replies. Please read the case and answer the questions at the end. Please respond to two of your peers. Do you agree or disagree? Sales at a large telecommunications company were down for the third quarter. Management reviewed several strategies to improve sales and concluded that one solution would be to improve training for the large, dispersed sales force. For the sake of expediency, the training department began using a needs assessment it conducted...
Discussion Board - Unit 6, Lesson 1 9999 unread replies.9999 replies. 1) Discuss the purpose of...
Discussion Board - Unit 6, Lesson 1 9999 unread replies.9999 replies. 1) Discuss the purpose of academic standards? How do teachers use them to plan for instruction? What are your personal thoughts on how standards are used to plan and assess student learning? 2) What are some of the key benefits of using academic standards for planning? What are some of the arguments that critics have to say about academic standards? Explain using evidence from the text. FOR EDUCATION
Discussion for Chapter 29 ( principle of Economics Twelfth Edition) No unread replies.11 reply. At the...
Discussion for Chapter 29 ( principle of Economics Twelfth Edition) No unread replies.11 reply. At the end of the presentation and notes for this chapter,29 there is a section that discusses the pros and cons of government deficits. Choose a side (pro or con) and argue your reasoning for choosing that stance. Use sound economic concepts/theories when answering (not your opinion). You may need to use outside sources to develop your argument. If so, cite your sources.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT