C++ Program to display factors of a number

A number that divides into another number exactly and without producing a leftover is said to be a factor. For instance, the factors of 20 are 1, 2, 4, 5, 10, and 20. Lets

1. Header Inclusion

The input and output stream functions from the C++ Standard Library are included at the beginning of the program with #include <iostream>. It enables the usage of cin for input and cout for output by the program.

2. Namespace Declaration

Stating that we are using the std namespace is known as using namespace std. By eliminating the need to explicitly mention std:: each time you use cout and cin, this declaration helps to streamline the code.

3. Declarations for variables

There are two integer variables specified.

  • num_1: The user-inputted positive integer will be stored in this variable.
  • k: This variable serves as a loop counter to go over all of the potential factors of num_1.
  • The user's input
  • Using the command cout "Enter the positive integer: ";, the program asks the user to provide a positive integer.
  • The command cin >> num_1; reads and stores the user's input in the num_1 variable.

5. Initialization of the output

The cout "Factors of the number " num_1 " are: "; is used to display a message informing the user that the program will list the factors of the entered number.

6. The factor calculation loop

  • The loop variable k is initialized to 1 once the program enters a for loop. The k must be less than or equal to num_1 for the loop to continue.
  • It checks if num_1% k == 0 inside the loop. In this condition, the residue after dividing num_1 by k is used to determine whether k is a factor of num_1. If there is no residue, then k divides num_1 into an even number, making it a factor.
  • If k is a factor, cout k " "; is used to print the value to the console.

7. End of the Program

The program uses the return 0; statement to return 0 To let the operating system know that the loop has ended successfully.

The offered C++ code creates a straightforward program that displays a positive integer's factors after receiving a positive integer from the user.

Explanation:

  1. In this example, the program begins by including the iostream header file, which is required for Input and output operations, and then declares the using namespace std; line, which enables you to use objects and functions from the std (standard) namespace without explicitly mentioning it.
  2. There are two declared integer variables: num_1 and k. The user-inputted positive integer will be stored in the variable num_1, and the loop counter k will be used to check for factors.
  3. With the instruction "Enter the positive integer:," the program asks the user to enter a positive integer. After that, it receives the user's input and uses cin to store it in the num_1 variable.
  4. Following the value of num_1 using cout, it shows the message "Factors of the number".
  5. The program begins a for loop and initializes the loop variable k to 1. The looping continues as long as k is less than or equal to num_1, at which point the program exits the for loop.
  6. Utilizing the modulo operator (%), the loop determines whether num_1 is divisible by k. It indicates that k is a factor of num_1 if the remainder of the division is 0.
  7. The console prints k, followed by a space, if it is a factor.
  8. The program ends and returns 0 at the end of the loop to signify that it was successful in running.

This C++ program accepts a user-supplied positive integer as input, calculates its factors by repeatedly iterating through all possible divisors from 1 to the input number, then displays the factors. It makes use of looping, conditional statements, and simple input/output operations to complete this objective.