C Program to find prime factors of number using stack

By | 12.04.2017

Prime factors of number using stack


Write a C Program to find prime factors of number using stack. Write a program that uses stack to print the prime factors of positive integers in descending order in data structures. Here’s simple Program to print prime factors of number using stack in C Programming Language.


What is Stack ?


Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack, just like a pile of objects.


Basic Operations : :


  • push() − Pushing (storing) an element on the stack.
  • pop() − Removing (accessing) an element from the stack.
  • peek() − get the top data element of the stack, without removing it.
  • isFull() − check if stack is full.
  • isEmpty() − check if stack is empty.

Below is the source code for C Program to find prime factors of number using stack which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to find prime factors of number using stack  */

#include<stdio.h>
#include<stdlib.h>
#define MAX 50

int isEmpty(int top, int stack_arr[]);
void PFactors( int num);
void push(int x, int *top, int stack_arr[]);
int pop(int *top, int stack_arr[]);

int main()
{
        int num;
        printf("Enter an integer : ");
        scanf("%d",&num);
        printf("Prime factors are : ");
        PFactors(num);

        return 0;

}/*End of main()*/

void PFactors( int num)
{
        int stack[MAX], top=-1, i;
        for(i=2; num!=1; i++)
                while(num%i == 0)
                {
                        push(i, &top, stack);
                        num = num/i;
                }
        while(top!=-1)
        {
                printf("%d ",stack[top]);
                pop(&top, stack);
        }
        printf("\n");
}/*End of PFactors()*/

void push(int x, int *top, int stack_arr[])
{
        if(*top == (MAX-1))
                printf("Stack Overflow\n");
        else
        {
                *top=*top+1;
                stack_arr[*top] = x;
        }
}/*End of push()*/

int pop(int *top, int stack_arr[])
{
        int x;
        if(*top == -1)
        {
                printf("Stack Underflow\n");
                exit(1);
        }
        else
        {
                x = stack_arr[*top];
                *top=*top-1;
        }
        return x;
}/*End of pop()*/

OUTPUT : :


/*  C Program to find prime factors of number using stack  */

Enter an integer : 10000
Prime factors are : 5 5 5 5 2 2 2 2

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.8 5 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Riya

Devise the radix sort algorithm to sort elements with alphanumeric characters. Illustrate it
with the help of an example.