Bubble Sort Program In C Using Function

Bubble Sort Program In C Using Function. If you are looking for a bubble sort program in C with function example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming tutorial to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using function.

Bubble Sort in C

Table of Contents

What is Bubble Sort?

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Bubble Sort Example

This below image illustrates what is Bubble Sort in C.

Bubble Sort Program in C
Bubble Sort Program in C

C Programming Tutorials

  1. Bubble Sort Program in C Using Array
  2. Stack Push Pop Program in C Using Arrays
  3. Factorial Program in C Using Pointers
  4. Factorial Program in C Using While Loop
  5. C Program For Factorial Using For Loop
  6. Factorial Program in C Using Recursion Function

Bubble Sort in C with Function

Learn how to write a  bubble sort in c using function. Writing a Bubble Sort C Program can be done using various techniques like an array, pointers, recursion, function but here in this program, we show how to write a bubble sort program in c using function in a proper way.

Bubble Sort Program in C Using Function – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own Bubble Sort in C Using Function with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING FUNCTION - BUBBLESORT.C */

#include<stdio.h>
#include<stdlib.h>
void bubblesort(int a[],int size);
void main()
{
  int a[50],n,i;
  printf("\n Enter the size of the array");
  scanf("%d",&n);
  if(n>50)
  {
    printf("\n error");
    exit(0);
  }

 printf("\n Enter the array elements: \n");

 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
  bubblesort(a,n);
  printf("\n The sorted array is\n");
  for(i=0;i<n;i++)
  printf("%d\t",a[i]);
}
void bubblesort(int a[],int size)
{
  int temp,i,j;
  for(i=0;i<size;i++)
  {
   for(j=0;j<size-1;j++)
   {
    if(a[j]>a[j+1])
    {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }
  }
 }
}

C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using function, your C compiler asks you to enter elements for bubble sort. After you enter elements, the program will be executed and give output like below expected output using bubble sort functionality.

Output:

Enter the size of the array: 5

Enter the array elements: 29541

The sorted array is 12459

C PROGRAMMING EXAMPLES

Bubble Sort Program In C Using Array With Example

Bubble Sort Program In C Using Array – If you are looking for a bubble sort program in C with array example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming example to learn about bubble sort, we are sure that you will be able to write an C program for bubble sort using array.

Bubble Sort in C

Table of Contents

What is Bubble Sort?

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Bubble Sort Example

This below image illustrates what is bubble sort.

Bubble Sort Program in C
Bubble Sort Program in C

C Programming Tutorials

  1. Stack Push Pop Program in C Using Arrays
  2. Factorial Program in C Using Pointers
  3. Factorial Program in C Using While Loop
  4. C Program For Factorial Using For Loop
  5. Factorial Program in C Using Recursion Function
  6. C Program For Palindrome String
  7. C Program For Palindrome Numbers
  8. C Program To Reverse a String with Using Function
  9. C Program To Reverse a String without Using Function
  10. C Program To Reverse a String Using Recursion

Bubble Sort in C with Array

Learn how to write a  bubble sort in c using array. Writing a c program for bubble sort can be done using various techniques like array, pointers, recursion, function but here in this program, we show how to write a bubble sort program in c using array in a proper way.

Bubble Sort Program in C – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own program on bubble sort using this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING ARRAY - BUBBLESORT.C */

#include<stdio.h>

//program execution starts from here. 

int main()

{
    int a[50],n,i,j,temp; //Variable declaration

    printf("Enter the size of array: ");

    scanf("%d",&n); //Taking size of the array from user

    printf("Enter the array elements: "); 

    for(i=0;i<n;++i)

        scanf("%d",&a[i]); //Taking array elements from user
   
    for(i=1;i<n;++i)

        for(j=0;j<(n-i);++j)

            if(a[j]>a[j+1]) //Bubble sort in c using array logic

            {

                temp=a[j];

                a[j]=a[j+1];

                a[j+1]=temp;

            }            

    printf("\nArray after sorting: ");

    for(i=0;i<n;++i)

        printf("%d ",a[i]); //Printing elements after bubble sort

    return 0;

}

C Program for Bubble Sort – Output

After you compile and run the above bubble sort program in c using array, your C compiler asks you to enter array size and array elements for bubble sort. After you enter elements, the program will be executed and give output like below expected output using bubble sort functionality.

