×

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

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

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.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.