Factors of a Given Number using C++ Programming


The numbers that are completely divisible by the given number (it means the remainder should be 0) called as factors of a given number using for loop and if condition. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. The if statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.

Example :
     limit = 5
    5 , 1 => 5 % 1 = 0
    5 , 2 => 5 % 2 = 0
    5 , 3 => 5 % 3 = 1
    5 , 4 => 5 % 4 = 2
    5 , 5 => 5 % 5 = 0

The program is written in C++ programming language and it displays the factors of a given integer "n" using a for loop and an if statement. Here's a step-by-step explanation of the program:

  • #include<iostream> - This is a preprocessor directive that includes the iostream header file in the program. This file provides input and output operations in C++.
  • using namespace std; - This statement tells the compiler to use the standard namespace, which contains the standard C++ library.
  • int main() - This is the starting point of the program. The main() function is the entry point for all C++ programs.
  • int n; - This statement declares an integer variable "n" to store the number whose factors need to be calculated.
  • cout<<"\nEnter The Number : "; - This statement prints a message to the console, asking the user to enter the number whose factors need to be calculated.
  • cin>>n; - This statement reads the user input from the console and stores it in the variable "n".
  • for(int i=1; i<=n; i++) - This is a for loop that iterates from 1 to the value of "n". The loop variable "i" is initialized to 1 and incremented by 1 in each iteration until it reaches the value of "n".
  • if(n%i==0) - This is an if statement that checks if the remainder of "n" divided by "i" is equal to zero. If the remainder is zero, it means that "i" is a factor of "n".
  • cout<<i<<endl; - This statement prints the value of "i" to the console, which is a factor of "n".
  • return 0; - This statement ends the program and returns the value 0 to the operating system. The value 0 indicates that the program executed successfully.

Source Code

//Program to find the factors of given number
#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"\nEnter The Number : ";
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        if(n%i==0)
            cout<<i<<endl;
    }
    return 0;
}
 

Output

Enter The Number : 5
1
5
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples