#include void main() { long int n; printf("ENTER A NUMBER: "); scanf("%ld",&n); int n1,mod; n1=n; int rev=0; while(n>0) { mod = n%10; rev = rev * 10 + mod; n = n / 10; } if (n1 == rev) printf("ENTERED NUMBER IS A PALINDROME\n"); else printf("ENTERED NUMBER NOT A PALINDROME\n"); }
#include<stdio.h>
unsigned reverse (unsigned num) {
unsigned dgt, acc = 0;
while (num>0) { dgt = num%10; // determine last digit of num (remainder after division by 10)
rev = rev * 10; // left-shift the accumulator by 1 decimal digit
rev = rev + dgt; // add the digit
num = num / 10; // right-shift the number by 1 decimal digit
}
return acc; // the accumulator is the reverse of original number
}
bool is_palindrome (unsigned num) {
return num == reverse (num);
}
int main (void) {
unsigned num;
printf ("Enter a number greater than zero: ");
scanf ("%u\n", &num);
if (is_palindrome (num))
printf ("The number is a palindrome\n");
else
printf ("The number is not a palindrome\n");
return 0;
}
//assume all files r included.
void main()
{
int n,n1,s=0,r;
cout<<"enter the number";cin>>n;
n1=n;
while(n)
{r=n%10;
s+=r*10;
n=n%10;
}
if (n1==s)
cout<<"no. is palindrome";
else cout<<endl<<"no. is not palindrome";
}
The easiest way to check if a number is a palindrome or not is to reverse the number and see if the value is the same as the original. To reverse a number in any base, use the following function:
unsigned int reverse(unsigned int num, unsigned int base=10)
{
unsigned int rev=0;
while( num )
{
rev*=base;
rev+=num%base;
num/=base;
}
return( rev );
}
3
#include
#include
main()
{
int n, num, digit, sum=0, rev=0;
printf("Input the number");
n=num;
do
{
digit=num%10;
sum+=digit;
rev=rev*10+digit;
num!=10;
}
while (num!=0);
printf("sum of the digits of the number=%d%d%d%d\n",sum);
printf("reverse of the number=%d%d%d%d%d%d%d\n", rev);
if(n==rev)
printf("the number is a palindrome\n");
else
print("the number is not a palindrome\n");
getch();
}
OUTPUT:
Run1:- Input the number 1234
sum of the digits of the number= 10
reverse of the number= 4321
the number is not a palindrome
Run2:- Input the number 1221
sum of the digits of the number= 6
reverse of the number= 1221
the number is a palindrome
/* Palindrome program by Shekhar */
#include<stdio.h>
#include<conio.h>
void main() {
int n, i = 0, k = 0;
clrscr();
printf( "Enter number: " );
scanf( "%d", &n );
int p = n;
while( n != 0 ) {
k = k * 10 + ( n % 10 );
n = n / 10;
i++;
}
if ( k==p ) {
printf( "number is palindrome\n" );
} else {
printf( "number is not palindrome\n" );
}
getch();
}
Reverse the digits then test to see if the new number is the same as the original number. If it is, the number is a palindrome, otherwise it is not.
To reverse an unsigned integer (any base, default is 10):
unsigned rev (unsigned num, unsigned base=10)
{
unsigned r=0;
while (num)
{
r*=base;
r+=num%base;
num/=base;
}
return r;
}
Checking a number to see if it's palindromic in nature is a fun task. If the data were in arrays, all you would need to do is:
- calculate the halfway point in the array
- loop "count" from 0 to halfway-1 {
-- ch1 = arr[count]
-- ch2 = arr[array length - count - 1]
-- if ch1 and ch2 are not equal, return false
- }
- return true
If you're using an int, however, then to keep things simple (and probably faster) you may wish to break the integer down into each of its digits using the "%" modulus and "/" divide operators) while storing each of those digits in an array. Then pass that array into the function above. The bonus is that you know the maximum size of the array depends upon the size of the integer. If you're using a regular ol' "int", the maximum value's going to be about 2 billion, so 10 digits are going to be the max. Negative integers won't be palindromic because of the prefixed "-".
The author will leave it to you, the intrepid programmer, to write the code. Enjoy!
#include
#include
main()
{
int n, num, digit, sum=0, rev=0;
printf("Input the number");
n=num;
do
{
digit=num%10;
sum+=digit;
rev=rev*10+digit;
num!=10;
}
while (num!=0);
printf("sum of the digits of the number=%d%d%d%d\n",sum);
printf("reverse of the number=%d%d%d%d%d%d%d\n", rev);
if(n==rev)
printf("the number is a palindrome\n");
else
print("the number is not a palindrome\n");
getch();
}
just use the modulo (%) fcn to get the palindrome of the number.
the algorithm is for you to think so you'll know.
it is just easy. try it!
This program only suits PHP. If you want a proper one try C program for it available on web <body> <?php if(isset($_POST['submit'])) { $text = $_POST['text']; $string = mysql_real_escape_string($text); $invert = strrev($string); if($string == $invert) { echo "<br>Given number is a palindrome!!"; } else { echo "<br>Given number is not a palindrome!!"; } } ?> <form method="post"> <input type="text" name="text" id="text" /> <input type="submit" name="submit" value="Submit" id="submit" onclick="<?php $_SERVER['PHP_SELF']; ?>" /> </form> </body>
/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }
#include <stdio.h> #include<conio.h> void main () { int a,r,n,sum=0; clrscr(); printf("enter the number"); scanf("%d",& n); c=n while (n!=0) { a=n%10; n=n/10; sum=sum*a+a } printf("the reverse is %d \n",sum); if(c==sum) printf("\n the given number is palindrome"); else printf("\n the given number is not a palindrome"); getch(); }
The generation of palindromic numbers within a given range is best done with a computer program. Space it limited so an example of program code cannot be shown here, but the Codecast website gives full guidance.
#include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; } #include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; }
This program only suits PHP. If you want a proper one try C program for it available on web <body> <?php if(isset($_POST['submit'])) { $text = $_POST['text']; $string = mysql_real_escape_string($text); $invert = strrev($string); if($string == $invert) { echo "<br>Given number is a palindrome!!"; } else { echo "<br>Given number is not a palindrome!!"; } } ?> <form method="post"> <input type="text" name="text" id="text" /> <input type="submit" name="submit" value="Submit" id="submit" onclick="<?php $_SERVER['PHP_SELF']; ?>" /> </form> </body>
/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }
#include <stdio.h> #include<conio.h> void main () { int a,r,n,sum=0; clrscr(); printf("enter the number"); scanf("%d",& n); c=n while (n!=0) { a=n%10; n=n/10; sum=sum*a+a } printf("the reverse is %d \n",sum); if(c==sum) printf("\n the given number is palindrome"); else printf("\n the given number is not a palindrome"); getch(); }
You write the number and then follow it with the digits in reverse order.
8086 assembly language program to check wether given number is perfect or not
The generation of palindromic numbers within a given range is best done with a computer program. Space it limited so an example of program code cannot be shown here, but the Codecast website gives full guidance.
write a program in C to check whether a given number is apalindrome or not
#include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; } #include <stdio.h> #include <string.h> #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; }
Reverse the string and compare it to the original. If they match, then it is a palindrome.
45
The program is here guys.......... //Finding whether the given number is perfect or not //Starts here #include<stdio.h> void main() { int i=1,temp=0,number; scanf("%d",&number); while(i<=number/2){ if(number%i==0) temp+=i; i++; } if(temp==number) printf("Its a perfect number"); else printf("Its not a perfect number"); } //ends here
civic