Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

lab 5 c programming 1. Lab Target (1) Be able to use different looping structures to solve problems (2) Be able to use nested looping

lab 5 c programming
image text in transcribed
image text in transcribed
1. Lab Target (1) Be able to use different looping structures to solve problems (2) Be able to use nested looping structure (3) Be able to combine decision making structure to solve sophisticated problems 2. Tools Operation System: Windows XP Programming Language: C Textbook: C Programming A Q&A approach 3. Tasks (After finish the following tasks, hand in Lab Reports, all source code, and snapshot of running results) 3.1 Task One: While loop practice C program to find sum of even number from 1 to N.Logic to find sum of even numbers. Step by step descriptive logic to find sum of even numbers. 1. Input upper limit to find sum of even number. Store it in some variable say N 2. Initialize another variable to store sum with O say sum = 0 3. To find sum of even numbers we need to iterate through even numbers from 1 to n. Initialize a loop from 2 to N and increment 2 on each iteration 4. Inside the loop body add previous value of sum with i.e. sum = sum + i. 5. After loop print final value of sum The execution of the program should be like the following: Input upper linit N: 10 Sun of even number fron 1 to N: 39 Process exited after 3.883 seconds with return value 34 TRE NER... 3.2 Task Two: For loop practice The Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence, FO=0, F1=1, Fn=Fn-1+Fn-2 (n=2, NEN"): Use a for loop to implement the task. Logic to print Fibonacci series up ton terms: Step by step descriptive logic to print n Fibonacci terms. 1. Input number of Fibonacci terms to print from user. Store it in a variable say terms 2 Declare and initialize three variables, I call it as Fibonacci magic initialization F0-0,F1-1 and F2-FO+F1. Here F2 is the current(n) term, F1 is the nth term and FO is n2th term. 3. Run a loop from 1 to terms, increment loop counter by 1. The loop structure should look like for(i=1;i termi++). It williterate through n terms 4. Inside the loop copy the value of n-1th term to n-2th term ie. FO-F1. Next, copy the value of nth to n-1th term F1 = F2 Finally compute the new term by adding previous two terms ie. F2 - F1+FO 5 Print the value of current Fibonacci termie. F2 The execution of the program should be like the following: PEALecture_ZJUTC Programming 2015-2016121 a b6-2.exe How many Fibonacci numbers you want to print out? 20 1 1 2 3 5 8 13 21 34 55 89144 233 377 610 987 1597 2584 4181 6765 3.3 Task Three: Do-while loop practice Use a do-while loop and the following formula to calculate the value of Analyze: The first approximation start from 4.0 A more precise approximation done by add an item that is 1.0/add_item The sign of the added item is changed to its opposite after each addition The added item item is incremented by 2 after each addition It terminates when the approximation doesn't change too much between additions Procedure: define variables: pi, add_item, precision, sign initialize variables use proper loop to calculate the sequence precision=1.0e-6 pi +=4.0*sign*1.0/add_item; sign *--1.0; add_item += 2.0

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

Explain the steps of a criminal prosecution.

Answered: 1 week ago