C program to check whether a given 5 digit number is palindrome or not

Check for Palindromic number. C program to check whether a given 5 digit number is palindrome or not.

Problem Statement

Write a C program to check if a given 5 digit number is palindrome or not. Take input from the user, check if the given number is a palindromic number and display appropriate message in the end.

A number is said to be a palindromic number if it remains same when reversed. For example 12321 is a palindromic number, where as 12345 is not. You can read more about Palindromic number.

Solution

The program takes input number from the user. Then the digits of the given number are reversed. Reversed number is compared with the original number to check if they are equal. If they are equal, the original number is a palindrome. If they are not equal, then the number is not a palindrome.

The Program

/* C program to check whether a given 5 digit number is palindrome or not
* (c) www.c-program-example.com
*/
#include<stdio.h>
int main() {
int num, rem, i, rev = 0, num1, count = 0;
printf("Enter a five digit number:\n");
scanf("%d", &num);
num1 = num;
// reverse the given number
while(num > 0) {
rem = num % 10;
rev = rem + rev * 10;
num = num / 10;
count++;
}
if(count == 5) {
if(num1 == rev) { // if it's palindrome, reverse & original number will be same
printf("The Given Number %d is Palindrome", num1);
} else {
printf("The Given Number %d is NOT Palindrome",num1);
}
} else {
printf("The given %d number is not a five digit number!",num1);
}
return 0;
}
view raw palindrome.c hosted with ❤ by GitHub

Sample Output

Palindrome number or not

Related Programs

  1. C program to check whether a given string is palindrome or not
  2. Number System

To get regular updates on new C programs, you can Follow @c_program

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to convert a Binary number into its equivalent Decimal, Octal and Hexadecimal numbers.

C Program to convert binary number into its equivalent Decimal, Octal, and Hexadecimal representations. In this program we convert a binary number to decimal. Then convert that decimal number to octal and Hexadecimal equivalent.

The program implements various functions to do the job.

  • binary_to_decimal : Convert given binary number to it’s decimal equivalent
  • decimal_to_octal : Convert from decimal to octal
  • decimal_to_hex : Convert from decimal to hexadecimal

The main function takes care of getting input from user, calling the above functions and displaying the results.

Read more here: What are binary, octal, and hexadecimal notation?

The Program

#include <stdio.h>
/*
* Function to convert a given decimal number to its
* hexadecimal equivalent.
*/
int decimal_to_hex(long number, char *hexstr, int pos) {
long i;
int new_position, n;
char hexchar;
new_position = pos;
if(number > 0) {
i = number % 16;
n = number / 16;
new_position = decimal_to_hex(n, hexstr, pos);
if(i >= 10) {
switch(i) {
case 10: hexchar = 'A'; break;
case 11: hexchar = 'B'; break;
case 12: hexchar = 'C'; break;
case 13: hexchar = 'D'; break;
case 14: hexchar = 'E'; break;
case 15: hexchar = 'F'; break;
}
} else {
hexchar = '0' + i;
}
hexstr[new_position++] = hexchar;
hexstr[new_position] = '\0';
}
return new_position;
}
/*
* Function to convert a given binary number to
* its decimal equivalent.
*/
int binary_to_decimal(int num) {
int rem, base = 1, decimal_number = 0;
while( num > 0) {
rem = num % 10;
if((rem == 0) || (rem == 1)) {
decimal_number = decimal_number + rem * base;
num = num / 10 ;
base = base * 2;
} else {
return -1; // Invalid binary number
}
}
return decimal_number;
}
/*
* Function to convert a given decimal number in to
* its octal equivalent.
*/
int decimal_to_octal(int number) {
int octal_number = 0, rem, base = 1;
while(number > 0) {
rem = number % 8;
octal_number = octal_number + rem * base;
base = base * 10;
number = number / 8;
}
return octal_number;
}
void main() {
int binary_numer, decimal_number, octal_number;
char hexstr[25] = "0";
printf("Enter a binary number: ");
scanf("%d", &binary_numer);
printf("Given Binary number is: %d\n", binary_numer);
decimal_number = binary_to_decimal(binary_numer);
if(decimal_number == -1) {
printf("\nInvalid binary number. Try again.");
return;
}
printf("Its Decimal equivalent is: %d", decimal_number);
octal_number = decimal_to_octal(decimal_number);
printf("\nIts octal equivalent is: %d", octal_number);
decimal_to_hex(decimal_number, hexstr, 0);
printf("\nIts Hexa Decimal equivalent is: %s", hexstr);
}

Sample Output

Binary to decimal, binary to octal, binary to hexadecimal

Related Programs

You can discuss these programs on our Facebook Page. Like to get updates right inside your feed reader? Grab our feed!

Edit1 : 11th August 2017

  • Added screenshot of sample runs
  • Moved the code to it’s own Gist
  • Modified the program to make it more modular

(c) www.c-program-example.com

C program to create a subsets using backtracking method.

C Program to find the subsets in the set. We use the backtracking method to solve this problem. Backtracking is the refinement method of Brute-Force method. Backtrack method means it finds the number of sub solutions and each may have number of sub divisions, and solution chosen for exactly one. Backtracking method is a recursive method. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
int inc[50],w[50],sum,n;
int promising(int i,int wt,int total)
{
return(((wt+total)>=sum)&&((wt==sum)||(wt+w[i+1]<=sum)));
}
/*
* You can find this program on GitHub
* https://github.com/snadahalli/cprograms/blob/master/subsets.c
*/
void main()
{
int i,j,n,temp,total=0;
clrscr();
printf("n Enter how many numbers:n");
scanf("%d",&n);
printf("n Enter %d numbers to th set:n",n);
for(i=0;i<n;i++)
{
scanf("%d",&w[i]);
total+=w[i];
}
printf("n Input the sum value to create sub set:n");
scanf("%d",&sum);
for(i=0;i<=n;i++)
for(j=0;j<n-1;j++)
if(w[j]>w[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("n The given %d numbers in ascending order:n",n);
for(i=0;i<n;i++)
printf("%d t",w[i]);
if((total<sum))
printf("n Subset construction is not possible");
else
{
for(i=0;i<n;i++)
inc[i]=0;
printf("n The solution using backtracking is:n");
sumset(-1,0,total);
}
getch();
}
void sumset(int i,int wt,int total)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("n{t");
for(j=0;j<=i;j++)
if(inc[j])
printf("%dt",w[j]);
printf("}n");
}
else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

–>

(c) www.c-program-example.com

C program to implement Depth First Search(DFS)

C program to implement Depth First Search(DFS). Depth First Search is an algorithm used to search the Tree or Graph. DFS search starts from root node then traversal into left child node and continues, if item found it stops other wise it continues. The advantage of DFS is it requires less memory compare to Breadth First Search(BFS).Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("n %d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf("n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("n Enter the adjacency matrix:n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("n Graph is connected");
else
printf("n Graph is not connected");
getch();
Read more Similar C Programs
Data Structures
Breadth First Search(BFS)
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

Breadth First Search (BFS) in C

Breadth First Search/Traversal

C program to implement Breadth First Search(BFS). Breadth First Search is an algorithm used to search a Tree or Graph. BFS search starts from root node then traverses into next level of graph or tree, if item found it stops other wise it continues with other nodes in the same level before moving on to the next level. The algorithm can also be used for just Tree/Graph traversal, without actually searching for a value. This is what being done in the program below. The disadvantage of BFS is it requires more memory compare to Depth First Search(DFS).

The Program

#include<stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
}
}
void main() {
int v;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
for(i=1; i <= n; i++) {
q[i] = 0;
visited[i] = 0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1; i<=n; i++) {
for(j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
}
}
printf("\n Enter the starting vertex:");
scanf("%d", &v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1; i <= n; i++) {
if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are reachable");
break;
}
}
}
view raw bfs.c hosted with ❤ by GitHub

Sample Output

BFS in C

The graph’s matrix representation is used as input to our program. A value of 1 at [i][j] represents presence of a path from i to j. 0 represents no path.

Related Programs

To get regular updates on new C programs, you can Follow @c_program on Twitter. Or you can discuss these programs on our Facebook Page.

Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com

C Program to find the Inverse of the Matrix.

C Program to find the Inverse of a Matrix. To find the Matrix Inverse, matrix should be a square matrix and Matrix Determinant is should not Equal to Zero. if A is a Square matrix and |A|!=0, then AA’=I (I Means Identity Matrix). Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<math.h>
float detrminant(float[][], float);
void cofactors(float[][], float);
void trans(float[][], float[][], float);
main() {
float a[25][25], n, d;
int i, j;
printf("Enter the order of the matrix:n");
scanf("%f", &n);
printf("Enter the elemnts into the matrix:n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%f", &a[i][j]);
}
}
d = detrminant(a, n);
printf("nTHE DETERMINANT IS=%2f", d);
if (d == 0)
printf("nMATRIX IS NOT INVERSIBLEn");
else
cofactors(a, n);
}

float detrminant(float a[25][25], float k) {
float s = 1, det = 0, b[25][25];
int i, j, m, n, c;
if (k == 1) {
return (a[0][0]);
} else {
det = 0;
for (c = 0; c < k; c++) {
m = 0;
n = 0;
for (i = 0; i < k; i++) {
for (j = 0; j < k; j++) {
b[i][j] = 0;
if (i != 0 && j != c) {
b[m][n] = a[i][j];
if (n < (k - 2))
n++;
else {
n = 0;
m++;
}
}
}
}
det = det + s * (a[0][c] * detrminant(b, k - 1));
s = -1 * s;
}
}
return (det);
}

