write a program to print all the factors of 20 with output
Answers
menu
home
TUTORIAL
EXAMPLES
search
C Program to Display Factors of a Number
Example to find all factors of an integer (entered by the user) using for loop and if statement.
Factors of a number
To understand this example, you should have the knowledge of following C programming topics:
C Programming Operators
C if...else Statement
C Programming for Loop
This program takes a positive integer from the user and displays all the positive factors of that number.
Example: Factors of a Positive Integer
#include <stdio.h>
int main()
{
int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;
}
Output
Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 10 12 15 20 30 60
In the program, a positive integer entered by the user is stored in variable number.
The for loop is iterated until i <= number is false. In each iteration, whether number is exactly divisible by i is checked (condition for i to be the factor of number) and the value of i is incremented by 1.
Check out these related examples:
Check Whether a Number is Positive or Negative
Count Number of Digits in an Integer
Print an Integer (Entered by the User)
Check Prime or Armstrong Number Using User-defined Function
Check Whether a Number is Prime or Not
Check Whether a Number is Even or Odd
Find Factorial of a Number
Check Whether a Number can be Expressed as Sum of Two Prime Numbers
C Examples
Check Whether a Number is Even or Odd
Check Whether a Character is Vowel or Consonant
Find the Largest Number Among Three Numbers
Find all Roots of a Quadratic Equation
Check Leap Year
Check Whether a Number is Positive or Negative
Check Whether a Character is an Alphabet or not
Receive the latest tutorial to improve your programming skills
Enter Your Email
Join
RECOMMENDED READINGS expand_more
Get Latest Updates on Programiz
Enter Your Email
Subscribe
TUTORIALS
Python Tutorials
C Tutorials
Java Tutorials
Kotlin Tutorials
C++ Tutorials
Swift Tutorials
R Tutorials
Algorithms Tutorials
EXAMPLES
Python Examples
C Examples
Java Examples
Kotlin Examples
C++ Examples
R Examples
COMPANY
About
Advertising
Contact
LEGAL
Privacy Policy
Terms And Conditions
Copyright © Parewa Labs Pvt. Ltd. All rights reserved.
By using Programiz, you agree to use cookies as stated in our Privacy policy Continue
Following are the program in C++ that is mention below
Explanation:
#include <iostream> // header file
using namespace std; // namespace
int main() // main function
{
int n2=20; // variable declartion
int k;// variable declaration
cout<<" the factors are :";
for(k=1; k<= n2; ++k) // iterating the loop
{
if (n2%k == 0) // check condition
{
cout<<k<<" "; // display factor
}
}
return 0;
}
Output:
the factors are :1 2 4 5 10 20
Description of program
- Declared a variable n2 as integer type and store 20 to them.
- iterating the for loop to print the factor of 20 .The if condition will all the factor of 20.
Learn More :
- brainly.in/question/2323394