Output:

Enter the size of array: 5
Enter the array elements: 29541

Array after sorting: 12459

C PROGRAMMING EXAMPLES

Stack Push Pop Program In C Using Arrays With Example

Stack push pop program in c using arrays with an example. If you are looking for a stack push pop program in c, this C programming tutorial will help you to learn how to code stack program in c using arrays. Just go through this C programming example, you will be able to write a C program to push and pop.

Stack Push Pop Program in C

Table of Contents

Stack Push Pop Program in C using Arrays

Learn how to write a  stack program in C using arrays. Writing a stack push pop program in C can be done using various techniques but here in this program, we show how to write a C program to push and pop using arrays in a proper way.

C Programming Tutorials

  1. Factorial Program in C Using Pointers
  2. Factorial Program in C Using While Loop
  3. C Program For Factorial Using For Loop
  4. Factorial Program in C Using Recursion Function
  5. C Program For Palindrome String
  6. C Program For Palindrome Numbers
  7. C Program To Reverse a String with Using Function
  8. C Program To Reverse a String without Using Function
  9. C Program To Reverse a String Using Recursion

Push Function Creation

/*STACK PUSH() AND POP() IMPLEMENTATION USING ARRAYS*/

#include <stdio.h>

#include<conio.h>

#define MAX 5

int top, status;

 

/*PUSH FUNCTION*/

void push (int stack[], int item)

{   if (top == (MAX-1))

status = 0;

    else

    {   status = 1;

++top;

stack [top] = item;

    }

}

Pop Function Creation

/*POP FUNCTION*/

int pop (int stack[])

{  

int ret;

    if (top == -1)

    {   ret = 0;

status = 0;

    }

    else

    {   status = 1;

ret = stack [top];

--top;

    }

return ret;

}

Function To Display Stack 

/*FUNCTION TO DISPLAY STACK*/

void display (int stack[])

{   int i;

    printf ("\nThe Stack is: ");

    if (top == -1)

printf ("empty");

    else

    {   for (i=top; i>=0; --i)

  printf ("\n--------\n|%3d   |\n--------",stack[i]);

    }

    printf ("\n");

}

Stack Push Pop Program in C Source Code

/*MAIN PROGRAM*/

void main()

{  

int stack [MAX], item;

    int ch;

    clrscr ();

    top = -1;

 

    do

    {  do

       {   printf ("\NMAIN MENU");

  printf ("\n1.PUSH (Insert) in the Stack");

  printf ("\n2.POP  (Delete) from the Stack");

  printf ("\n3.Exit (End the Execution)");

  printf ("\nEnter Your Choice: ");

  scanf  ("%d", &ch);

  if (ch<1 || ch>3)

      printf ("\nInvalid Choice, Please try again");

}while (ch<1 || ch>3);

       switch (ch)

       {case 1:

printf ("\nEnter the Element to be pushed : ");

scanf  ("%d", &item);

printf (" %d", item);

push (stack, item);

if (status)

{   printf ("\nAfter Pushing ");

   display (stack);

   if (top == (MAX-1))

printf ("\nThe Stack is Full");

}

else

   printf ("\nStack overflow on Push");

break;

       case 2:

item = pop (stack);

if (status)

{    printf ("\nThe Popped item is %d.  After Popping: ");

    display (stack);

}

else

    printf ("\nStack underflow on Pop");

break;

       default:

printf ("\nEND OF EXECUTION");

       }

    }while (ch != 3);

getch();

}

C PROGRAMMING EXAMPLES

Factorial Program In C Using Pointers With Example

Factorial Program In C Using Pointers With Example. If you are looking for a factorial program in C with pointers example, this C programming tutorial will help you to learn how to find the factorial of a number. Just go through this C programming example to calculate the factorial of a positive integer, you will be able to write an C program for factorial using pointers.

Factorial Program in C Using Pointers

Table of Contents

Factorial Program in C with Pointers

 Learn how to write a  C program for factorial using pointers. Writing a factorial program in C can be done using various techniques like using for loop, while loop, pointers, recursion function but here in this program, we show how to write a factorial program using pointers in a proper way.

Factorial Program in C using Pointers Source Code

Copy the below program to find the factorial of a number using pointers or write your own logic by using this program as a reference. Paste the factorial program into C compilers and run the program to see the result.