void cofactors(float num[25][25], float f) {
float b[25][25], fac[25][25];
int p, q, m, n, i, j;
for (q = 0; q < f; q++) {
for (p = 0; p < f; p++) {
m = 0;
n = 0;
for (i = 0; i < f; i++) {
for (j = 0; j < f; j++) {
b[i][j] = 0;
if (i != q && j != p) {
b[m][n] = num[i][j];
if (n < (f - 2))
n++;
else {
n = 0;
m++;
}
}
}
}
fac[q][p] = pow(-1, q + p) * detrminant(b, f - 1);
}
}
trans(num, fac, f);
}

void trans(float num[25][25], float fac[25][25], float r)

{
int i, j;
float b[25][25], inv[25][25], d;
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++) {
b[i][j] = fac[j][i];
}
}

d = detrminant(num, r);
inv[i][j] = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++) {
inv[i][j] = b[i][j] / d;
}
}

printf("nTHE INVERSE OF THE MATRIX:n");
for (i = 0; i < r; i++) {
for (j = 0; j < r; j++) {
printf("t%2f", inv[i][j]);
}
printf("n");
}
}
Read more Similar C Programs
Matrix Programs
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to implement Dijkstra’s algorithm.

C Program to implement Dijkstra’s algorithm. Dijkstra’s Algorithm finds the shortest path with the lower cost in a Graph. Dijkstra’s Algorithm solves the Single Source Shortest Path problem for a Graph. It is a Greedy algorithm and similar to Prim’s algorithm. Read more about C Programming Language .


/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include "stdio.h"
#include "conio.h"
#define infinity 999

void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}

void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("n Enter the number of nodes:");
scanf("%d",&n);
printf("n Enter the cost matrix:n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("n Shortest path:n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%dn",v,i,dist[i]);
getch();
}
Read more Similar C Programs
Data Structures
Prims Algorithm

Learn C Programming

You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to solve Knapsack problem

C Program to solve Knapsack problem. Knapsack problem is also called as rucksack problem. In Knapsack problem, given a set items with values and weights and a limited weight bag . We have to find the optimum solution so that, in minimum cost(value) fill the bag with the maximum weight. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("nEnter the number of elementsn");
scanf("%d",&n);
printf("Enter the profit and weights of the elementsn");
for(i=1;i<=n;i++)
{
printf("For item no %dn",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("nEnter the capacity n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included aren");
printf("Sl.notweighttprofitn");
for(i=1;i<=n;i++)
if(x[i])
printf("%dt%dt%dn",++count,w[i],p[i]);
printf("Total profit = %dn",profit);
getch();
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

Binary To Decimal in C

C Program to convert a binary number into its equivalent Decimal. In binary number system or base-2 system numeric valuer are represented by using two different symbols 0 and 1. The binary number system is a positional notation with a radix of 2.

Read more here: What are binary, octal, and hexadecimal notation?

This program converts a given base-2 number to it’s decimal equivalent (or base-10 representation).

The Program

#include<stdio.h>
/*
* Function to convert a given binary number to
* its decimal equivalent.
*/
int binary_to_decimal(int num) {
int rem, base = 1, decimal_number = 0;
while( num > 0) {
rem = num % 10;
if((rem == 0) || (rem == 1)) {
decimal_number = decimal_number + rem * base;
num = num / 10 ;
base = base * 2;
} else {
return -1; // Invalid binary number
}
}
return decimal_number;
}
int main() {
long int binary_number, decimal_number;
printf("Enter any binary number : ");
scanf("%ld", &binary_number);
decimal_number = binary_to_decimal(binary_number);
if(decimal_number != -1) {
printf("The decimal equivalent value of binary %ld is: %ld",
binary_number, decimal_number);
} else {
printf("\nPlease enter a valid binary number!");
}
return 0;
}

Sample Output

Binary To Decimal

 

Related Programs

Edit1: 21st August 2017

  • Added links to related programs
  • Added a screenshot of sample output
  • Moved program to it’s own gist.

 

To get regular updates on new C programs, you can Follow @c_program.

You can discuss these programs on our Facebook Page.

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to count number of characters in the file

C Program to count number of characters in the file. In this program you can learn c file operations. Here we counting the characters by reading the characters in the file one by one and if read character was not an ‘n’ ,’t’ or EOF, it increments the counter by one. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

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

void main()
{
char ch;
int count=0;
FILE *fptr;
clrscr();
fptr=fopen("text.txt","w");
if(fptr==NULL)
{
printf("File can't be createda");
getch();
exit(0);
}
printf("Enter some text and press enter key:n");
while((ch=getche())!='r')
{
fputc(ch,fptr);
}
fclose(fptr);
fptr=fopen("text.txt","r");
printf("nContents of the File is:");
while((ch=fgetc(fptr))!=EOF)
{
count++;
printf("%c",ch);
}
fclose(fptr);
printf("nThe number of characters present in file is: %d",count);
getch();
}

Read more Similar C Programs
C Basic

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com