-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber_palindrome.c
101 lines (85 loc) · 1.46 KB
/
number_palindrome.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
} *head, *tail;
struct node *CreateNode(int data)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
if(n==NULL)
{
printf("memory not allocated");
return NULL;
}
else
{
n->data=data;
n->next=NULL;
n->prev=NULL;
return n;
}
}
struct node *createList(long num)
{
for(int j=1; j<=10; j++)
{
int i=num%10;
struct node *newNode=CreateNode(i);
num=num/10;
if(head==NULL)
{
head=newNode;
}
else
{
newNode->next=head;
head->prev=newNode;
head=newNode;
}
}
return head;
}
void DisplayList() {
struct node *temp=head;
if (head == NULL)
{
printf("List empty.");
}
else
{
printf("The list is: ");
while (temp != NULL)
{
printf("%d", temp->data);
temp = temp->next;
}
printf("\n");
}
}
void palindrome(long num)
{
long reversed = 0, remainder, original=num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num=num/10;
}
if (original == reversed)
printf("%ld is a palindrome.", original);
else
printf("%ld is not a palindrome.", original);
}
int main()
{
long num;
printf("enter a 10 digit number: ");
scanf("%ld",&num);
head=createList(num);
DisplayList();
printf("\n");
palindrome(num);
return 0;
}