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

C Program to Demonstrate the increment and decrement operators

C program which demonstrates the working of increment(++) and decrement(–) operators. Increment operator ++ adds 1 to its operand and Decrement operator — subtracts 1 from its operand.

These operators may be used either as a prefix operator or post-fix operator. Read more here: Increment and decrement operators

The Program

/* C Program to demonstrate increment and decrement operators */
main()
{
/*
* ++i -> i incremented before i is used.
* --i -> i decremented before i is used.
* j++ -> j is incremented AFTER j has been used.
* j-- -> j is decremented AFTER j has been used.
*/
int i=1,j=1;
puts("\tDemo 1");
printf("\t%d %d\n",++i, j++); /* O/P 2 1 */
printf("\t%d %d\n",i, j); /* O/P 2 2 */
i=1; j=1;
puts("\n\tDemo 2");
printf("\t%d \n", i=j++); /* O/P 1 */
printf("\t%d \n", i=++j); /* O/P 3 */
i = 0; j = 0;
puts("\n\tDemo 3");
if ( (i++ == 1) && (j++ == 1)) puts("Some text");
/* Will i and j get incremented? The answer is NO! Because
* the expression in the left of '&&' resolves to false the
* compiler does NOT execute the expression on the right and
* so 'j' does not get executed!!!!! */
printf("\t%d %d\n",i, j); /* O/P 1 0 */
}

Sample Output

Increment and decrement operators in c

Related programs

Airthmatic Operators
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.

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

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

C program to print Fibonacci numbers using Recursion

C Program to print Fibonacci numbers. In this program we used the Recursion method. Recursion is the programming technique that a process invoking itself again and again. Fibonacci numbers are sequence of numbers starts from 0 and 1 , continue by adding previous number. 0,1,1,2,3,5,8,13,…….. 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 fib(int);
int f=1,fib1=0,fib2=0,i=0,j;
void main()
{
int num;
clrscr();
printf(" How many Fibonacci numbers do you want?n");
scanf("%d",&num);
printf("nFibonacci Numbers are:n");
f=0;
printf("n%dn",f);
f=1;
printf("n%dn",f);
for(j=0;j<num-2;j++)
{
f=fib(num);
printf("n%dn",f);
}
getch();
}
int fib(int n)
{

while(i<n)
{
if(i<=n)
{
i++;
fib1=fib2;
fib2=f;
f=fib2+fib1;
fib(1);
return f;
}
}

}


Read more Similar C Programs
Learn C Programming
Recursion
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 calculate factorial using Recursive function

C Program to find the factorial of a number. In this program we used the Recursion method. Recursion is the programming technique that a process invoking itself again and again. The standard recursive function for factorial is factorial=n*fact(n-1). factorial of number denoted by ‘!’, means product of all non negative integers from 1 to number. example: 5!=5*4*3*2*1=120. 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 factorial(int);

void main()
{
int num;
printf("Enter the number to calculate Factorial :");
scanf("%d",&num);
printf("nFactorial : %d", factorial (num));

}
int factorial (int i)
{
int f;
if(i==1)
return 1;
else
f = i* factorial (i-1);
return f;
}
Read more Similar C Programs
Learn C Programming
Recursion
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

Array search program in C

Very often we encounter a situation where we have to search for a given target value in a list of values. Simple value types like integers are stored in arrays. This program demonstrates how to do a search within an array by using a method called linear search.

Linear search, also called as ‘Sequential search’ is a technique of finding a target value within a list. It sequentially checks each element of the list with target value until a match is found or the list is exhausted. This approach may be very inefficient to be used in practical problem solving. However, this is very easy to understand and implement. You can read more about linear search.

This program implements linear search to search for a given integer key in an integer array. We have many more C programs that deal with arrays. See more Programs on Arrays in C.

The Program

#include <stdio.h>
#include <conio.h>
void main()
{
int array[10];
int i, N, keynum, found=0;
printf("Enter the value of N\n");
scanf("%d",&N);
printf("Enter the elements one by one\n");
for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);
}
printf("Input array is\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
printf("Enter the element to be searched\n");
scanf("%d", &keynum);
for ( i=0; i < N ; i++)
{
if( keynum == array[i] )
{
found = 1;
break;
}
}
if ( found == 1)
printf("Element found in the given array");
else
printf("Element was not found in the array");
} /* End of main */

Sample Output


Related programs

 

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