/* C PROGRAM FOR FACTORIAL USING POINTERS - FACTORIAL.C */

#include<stdio.h>
 
void findFactorial(int,int *); //function
int main(){
 int i,factorial,n;
 
 printf("Enter a number: ");
 scanf("%d",&n);
 
 findFactorial(n,&factorial);
 printf("Factorial of %d is: %d",n,*factorial);
 
 return 0;
}
 
void findFactorial(int n,int *factorial){
 int i;
 
 *factorial =1;
 
 for(i=1;i<=n;i++)
 *factorial=*factorial*i;
}

Factorial Program in C using Pointers Output

After you compile and run the above factorial program in c to find the factorial of a number using pointers, your C compiler asks you to enter a number to find factorial. After you enter your number, the program will be executed and give output like below expected output.

Enter a number: 7
Factorial of 7 is: 5040

C PROGRAMMING EXAMPLES

Factorial Program In C Using While Loop With Example

Factorial Program In C Using While Loop With Example. If you are looking for a factorial program in C with while loop example, this C programming tutorial will help you to learn how to find the factorial of a number. Just go through this C program to calculate factorial of a number, you will be able to write a factorial C program using while loop.

Factorial Program in C – Table of Contents

 Factorial Program in C Using While Loop

 Learn how to write a  C program for factorial. Writing a factorial program in C to find factorial can be done using various techniques like using for loop, while loop, pointers, recursion function but here in this program, we show how to write a factorial program using while loop in a proper way.

Factorial Program in C Using While Loop Source Code

Copy the below program to find the factorial of a number using a while loop or write your own logic by using this program as a reference. Paste the factorial program into C compilers and run the program to see the result.
/* C PROGRAM FOR FACTORIAL USING WHILE LOOP - FACTORIAL.C */

#include<stdio.h>
#include<conio.h>

void main() //main method

{
   //variable declaration
   long int calcfact =1;
   int i=1,n;

   clrscr();

   printf("Enter a a positive number : "); //asking user to enter a num
   scanf("%d",&n); //reading entered number by user

   while(i<=n) //while loop logic for factorial

   {

     calcfact*=i; //calcfact = calcfact*i;

     i++;

   }

printf("factorial of %d is = %ld  \n", n, calcfact); 

   getch();

}

FACTORIAL PROGRAM IN C USING While Loop OUTPUT

After you compile and run the above factorial program in c to find the factorial of a number using while loop, your C compiler asks you to enter a number to find factorial. After you enter your number, the program will be executed and give output like below expected output.

Output – 1

Enter a positive number: 6
Factorial of 6 is = 720

FACTORIAL Program in C using Recursion with Explanation
  • The program execution starts from main() function.
  • Define three variable n, i, calcfact.
  • Asks the user to enter a positive number to find factorial using printf().
  • Gets the value from the user using scanf() function.
  • Let’s take a variable ‘i’ with an initial value of 1 to compare with user entered value ‘n’.
  • Now while loop will be executed until the statement while(i<=n) is false.
  • In each iteration in while loop, ‘i’ value will be increased (i++) and checked with ‘n’ (i<=n).
  • The ‘i’ value is multiplied with ‘calcfact’ variable and the new value will be stored in ‘calcfact’ variable. calcfact*=i; or calcfact = calcfact*i;
  • When while statement gets false, the compiler gets out of the while loop and prints the factorial of the entered number.
  • 1*2*3*4*5*6 = 720

C PROGRAMMING EXAMPLES

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

Factorial Program In C Using Recursion Function With Explanation

Factorial Program In C Using Recursion Function With Explanation. If you are looking for a factorial program in C with recursion function example, this C programming tutorial will help you to learn how to find the factorial of a number. Just go through this C program to calculate factorial of a number, you will be able to write a factorial C program using recursion function.

Factorial Program in C – Table of Contents

What is Factorial in Mathematics?

In mathematics, Factorial is the product of a positive number (n) and all the numbers below of it (n-1). Example, the factorial of positive number n is ( n! ) is equal to 1*2*3*…*n.

Example: What is the value of 5!

5! = 1*2*3*4*5 => 120

The value of 5! is 120.

Factorial Program in C

Learn how to write a  C program for factorial. Writing a C program to find factorial can be done using various techniques like using for loop, while loop, pointers, recursion but here in this program, we show how to write a factorial program using for loop in a proper way.

Factorial Program in C using Recursion

This factorial program in c using recursion function is the 12th C programming example in the series, it helps newbies who started coding, programming students and B.Tech graduates in enhancing their C programming skills and get a job in software industry.

All the best guys in learning c programs with coding compiler website. We wish all the success in your career. Happy Learning.

C Programming Tutorials

  1. C Program For Factorial Using For Loop
  2. C Program For Palindrome String
  3. C Program For Palindrome Numbers
  4. C Program To Reverse a String with Using Function
  5. C Program To Reverse a String without Using Function
  6. C Program To Reverse a String Using Recursion

Factorial Program in C using Recursion Function

Copy the below source code to find the factorial of a number using recursive function program or write your own logic by using this program as a reference. Paste the factorial program into C compilers and run the program to see the result.

/* C PROGRAM FOR FACTORIAL USING RECURSION FUNCTION - FACTORIAL.C */

#include <stdio.h>
long int multiplyNumbers(int n);

int main()
{
 int n; //variable declaration
 
 printf("Enter a positive number: "); //asking user to enter number
 
 scanf("%d", &n); //reading number from user
 
 printf("Factorial of %d = %ld", n, multiplyNumbers(n));
 return 0;
}

//Recursive function for finding factorial

long int multiplyNumbers(int n)
{
 if (n >= 1)
 return n*multiplyNumbers(n-1);
 else
 return 1;
}

FACTORIAL program in c using recursion function OUTPUT

After you compile and run the above factorial program in c to find the factorial of a number using a recursive function, your C compiler asks you to enter a number to find factorial. After you enter your number, the program will be executed and give output like below expected output.

Output – 1

Enter a positive number: 5
Factorial of 5 = 120

FACTORIAL Program in C using Recursion with Explanation

In the above output user entered number 5 to find the factorial. Program execution will start from the beginning of the main() function. The main function consists of multiplyNumbers() recursive function, this multiplyNumbers() function is called from main() function with user entered number 5 as an argument.

After passing number 5 to the multiplyNumbers() function will call multiplyNumbers() function (recursive call). In recursive call, the value of that passed argument ‘n’ is decreased by 1 until n value reaches less than 1.

Once n value is less than one, there is no recursive call and the factorial program will calculate and print output. Factorial of 5 as 120.

C PROGRAMMING EXAMPLES

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

C Program For Factorial Of A Number Using For Loop

C Program For Factorial Of A Number Using For Loop.  If you are looking for a factorial program in C with an example, this factorial tutorial will help you to learn how to find the factorial of a number in C language. Just go through this C programming example for factorial, you will be able to write a C program for factorial using for loop.

C Program For Factorial – Table of Contents

What is Factorial?

Factorial is the product of a positive integer (n) and all the integers below of it (n-1). Example, the factorial of positive integer n is ( n! ) is equal to 1*2*3*…*n.

Example: What is the value of 4!

4! = 1*2*3*4 => 24

The value of 4! is 24.

C Program For Factorial

Learn how to write a  C program for factorial. Writing a C program to find factorial can be done using various techniques like using for loop, while loop, pointers, recursion but here in this program, we show how to write a factorial program using for loop in a proper way.

This is the eleventh C programming example in the series, it helps newbies coders, programming students and B.Tech graduates in enhancing their C programming skills and land on their dream job in software industry. All the best guys in learning c programs with coding compiler website. We wish all the success in your career. Happy Learning.

C Programming Tutorials

  1. C Program For Palindrome String
  2. C Program For Palindrome Numbers
  3. C Program To Reverse a String with Using Function
  4. C Program To Reverse a String without Using Function
  5. C Program To Reverse a String Using Recursion

C Program For Factorial Source Code

Copy the below C program to find the factorial of a number source code or write your own logic by using this program as a reference. Paste the factorial program into C compilers and run the program to see the result.

/* C PROGRAM FOR FACTORIAL - FACTORIAL.C */

#include <stdio.h>
int main()
{
 int n, i,factorial = 1; //varialbes declaration

printf("Enter a number: "); //asking your to enter a number
 scanf("%d",&n); //reading entered number

// showing error message, if the entered number is a negative number
 if (n < 0)
 printf("Error! Factorial of a negative number doesn't exist.");

else
 {
   for(i=1; i<=n; ++i) //finding factorial using for loop
   {
      factorial=factorial*i; // calculating factorial of a number
   }
   printf("Factorial of %d = %d", n, factorial);
 }

return 0;
}

C Program For Factorial Output

After you compile and run the above c program for factorial of a number using for loop, your C compiler asks you to enter a number to find factorial. After you enter your number, the program will be executed and give output like below expected output.

Output – 1

Enter a number: 4
Factorial of 4 = 24

Output – 2

Enter a number: -4
Error! Factorial of a negative number doesn’t exist.

C Programming Examples

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

Switch Case Program In C With Example Output

Switch Case Program In C With Example – If you are looking for a switch case program in C with an example, this switch case statement tutorial will help you to learn how to write switch case program in C language. Just go through this C program for switch case statement. After going through this C programming tutorial, you will be able to write a switch case program in C.

Switch Case Program in C – Table of Contents

Switch Case Statement

What is switch case statement? A switch case statement is used to test a variable against a list of values. In switch case, each value is called as a case, and the entered variable is called switch. The switched variable is checked against each case value in switch case statement.

Switch Case Statement Syntax in C Programming

Here, you will get Switch Case Syntax in C Programming. This below syntax illustrates how to use switch case statement in C programming to perform arithmetic operations.

//Switch Case Statement Example Syntax in C Programming

switch (n)
​{
 case constant1:
 // logic will be executed if n is equal to constant1;
 break;

case constant2:
 // logic will be executed if n is equal to constant2;
 break;
 .
 .
 .
 default:
 // logic will be executed if n doesn't match any constant
}

C Programming Tutorials

  1. C Program For Palindrome String
  2. C Program For Palindrome Numbers
  3. C Program To Reverse a String with Using Function
  4. C Program To Reverse a String without Using Function
  5. C Program To Reverse a String Using Recursion

Switch Case Program in C

Learn how to write a  Switch case program in C language. Writing a C program for switch case statement can be done using various techniques, but here in this program, we show how to write switch case program in a proper way.

This is the tenth C programming example in the series, it helps newbies, students and B.Tech graduates in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website. Happy Learning.

Switch Case Program in C Source Code

Copy paste the below switch case program source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM FOR SWITCH CASE STATEMENT - SWITCHCASE.C */

#include<stdio.h>
#include<conio.h>
void main()
{
    //variable declaration
    int n1, n2, ch;
    clrscr();
   
    //Asking user to enter a number 
    printf("Enter the first number : ") ;

    //Taking input from user
    scanf("%d", &n1) ;

    //Asking user to enter secpmd number 
    printf("\nEnter the second number : ") ;

    //Taking input from user
    scanf("%d", &n2) ;
    printf("\n[1] -> Addition ") ;
    printf("\n[2] -> Subtraction ") ;
    printf("\n[3] -> Multiplication ") ;
    printf("\n[4] -> Division ") ;

    //Asking your to enter their choice to perform arithmetic operation
    printf("\n\nEnter your choice <1...4> : ") ;
    scanf("%d", &ch) ;

    //Checking user choice against different options in switch case
    switch(ch)
    {
        case 1 :
        printf("\n%d + %d = %d", n1, n2, n1 + n2) ;
        break ;
        case 2 :
        printf("\n%d - %d = %d", n1, n2, n1 - n2) ;
        break ;
        case 3 :
        printf("\n%d * %d = %d", n1, n2, n1 * n2);
        break ;
        case 4 :
        printf("\n%d / %d = %.2f", n1, n2, (float)n1 / n2);
        break ;
        default :
        printf("\nInvalid choice");
        break ;
    }
    getch();
}

Switch Case Program in C OutPut

After you compile and run the above switch case program in C, your C compiler asks you to enter a choice to perform some action using your choice. After you enter your choice, the program will be executed and give output like below example output.

Output – 1

Enter the first number : 11
Enter the second number : 22
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1…4> : 1
11 + 22 = 33

Output – 2

Enter the first number : 15
Enter the second number : 5
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1…4> : 3
15 + 5 = 75

C Programming Examples

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

C Program For Palindrome – Check Whether A String Is Palindrome Or Not

C Program For Palindrome String – What is Palindrome? A palindrome is a sequence of characters which reads the same backward as forward. A palindrome could be a word, phrase, number, or other sequences of characters. such as madam or racecar.

 

Palindrome Word Examples:

Madam, Racecar, Radar, Etc.

C Program For Palindrome

If you are looking for a palindrome string in C program, this article will help you to learn how to write palindrome program in C language. Just go through this C program for palindrome to check whether a string is palindrome or not. After going through this C programming tutorial, you will be able to write a  C palindrome program.

C Programming Tutorials

C Program For Palindrome Numbers

C Program To Reverse a String with Using Function

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Recursion

C Program To Reverse a String Using Pointers

C Program For Palindrome String

Learn how to write a  Palindrome program in C language. Writing a C program to check whether a string is a palindrome or not can be done using various techniques, but here in this program, we show how to write palindrome program in a proper way.

This is the eighth C programming example in the series, it helps newbies, students and B.Tech graduates in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website. Happy Learning.

C Program For Palindrome Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

#include <stdio.h> 

#include <string.h> 

int main()

{ 

//Variables declaration

   char string1[20]; 

   int i, length; 

   int flag = 0; 

//Reading a string from user

   printf("Enter a string:"); 

   scanf("%s", string1); 

   length = strlen(string1); 

//checking whether a string is palindrome or not

   for(i=0;i < length ;i++)

   { 

      if(string1[i] != string1[length-i-1])

      { flag = 1; break; } 

   } 

   if (flag) 

   {

      printf("%s is not a palindrome", string1); 

   } 

   else 

  { 

     printf("%s is a palindrome", string1); 

  }

   return 0; 

}

C PROGRAM FOR PALINDROME OUTPUT

After you compile and run the above program, your C compiler asks you to enter a string to check whether entered string is palindrome or not. After you enter a string, the programming logic will check and show result like below expected output.

Example-1

Enter a string:

Radar

Radar is a palindrome

Example-2

Enter a string:

King

King is not a palindrome

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a Sting Using Recursion

C Program For Palindrome – Check Whether A Number Is Palindrome Or Not

C Program For Palindrome – What is Palindrome? A palindrome is a sequence of characters which reads the same backward as forward. A palindrome could be a word, phrase, number, or other sequences of characters. such as madam or racecar.

Palindrome Number Examples:

1001, 2552, 32423, 959, Etc.

Palindrome Word Examples:

Madam, Racecar, Etc.

C Program For Palindrome

If you are looking for a palindrome in C program, this article will help you to learn how to write palindrome program in C language. Just go through this C program for palindrome to check whether a number is palindrome or not. After going through this C programming tutorial, you will be able to write a  C palindrome program.

C Programming Tutorials

C Program For Palindrome String

C Program To Reverse a String with Using Function

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Recursion

C Program To Reverse a String Using Pointers

C Program For Palindrome Numbers

Learn how to write a  Palindrome program in C language. Writing a C program to check whether a number is a palindrome or not can be done using various techniques, but here in this program, we show how to write palindrome program in a proper way.

This is the seventh C programming example in the series, it helps newbies, students and B.Tech graduates in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website. Happy Learning.

C Program For Palindrome Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM FOR PALINDROME - PALINDROME.C */

#include <stdio.h>
int main()
{

//Variables declaration

 int n, reversedInteger = 0, remainder, originalInteger;

//Taking input number from user

printf("Enter a number: ");
 scanf("%d", &n);

originalInteger = n;

// reversed integer is stored in "reversedInteger" variable
 
 while( n!=0 )
 {
 remainder = n%10;
 reversedInteger = reversedInteger*10 + remainder;
 n /= 10;
 }

// palindrome logic
// if orignalInteger and reversedInteger are equal
// then it is a palindrome

 if (originalInteger == reversedInteger)
 printf(" Entered Number %d is a Palindrome.", originalInteger);
 else
 printf(" Entered Number %d is Not a Palindrome.", originalInteger);
 
 return 0;
}

C PROGRAM For Palindrome OUTPUT

After you compile and run the above program, your C compiler asks you to enter a number to check whether a number is palindrome or not. After you enter a number, the programming logic will check and show result like below expected output.

Enter a number:

1001

Entered Number 1001 is a Palindrome.

 

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a Sting Using Recursion