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

[gist id=”edcbcb339781cb831a2143821b0f32ce”]

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 find GCD and LCM using Recursion

This C program finds the GCD (Greatest Common Divisor) and LCM (Least Common Multiple) of two numbers using recursion. Recursion is a technique where a function calls itself to solve a smaller version of the same problem. The GCD is computed with the classic recursive Euclidean algorithm, and the LCM is then derived from it using a simple mathematical identity.

The Key Idea

For any two positive integers, GCD(a, b) × LCM(a, b) = a × b. So once we know the GCD, the LCM is just:

LCM = (a / GCD) × b

The recursive Euclidean rule for GCD is: GCD(a, b) = GCD(b, a % b), stopping when b becomes 0.

The Program

#include <stdio.h>

/* Recursive Euclidean algorithm for GCD. */
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int main(void)
{
    int num1, num2, g, lcm;

    printf("Enter two numbers : ");
    scanf("%d %d", &num1, &num2);

    g = gcd(num1, num2);
    lcm = (num1 / g) * num2;   /* divide first to avoid overflow */

    printf("GCD of %d and %d is : %d\n", num1, num2, g);
    printf("LCM of %d and %d is : %d\n", num1, num2, lcm);
    return 0;
}

How the Program Works

  • gcd() calls itself with (b, a % b) each time. The remainder shrinks on every call until b is 0, at which point a holds the GCD — this is the recursive base case.
  • The LCM uses the identity above. We divide num1 / g before multiplying by num2 to reduce the risk of integer overflow on large inputs.
  • This is far cleaner than the original textbook version, which counted upwards with a static variable inside the recursion — correct but confusing and slow.

Sample Output

Enter two numbers : 8 12
GCD of 8 and 12 is : 4
LCM of 8 and 12 is : 24

For a clear explanation of recursion and the Euclidean algorithm, The C Programming Language by Kernighan and Ritchie is the classic reference — find it on Amazon.

This post contains affiliate links. If you buy through them, we may earn a small commission at no extra cost to you.

Related C Programs

Try it instantly in one of the best online C compilers, or set up a local toolchain with our complete C development environment guide.

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

[gist id=”0b170ef7c9f4a39dc7381d8e33f7b9bc”]

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

C Program to demonstrate the ‘getchar’ function.

The getchar() function reads a single character from standard input (the keyboard) and returns it. It is one of the simplest input functions in C and a great way to understand how the input buffer works. In this program we use getchar() in a loop to read text entered by the user and count how many characters were typed before the Enter key.

About getchar()

  • getchar() reads one character at a time from stdin and returns it as an int.
  • It returns int — not char — so it can also return the special value EOF (end of file) when there is no more input.
  • Input is line-buffered: characters are delivered to your program only after the user presses Enter.

The Program

#include <stdio.h>

int main(void)
{
    int count = 0;
    int ch;

    printf("Please enter some text and press Enter:\n");

    /* Read characters until Enter (newline) or end-of-input. */
    while ((ch = getchar()) != '\n' && ch != EOF)
        count++;

    printf("You entered %d characters.\n", count);
    return 0;
}

How the Program Works

  • The expression (ch = getchar()) reads one character and assigns it to ch, all inside the while condition.
  • The loop keeps going as long as the character is not a newline ('\n') and not EOF, incrementing count each time.
  • When the user presses Enter, getchar() returns '\n', the loop stops, and the total is printed.
  • ch is declared int (not char) so the comparison with EOF is reliable — a very common beginner bug is declaring it char.

This is a clean, standard-C version of the classic example. The original used main() with no return type and #define RETURN 'n' — note that 'n' is the letter n, whereas the newline character is '\n' (with a backslash).

Sample Output

Please enter some text and press Enter:
Hello World
You entered 11 characters.

(The 11 characters are H e l l o (space) W o r l d.)

getchar() vs Other Input Functions

Function Reads Typical use
getchar() One character Character-by-character processing
scanf() Formatted input Numbers and words
fgets() A whole line (safely) Reading strings without overflow

For a thorough explanation of getchar(), the input buffer and EOF, The C Programming Language by Kernighan and Ritchie is the definitive reference — find it on Amazon.

This post contains affiliate links. If you buy through them, we may earn a small commission at no extra cost to you.

Related C Programs

Want to try it instantly? Paste the code into one of the best online C compilers and run it in your browser, or set up a local toolchain with our complete C development environment guide.

C Program to demonstrate the ‘fgets’ function.

Program to demonstrate the ‘fgets’ function. The program will count the number of lines in a file. This is a function of the UNIX command ‘wc’. 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>

#define LINE_LENGTH 80

main()
{
FILE* fp;
char line[LINE_LENGTH];
int count=0;

fp=fopen("/home/DOC/C/c.html","r");
/* Count up the lines here. */
while ( fgets(line, LINE_LENGTH, fp) != NULL) count++;

printf("File contains %d lines.n", count);

fclose(fp);
}
Read more Similar C Programs
C Strings

Simple C Programs

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 the Combinations and Permutations.

C Program to calculate the Combination and Permutations. Combination means way of selecting a things or particular item from the group or sets. nCr=n!/r!(n-r)!. Permutations means possible way of rearranging in the group or set in the particular order. 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>

main()
{
int n , r, ncr( int , int);
long npr( int , int);
long double fact( int);
printf(" Enter value of n & r n");
scanf("%d %d",&n , &r);
if( n>= r)
{
printf( " %dC%d is %dn", n,r,ncr( n , r));
printf(" %dP%d is %ld", n,r,npr( n, r));
}
else
{
printf("WRONG INPUT?? enter the correct input");
}
}
long double fact( int p)
{
long double facts = 1;
int i;
for( i = 1; i<= p; i++)
facts = facts * i;
return( facts);
}

int ncr ( int n, int r)
{
return( fact( n) / (fact( r) * fact(n- r) ) ) ;
}

long npr( int n , int r)
{
return( fact( n) / fact( n- r));
}
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 check the given number is Armstrong or not?

C Program to check the given number is Armstrong number or not?. Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. Example: 153 = 1^3 + 5^3 + 3^3. 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>
void main()
{
int num,num1,arms=0,rem;

printf("Enter the number:n");
scanf("%d",&num);
num1=num;
while(num>0)
{
rem=num%10;
arms=arms+rem*rem*rem;
num=num/10;
}
if(num1==arms)
{
printf(" n%d is an Armstrong number",num1);
}
else
{
printf("n%d is NOT an Armstrong number",num1);
}

}
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 Demonstrate #if, #else & #endif preprocessors.

C Program to demonstrate the Preprocessor directives like #if, #else, #define, #endif. C Preprocessors are not the program statements, they are executed before the actual compilation of the code. C Preprocessors substitute the code where they called, i.e they replace the code as they defined. #if, #else are the conditional directives. Here at the compile time #if value is false, so #else part is execute.
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>

#define SWITCH 0

/* The #if can only perform
* INTEGER tests!!!!
*/
#if ( SWITCH == 1 )
#define TEXT "Example to #if"
#else
#define TEXT "Example to # else"
#endif

main ()
{
printf(TEXT);
}
Read more Similar C Programs
Learn C Programming

Number System

Preprocessor
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