6.Write a program in c language to check if a given number is a palindrome or not using a do while loop.
Introduction
A palindrome is the reverse of a particular object that gives the same result as the original. The object can be anything, a number, a string, an expression, etc. In this tutorial we want to see what a palindrome is in C language, how it works in a C program and how to recognize a certain number or string is palindrome. As we said before, palindrome is the result obtained after the reversing.
For example, If you reverse a certain number 121, the result will be the same i.e. 121. So we can say that 121 is palindrome.
Real Life Applications
Data validation
Palindrome or Alternating numbers can usually be used in data validation procedures to ensure the validity and accuracy of data entered or transmitted. For example, the system can use palindrome to verify that a sequence of numbers or letters has been entered correctly.
Encryption
It can be used in cryptographic algorithms as keys for encryption and decryption operations. The consistency and uniqueness of palindrome numbers can improve the security of encrypted data.
Error Detection
Palindromic numbers can be used in error detection mechanisms to detect errors in data transmission or storage. By comparing a number with its counterpart, inconsistencies can be identified and corrected.
Number Theory
In number theory the palindrome numbers are used to study the relationship with mathematical concepts and things of symmetric numbers.
Examples
Let us study the following examples to check whether a numbers are palindrome are not
1. 123421 – Reverse is 124321 so it is not a palindrome.
2. abdba – Reverse of abdba is abdba so it is a palindrome.
3. 123421 – Reverse is 124321 so it is not a palindrome.
4. acbac – Reverse of acbac is cabca, so it is not a palindrome.
From the above example, we can now determine whether given pattern is a palindrome or not. This is what we have done in theory or just words.
Now we want to see how we can check a palindrome in a C program, before moving on to that, we will first look at the logic of palindromes in C.
A Palindrome Logic in C program:
1. Ask the user to enter some input.
2. Store the entered input data in a temporary variable.
3. Find the reverse of the user input.
4. Compare the reverse value of the input data with a temporary variable.
5. If the reverse and temporary variables are the same, print that is a palindrome.
6. print not palindrome If the reverse and temporary variables do not match.
Working of do While Loop
A while loop is executed repeatedly based on the condition specified after the word while in the code.
the body of the loop executes and check the condition ,If this condition is true, the code within the parentheses will execute the do while loop. If the condition is false, the for loop jumps to the code after the loop without executing the code. Let's see how to check if a number is palindrome using a loop.
#include<stdio.h>
int main()
{
int n, rev_number = 0, temp;
printf("Enter a number to check palindrome value:\n");
// allow user to enter a number
scanf("%d", &n); // takes value from user
temp = n; //store number n in to temp variable
do{
rev_number=rev_number*10;
rev_number=rev_number+n%10;
n=n/10;
}while (n!= 0);
//check reverse number with original number
if(temp==rev_number)
{
//if match original given number then show palindrome
printf("\nThe Given number %d is palindrome",temp);
}
else
{
// If it does not match with original number show not palindrome
printf("\nGiven number %d is not palindrome",temp);
}
return 0;
}
Output
Enter a number to check palindrome value
121
The Given number 121 is palindrome
Previous :-->> 5.Write a program in c to display the Fibonacci series up to a given number using a while loop.
-->> NEXT:
7.Write a program to in c programming to calculate the power of a number using a for loop and while loop.
-->>ALL
Loops Assignments in c