Program to find factors of a Number

Description

To find factors of a number, check number divisible from 1 to n,
if it divisible print it.

C/C++

/* C Program to find factors of a Number */
//Save it as FactorsNumber.c

#include<stdio.h>
int main(){

    int i, num;
    printf("Enter a number : ");
    scanf("%d",&num);

    printf("The factors of %d is ", num);
    for(i=1;i<=num;i++) {
        if((num % i) == 0) {
            printf("\n%d",i);
        }
    }
}
Input:
Enter a number : 18

Output:
The factors of 18 is
1
2
3
6
9
18

Java

/* Java Program to find factors of a Number */
//Save it as FactorsNumber.java

import java.io.*;
import java.util.Scanner;

public class FactorsNumber {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a number : ");
        int num = scanner.nextInt();
        
        System.out.println("The factors of "+ num);
        for(int i=1;i<=num;i++) {
            if((num % i) == 0) {
                System.out.println(i);
            }
        }
    }
}
Input:
Enter a number : 
18

Output:
The factors of 18
1
2
3
6
9
18

Related Programs

1) Program to find modulus of two numbers
2) Program to find Quotient and Remainder
3) Program to calculate power of a number
4) Program to calculate percentage mark of student
5) Program to reverse a number
6) Program to calculate Factorial of a Number
7) Program to display multiplication table of a number
8) Program to convert Decimal to Binary
9) Program to find LCM(Least Common Multiple)
10) Program to add two complex numbers
Share Me

Leave a Reply