C Programming – HackerRank Solution | Palindrome Stack Integer |

(HackerRank) Write a Modular C Programming code to solve Palindrome Stack Integer, Micky is very fond of stacks data structure. He started experimenting the usage of stack for different purposes.

Micky is very fond of stacks data structure. He started experimenting the usage of stack for different purposes. He knew how to reverse an integer number while learning C programming. Now, the same task he wants to achieve using Stacks.

Help Micky to perform reversing and also to check whether an integer number is a palindrome.

Input Format

first line reads the integer.

Constraints

1< Integer <=1000000

Output Format

Print the Number is palindrome or not.

Sample Input 0

121

Sample Output 0

121 is a palindrome number

Sample Input 1

3334

Sample Output 1

3334 is not a palindrome number

Sample Input 2

-55

Sample Output 2

Invalid number 

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SIZE 100

struct stack
{
    int data[SIZE];
    int top;
};
void push(struct stack *sptr, int num);
void check(struct stack *sptr,int no);
int pop(struct stack *sptr);

void push(struct stack *sptr, int num)
{
    if(sptr->top==SIZE-1)
    {
        printf("Stack Overflow\n");
    }
    else
    {
        sptr->top++;
        sptr->data[sptr->top]=num;
    }
}
int pop(struct stack *sptr)
{
    int num;
    num=sptr->data[sptr->top];
    sptr->top--;
    return num;
}

void check(struct stack *sptr,int no)
{
    int temp,num=no,copy,c=0,stat=1;
    while(no!=0)
    {
        temp=no%10;
        push(sptr,temp);
        c++;
        no=no/10;
    }
    copy=num;
    while(copy!=0)
    {
        temp=copy%10;
        if(temp!=pop(sptr))
        {
            stat=0;
            break;
        }
        copy=copy/10;
    }
    if(stat==1)
    {
        printf("%d is a palindrome number",num);
    }
    else
    {
        printf("%d is not a palindrome number",num);
    }
    
    
}
int main()
{
    struct stack * sptr;
    struct stack s;
    int no;
    sptr=&s;
    sptr->top=-1;
    scanf("%d",&no);
    if(no>0)
    {
        check(sptr,no);
    }
    else
    {
        printf("Invalid number ");
        exit(0);
    }
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

121
Your Output (stdout)

121 is a palindrome number
Expected Output

121 is a palindrome number

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!