Anda di halaman 1dari 11

1. Palindrome Number #include<stdio.

h> int main(){ int num,r,sum=0,temp; printf("\nEnter a number:"); scanf("%d",&num); temp=num; while(num){ r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf("\n%d is a palindrome",temp); else printf("\n%d is not a palindrome",temp); return 0; } Definition of Palindrome number:

A number is called palindrome number if it is remain same when its digits are reversed. For example 121 is palindrome number. When we will reverse its digit it will remain same number i.e. 121 Examples of palindrome number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191 etc. The simplest method is to reverse the digits of the original number and check to see if the reversed number is equal to the original. This is not an efficient way of doing it but, for ints or long ints, it works well enough. void main() { // get the number to check long int n;

printf("ENTER A NUMBER: "); scanf("%ld", &n); long int n1,mod; // make a copy of the original number in n1 n1=n; // store the reversal in rev long int rev=0; while(n>0) { // grab the rightmost digit of n mod = n%10; // shift all digits of rev left, then add in the new digit rev = rev * 10 + mod; // shift all digits of n to the right (removing the rightmost one) n = n / 10; } // after the loop, rev will have the opposite digit ordering of n1; // if these two values are equal, then we have a palindrome if (n1 == rev) printf ("%ld is a palindrome\n",n1); else printf("%ld is not a palindrome\n",n1); } getch();

Alternate method: Split the number into its digits, as on the example below: Number: 3592 1st digit: 3592%10 = 2 3592/10=359. ... 2nd digit: 359%10 = 9 etc. Store these into an integer array, then check if all digits correspond to digits at the reflected position.

#include<stdio.h> #include<conio.h> #include<string.h> main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a palindrome.\n"); getch(); return 0; }

Palindrome with Library function #include<stdio.h> int is_palindrome(char*); void copy_string(char*, char*); void reverse_string(char*); int string_length(char*); int compare_string(char*, char*); main() {

char string[100]; int result; printf("Enter a string\n"); gets(string); result = is_palindrome(string); if ( result == 1 ) printf("\"%s\" is a palindrome string.\n", string); else printf("\"%s\" is not a palindrome string.\n", string); return 0; } int is_palindrome(char *string) { int check, length; char *reverse; length = string_length(string); reverse = (char*)malloc(length+1); copy_string(reverse, string); reverse_string(reverse); check = compare_string(string, reverse); free(reverse); if ( check == 0 ) return 1; else return 0; }

int string_length(char *string) { int length = 0; while(*string) { length++; string++; } return length; } void copy_string(char *target, char *source) { while(*source) { *target = *source; source++; target++; } *target = '\0'; } void reverse_string(char *string) { int length, c; char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++;

for ( c = 0 ; c < length/2 ; c++ ) { temp = *end; *end = *begin; *begin = temp; begin++; end--; } } int compare_string(char *first, char *second) { while(*first) { if (*first == *second) { first++; second++; } else return -1; } return 0; } Write a C program to check whether the string is palindrome or not?

A:">A: Palindrome with integer Array ..

#include<stdio.h> #include<conio.h> void main()

{ int i=0,n[5],s=0,m[5],r=0; clrscr(); for(i=0; i<5; i++) { printf("Enter Any number:"); scanf("%d",&n[i]); m[i]=n[i]; } for(i=0; i<5; i++) { while(n[i]!=0) { r=n[i]%10; s=(s*10)+r; n[i]=n[i]/10; } if(m[i]==s) { printf("\nPalindrome :"); } else { printf("\nNot palindrome :"); } printf("%d",s); s=0,r=0; } getch(); }

B:">B:

#include <stdio.h> #include <stdlib.h> int main(int argc,char *argv[]) { int strLen=0,indx; if(argv[1]==NULL){ return(0); } while((argv[1][strLen])!= '\0') { strLen++; } for(indx = 0;indx < strLen/2;indx++) { if(argv[1][indx] != argv[1][strLen-1-indx]) { printf("%s Not palindrome\n",argv[1]); return(0); } } printf("%s Palindrome",argv[1]); return 0; }

C:">C:

#include<stdio.h> #include<string.h> #define size 26

void main()

{ char strsrc[size]; char strtmp[size]; clrscr(); printf("\n Enter String:= "); gets(strsrc); strcpy(strtmp,strupr(strsrc)); strrev(strtmp); if(strcmp(strsrc,strtmp)==0) printf("\n Entered string "%s" is palindrome",strsrc); else printf("\n Entered string "%s" is not palindrome",strsrc); getch(); }

#include <stdio.h> int main() { int number,temp,remainder,sum=0; printf("\n\nEnter no: "); scanf("%d",&number); temp = number; //Value of temp stores unmodified n value while(number>0) { remainder=number%10; //Gets Last Digit number/=10; //Truncates Last Digit

sum=sum*10 + remainder; //Builds value of reversed number } if (sum==temp)

printf ("\nThe Number Is A Palindrome "); else printf ("\nThe Number Is Not A Palindrome "); getch (); return 0; }

2. Armstrong Number #include<stdio.h> #include<conio.h> main() { int r; long number = 0, c, sum = 0, temp; printf("Enter the maximum range upto which you want to find armstrong numbers "); scanf("%ld",&number); printf("Following Armstrong numbers are found from 1 to %ld\n",number); for( c = 1 ; c <= number ; c++ ) { temp = c; while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; } if ( c == sum ) printf("%ld\n", c); sum = 0;

} getch(); return 0; }

Anda mungkin juga menyukai