Simple While-End Loop Example: Counting Down from a User-Entered Number

 Create your own unique While-End repetition structure. You decide the theme. You should provide both the pseudocode and the flowchart of your example. Be sure to provide an overview of what your repetition structure is doing.Please keep the design simple for this exercise. Just a few lines of code is all that is needed for this response. Also provide the C code and results of testing your code in an online compiler,(please use indeone.com). Again, this should be a simple loop with just a few lines of code.

Simple While-End Loop Example: Counting Down from a User-Entered Number


The answer


Overview:

This repetition structure prompts the user for a positive integer and then counts down from that number to 1, displaying each number. The loop continues while the number is greater than 0.


Pseudocode:



Prompt user to enter a positive integer Read number While number > 0 Display number Decrement number by 1 End While

Flowchart (Text Version):



+-------------------------------+ | Start | +-------------------------------+ | v +-------------------------------+ | Prompt user for number | +-------------------------------+ | v +-------------------------------+ | Read number | +-------------------------------+ | v +-------------------------------+ | Is number > 0? | +-------------------------------+ / \ Yes No / \ +-------------------------+ | | Display number | | | Decrement number by 1 | | +-------------------------+ | | | +-----------<--------------+ | v +-------------------------------+ | End | +-------------------------------+

C Code:

c

#include <stdio.h> int main() { int number; printf("Enter a positive integer: "); scanf("%d", &number); while (number > 0) { printf("%d\n", number); number--; } return 0; }

Results of Testing on Ideone.com:

Input:


5

Output:



Enter a positive integer: 5 4 3 2 1

This example uses a basic while loop to count down from the user's number to 1.
Simple, clear, and effective for demonstrating the While-End repetition structure.


📩 Need a similar solution? Email me: adel455@hotmail.com


Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming