×

Palindrome Number in C

What is a Palindrome?

A number that doesn't change when reversed is known as a palindrome.

We reverse a number and compare it to the original number to determine whether it is a palindrome or not. It is a palindrome if both are the same; otherwise, it is not.

For example: abcba, 121, MALYALAM, 45454 etc.

These examples show that the number will remain the same whether we read them forward or backward. Therefore, this kind of number is a palindrome.

Logical Approach:

  • Get input from the user through the keyboard.
  • Store this input in any random variable.
  • Find the reverse of the input which is entered by the user.
  • Compare the reverse of input with the random variable.
  • If the reverse and random variables match, the input will be a palindrome.
  • If the reverse and random variables do not match, then the input number will not be a palindrome. 

Program to check whether the number is palindrome or not.

Using For loop:

#include<stdio.h>


int main()
{
    int number,i,remainder,random_variable,reversible_no=0;
   
     
    printf("\n Enter The Number:");
    scanf("%d",&number);
    random_variable = number;
     
    //For Loop to find the reversible number
    for(i=number;i>0; )
    {
        remainder=i%10;
       
reversible_no=reversible_no*10+remainder;
        i=i/10;
    }
     
// if else conditional statement to check whether the reversible number and input number are the same or not
    if(reversible_no==number)
    {
        printf("\n %d is a Palindrome Number", random_variable);
    }
    else
    {
        printf("\n %d is not a Palindrome Number", random_variable);
    }
 return 0;
}

Explanation:

  • Here we have declared five variables “number”, “i”, “remainder”, “random_variable”, and “reversible_no”.
  • We stored the “number” variable in the “random_variable”.
  • We have applied for loop to find the reverse of input.
  • In for loop “i” variable is assigned to the “number” variable.
  • Given the condition (i>0) in a for loop, the body of the loop will be executed up to the point where “i” is greater than 0. Following that, the loop will be broken, and control will shift outside of it.
  • In for loop curly braces, we have three action statements. The first statement gives us the “remainder” variable value. The second statement gives us the “reversible_no” variable value. And the last statement gives us a new ”i” value.
  • These Three statements will be executed until the condition is false.
  • Then we use the if-else conditional statement to check whether the entered number and the reversible number are the same or not.
  • If they both are the same, then the input number is a palindrome number.
  • If they are not identical, then the input number is not a palindrome number.

Program Output:

When the condition is satisfied

Palindrome Number in C

When the condition is not satisfied

Palindrome Number in C

C program to determine whether a string is a palindrome or not.

  • Palindrome of string denotes that if we read a sting from right to left or left to right, we get similar output.
  • So, if we want to check whether a given or input string is palindrome or not, we do a comparison between characters of a string.
  • If the string has 5 characters, so in that case, we will compare the first and last character of a string and then the second and second last character of a string; if they are the same, then this string will be a palindrome string.

Program:

#include <stdio.h>
#include <string.h>
int main()
{
char str[20];
int i, string_length, string_flag = 0;
//Give input from the user through the keyboard
printf("For a palindrome check, enter a string: "); 
scanf("%s", str); 
// count the length of the string
string_length = strlen(str); 
for(i = 0; i < string_length; i++)
{
if(str[i] != str[string_length-i-1])
{
string_flag = 1;
break;
}
}
if(string_flag==0)
{
printf("The provided string is a palindrome.");
}
else {
printf("The provided string is not a palindrome.");
}
return 0;
}

Explanation:

  • In above code, three variables with the int data type are present here: “i”, "string length," and "string flag." Additionally, we have a variable called "str" with the data type char.
  • To check a string for palindrome, we used a for loop to compare the first character with the final character and the second character with the second last character. We repeated this process until the middle character of the string was reached.
  • If any comparison results in a mismatch, end the for loop and set the " string_flag" value to 1.
  • If we determine that "string _flag" is equal to 0, using the if-else conditional expression, the supplied string will be a palindrome string.
  • However, the supplied string won't be a palindrome if "string_flag" is not equal to 0.

Program Output:

When the condition is satisfied

Palindrome Number in C

When the condition is not satisfied

Palindrome Number in C

Related Topics

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array. To determine the Mean: To get the average, mean is determined. The formula...

2 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.