C Program to print the pyramid pattern.

C program to print the pyramid pattern. This program prints pyramid of 5 rows using a for loop. 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>
int main()
{
int r, c,t=5;

for ( r = 1 ; r <= 5 ; r++ )
{
for ( c = 1 ; c < t ; c++ )
printf(" ");

t--;

for ( c = 1 ; c <= 2*r - 1 ; c++ )
printf("*");

printf("n");
}


return 0;
}
Read more Similar C Programs
Learn C Programming

Simple C Programs

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 demonstrate Ternary condition.

C Program to demonstrate the Ternary conditional operator(?). Ternary operator is a short hand combination of the if-else statement. You can use the ternary operator in initialize lists. 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>
int main()
{
int min,x,y;
printf("Enter two numbersn");
scanf("%d%d",&x,&y);
min=x <= y ? x : y;
/*
The ternary operator (?) can be rewritten using the if-else statement:
if(x<=y)
{
min=x;
}
else
{
min=y;
}*/
printf("n Small number is: %d",min);
return 0;
}
Read more Similar C Programs
Learn C Programming

Simple C Programs

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 find Binomial Coefficients

C Program to find Binomial Integers without using recursion.

Binomial coefficients are positive integers that are coefficient of any term in the expansion of (x + a) the number of combination’s of a specified size that can be drawn from a given set.

There are many ways to compute the Binomial coefficients. Like,

In this post we will be using a non-recursive, multiplicative formula.

The program is given below:

// C program to find the Binomial coefficient. Downloaded from www.c-program-example.com 
#include<stdio.h> 
void main() {
    int i, j, n, k, min, c[20][20]={0};
    printf("This program is brought to you by www.c-program-example.com\n" );     
    printf("\n Enter the value of n: ");     
    scanf("%d", &n);     
    printf("\n Enter the value of k: ");     
    scanf("%d", &k);
    if(n >= k) {         
        for(i=0; i<=n; i++) {             
            min = i<k? i:k;
            for(j = 0; j <= min; j++) {
                 if(j==0 || j == i) {
                     c[i][j] = 1;
                 } else {
                     c[i][j] = c[i-1][j-1] + c[i-1][j];
                 }
             }
         }
         printf("%d\t",c[n][k]);
         printf("\n");
     } else {
         printf("\n Invalid input \n Enter value n>=k \n");
     }
}

Sample output

Links

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 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

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 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