Q5 A positive integer is entered through the keyboard. Write a function to obtain the prime factor of this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.

Function For Prime Factors

Program: 126

A positive integer is entered through the keyboard. Write a function to obtain the prime factor of this number.
For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.

#include<stdio.h>
int prime(int x);
void main()
{
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    prime(num);
}

int prime(int x)
{
    int a;
    for(a=2;a<=x;a++)
    {
        if(x%a==0)
        {
            printf("%d, ",a);
            x = x/a;
            a--;
        }
    }
}

Output:

Enter an integer: 36
2, 2, 3, 3,
Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.