Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Learn the Repetition Structures by covering the following loops: 1. whilestatement 2. forstatement Task 1: Counting Loops using while/for statements Write Compile & Run the

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Learn the Repetition Structures by covering the following loops: 1. whilestatement
2. forstatement
Task 1: Counting Loops using while/for statements
Write Compile & Run the following C program in Dev-C++.
Save the source file in a new folder Lab5 as Lab5/program1.c
CSE 103: Computer Programming in C Lab \#5: Repetition Structures Obiective: Learn the Repetition Structures by covering the following loops: 1. while statement 2. for statement Task 1: Counting Loops using whilefor statements Write Compile \& Run the following C program in Dev-C+1. Save the source file in a new folder Lab5 as Lab5/program1. C 1. Draw the flowchart of the program Use pen and paper to draw the flowchart then take a photo and attach it here 2 3. Run the Debugger on the program several times and write down your observations regarding nested if statement. Observations: 4. Rewrite the two assignment statements appearing inside the loop body using conppound assginement operators. Give a short comment about compound arsginement operators Compovand assginement operators: 5. Rewrite the two assignment statements appearing inside the loop body using increment/decriment operators (if possible!). Give a short comment about. increment/decriment operatorsisorstsed $1315 of Leat Ad) Increment/decriment operators: 6. Rewrite the program using for statement: Save the modified program as Iab5/program2. c. Compile and debug the program. Observe the execution flow of the program! 1 c progran for conputing the summation: 1+2++$ uaing for statement*/ Task 2: Computing n! In mathematies, the factorial of a positive integer n, denoted by nt, is defined as: For example, n!=123n 5!=12345=120 Write a C program that computes n! using for statement. Save the source file in as Lab5/program 3.C Compile \& Run the C program in Dev.C+. / computing n!:12n using for statement / \#include int main (void) i return 0; 1 ; Task 3: Computing n' usine another definition In mathematics, n! sometimes is defined as: For example, n!=n(n1)(n2)321 5t=54321=120 Task 4: Computing n! using while statement Rewrite program4. e using while statenent! Save the source file in as Lab 5/ program 5.c Compile \& Run the C program in Dev-C++. /* conputing n! as n(nl)(n2)321 using while statement / Finclude int main (void) i // try to write cortect while statement equivalent to / for statement shown in program4 + c return 0 ; Task 5: Check Prime The following C program asks the user to enter an integer number and checks whether the number is prime or not. A prime number (2,3,5,7,11,13,17,19,23,29,31,37) is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number N is prime, divide it in a loop from 2 to N1 for remainder. If remainder is never 0 , then N is prime. For example, 23 and 47 are prime, but 12 and 27 are not. Save the source file in as Lab5/program 6.c Compile \& Run the C program in Dev-C ++. Run the Debugger on the program twice: In the first run, enetr a prime number, e.g., 7. Observe the execution flow of the program. In the second run, enter a non-prime number, e.g. 9 and observe the program execution flow. Observe the value of the vatiable count and is prime at cach iteration. Observations: Repetition Structures Loops are basically means to do a task multiple times, without actually writing the repeated statements over and over again. The format of a while loop is as follows: while (condition) I Statements; If the block of statements to be repeated is one, then the brackets I I are not required. The condition is evaluated, and if it is true, the code within the block is executed. This repeats until the condition becomes false. We can think of a while loop as a repeating if statemeat. for loop is another type of loop which allows for initialization and iteration control. Its format is for(initialization; condition; update) \& statements; The initialization part is executed first followed by the condition. If the condition is true, the code within the block is executed. Once at the end of the loop, we go to update, then condition. If the condition is true the statements are executed again and so on. Note that the execution part is executed only one time when the for loop starts. Each for loop can be converted easily into while loop. For and while loop are pre-test loops i.e. the iteration is cxecuted based on the condition being true. Thus, we can have zero iteration in case the condition is false at the beginning

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

8. Demonstrate aspects of assessing group performance

Answered: 1 week ago