0% found this document useful (0 votes)
3 views3 pages

Discussion Cpe

The document describes various C programs that demonstrate different looping constructs (do-while, for, and while) to print numbers, calculate squares, factorials, and the sum of squares based on user input. Each program includes necessary libraries, prompts for user input, and implements logic to perform calculations using the specified loop type. It also notes the lack of input validation in some cases and suggests improvements for robustness.

Uploaded by

Jhon Mark David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Discussion Cpe

The document describes various C programs that demonstrate different looping constructs (do-while, for, and while) to print numbers, calculate squares, factorials, and the sum of squares based on user input. Each program includes necessary libraries, prompts for user input, and implements logic to perform calculations using the specified loop type. It also notes the lack of input validation in some cases and suggests improvements for robustness.

Uploaded by

Jhon Mark David
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. This C program prints numbers 1 to 10 using a do-while loop. It begins by including <stdio.

h> for printf


and <stdlib.h> for system("cls"), which clears the screen on Windows. The main function initializes n to
1. The do-while loop then prints n and increments it by 1 each iteration. The loop condition, while (n <=
10);, ensures the loop runs until n exceeds 10, resulting in the numbers 1 to 10 printed each on a new
line.

This C program prints the numbers 1 to 10 using a for loop. The program starts by including <stdio.h> for
input-output functions like printf and <stdlib.h>. In the main function, an integer variable num is
declared. The for loop initializes num to 1, and it iterates as long as num is less than or equal to 10. On
each iteration, printf prints the current value of num followed by a newline, and then num is
incremented by 1. When num reaches 11, the loop condition fails, ending the loop, and the program
completes its execution. This results in numbers 1 to 10 being printed each on a new line.

This C program prints numbers 1 to 10 using a while loop. It starts by including <stdio.h> for printf. In
main, the variable n is set to 1. The while loop runs as long as n is less than or equal to 10. Inside the
loop, printf outputs the value of n followed by a newline, and then n is incremented by 1. This continues
until n becomes 11, ending the loop and the program, with numbers 1 to 10 printed each on a new line.

2.The C program prompts the user for an integer and outputs a table of numbers and their squares,
from 1 up to the entered value. It includes `stdio.h` for input/output functions and initializes two
variables, `num1` (user input) and `num2` (starting at 1). After the user enters a number, the program
uses a `do-while` loop to print each value of `num2` and its square, continuing until `num2` exceeds the
input. The loop ensures at least one iteration, even if the input is small. While the program works as
intended, it lacks input validation for negative numbers and zero. Removing `stdlib.h` and adding
validation would improve the code's robustness.

This C program accepts an integer input from the user and displays a sequence of numbers along with
their squares using a for loop. The program includes the necessary libraries <stdio.h> for input-output
functions and <stdlib.h>, although the latter is not used here. In the main function, the program prompts
the user to enter an integer, storing it in the variable num1. It then prints a header "Number | Square".
A for loop is used to iterate through numbers from 1 to num1, printing each number (num2) alongside
its square (num2 * num2). The loop runs until num2 exceeds num1. For example, if the user enters 5,
the program will output a table with numbers from 1 to 5 and their corresponding squares.

This program accepts an integer from the user and displays a sequence of numbers along with their
squares using a `while` loop. The program starts by declaring two integer variables: `num1` to store the
user input and `num2` which is initialized to 1. It then prompts the user to enter an integer number
using `printf()` and captures the input with `scanf()`. The program prints a header "Number | Square"
and then enters a `while` loop that continues as long as `num2` is less than or equal to `num1`. In each
iteration, it prints the current value of `num2` and its square (calculated as `num2 * num2`), and then
increments `num2` by 1. The program continues to display the number and its square until `num2`
exceeds `num1`, after which it terminates successfully by returning 0. The program effectively
demonstrates the use of a `while` loop to display a sequence of numbers and their squares based on
user input.

3.This program calculates the factorial of a given positive integer using a `do-while` loop. The factorial of
a number is the product of all positive integers from 1 to that number. The program begins by declaring
two integer variables: `num1` to store the user's input and `factorial`, initialized to 1, to hold the
resulting factorial value. It prompts the user to enter a positive integer and captures the input using
`scanf()`. If the input is negative, the program outputs that the factorial is not defined for negative
numbers. If the input is 0, it outputs that the factorial of 0 is 1. For positive integers, the program uses a
`do-while` loop, which multiplies the current `factorial` value by `num2` (starting from 1) and increments
`num2` until it reaches the value of `num1`. Once the loop ends, the program displays the calculated
factorial. This implementation uses a `do-while` loop to ensure that the factorial calculation is executed
at least once, even if the input is 0.

This program calculates the factorial of an input number using a `while` loop. The factorial of a number
is the product of all positive integers from 1 to that number. The program begins by declaring two
integer variables: `num1`, which stores the user's input, and `factorial`, initialized to 1, to hold the result
of the factorial calculation. The program prompts the user to enter a positive integer and captures the
input using `scanf()`. If the input is a negative number, the program informs the user that the factorial is
not defined for negative numbers. If the input is 0, the program outputs that the factorial of 0 is 1. For
positive integers, the program enters a `while` loop that multiplies the `factorial` by `num2` (starting
from 1) and increments `num2` until it reaches `num1`. Once the loop finishes, the program prints the
calculated factorial. This approach uses the `while` loop to repeatedly multiply the current value of
`factorial` by each number in the sequence from 1 to `num1`.

This program calculates the factorial of a given positive integer using a `for` loop. The factorial of a
number is the product of all positive integers from 1 to that number. The program starts by declaring
the variable `num1` to store the user's input and initializes `factorial` to 1. It prompts the user to enter a
positive integer and captures the input using `scanf()`. If the input is a negative number, the program
informs the user that the factorial is not defined for negative numbers. If the input is 0, the program
outputs that the factorial of 0 is 1, as defined by the mathematical rule. For positive integers, the
program uses a `for` loop to iterate from 1 to `num1`, multiplying the `factorial` by each number in the
sequence. Once the loop completes, the program prints the calculated factorial. This solution uses the
`for` loop to perform the repetitive multiplication, making it an efficient way to compute the factorial for
a given input.
4.This program calculates the sum of the squares of integers from 1 to the user-inputted number (n)
using a `do-while` loop. It starts by declaring the variables: `num1` to store the user input, `sum` to hold
the cumulative sum, and `num2` initialized to 1 for the loop. The program prompts the user to enter a
positive integer and captures the input using `scanf()`. Then, the program enters a `do-while` loop where
it squares `num2` and adds the result to `sum` in each iteration. After each addition, `num2` is
incremented. The loop continues until `num2` exceeds `num1`. Once the loop completes, the program
prints the final sum of squares. The program uses a `do-while` loop to ensure that the sum is calculated
at least once, even if the input is 1. This approach can be easily adapted to use `for` or `while` loops for
similar functionality.

This C program calculates the sum of the squares of integers from 1 to the input number (n) using a `for`
loop. It starts by declaring two integer variables: `num1` to store the user’s input and `sum` to
accumulate the total sum of the squares, initialized to 0. The program prompts the user to enter a
positive integer and stores the input in `num1`. Using a `for` loop, it iterates from 1 to `num1`. In each
iteration, it squares the current value of `num2` (ranging from 1 to `num1`) and adds the result to `sum`.
After the loop finishes, the program prints the final sum of the squares. This program effectively
demonstrates how to use a `for` loop to calculate the sum of squares from 1 to n. It can be modified to
use other loop types like `while` or `do-while` to achieve similar results.

This C program calculates the sum of the squares of integers from 1 to the user-inputted number (n)
using a `while` loop. The program begins by declaring three integer variables: `num1` for storing the user
input, `sum` for holding the cumulative sum of the squares (initialized to 0), and `num2` (initialized to 1)
to control the loop. It then prompts the user to enter a positive integer and stores the input in `num1`.
The `while` loop iterates as long as `num2` is less than or equal to `num1`. In each iteration, the program
squares the current value of `num2` and adds the result to `sum`. The value of `num2` is incremented by
1 after each iteration. Once the loop finishes, the program prints the final sum of squares. This approach
demonstrates the use of a `while` loop to calculate the sum of squares from 1 to n.

You might also like