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

C Program to find the GCD and LCM. In this program we used the Recursion method. Recursion is the programming technique that a process invoking itself again and again. Here find_gcd() and find_(lcm) are the recursive methods. for example LCM and GCD of 8,12 is 24 and 4 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
***********************************************************/

/*
* http://www.c-program-example.com/2011/10/c-program-to-find-gcd-and-lcm-using.html
*/

/* Find this on GitHub:
* https://github.com/snadahalli/cprograms/blob/master/gcd_lcm_rec.c
*/

#include "stdio.h"
int find_gcd(int,int);
int find_lcm(int,int);
int main(){
int num1,num2,gcd,lcm;
printf("nEnter two numbers:n ");
scanf("%d %d",&num1,&num2);
gcd=find_gcd(num1,num2);

printf("nnGCD of %d and %d is: %dnn",num1,num2,gcd);

if(num1>num2)
lcm = find_lcm(num1,num2);
else
lcm = find_lcm(num2,num1);

printf("nnLCM of %d and %d is: %dnn",num1,num2,lcm);
return 0;
}

int find_gcd(int n1,int n2){
while(n1!=n2){
if(n1>n2)
return find_gcd(n1-n2,n2);
else
return find_gcd(n1,n2-n1);
}
return n1;
}

int find_lcm(int n1,int n2){

static int temp = 1;

if(temp % n2 == 0 && temp % n1 == 0)
return temp;
temp++;
find_lcm(n1,n2);

return temp;
}
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

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

C program to find the character type.

C Program to find the Character types. Here we used the isdidit(), isalpa(), and isspace() functions.isdigit() function returns true if argument is a decimal digit, otherwise false. isalpa() function returns true if argument is a defined set of characters in c, otherwise false. isspace() function returns true if argument is a white space character (one of ‘ ‘ ‘f’ ‘n’ ‘r’ ‘t’ ‘v’), otherwise false. 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 <ctype.h>

#define FALSE 0
#define TRUE 1

/* function declarations */
int char_type(char);

main()
{
char ch;
/* get a character from the keyboard */
printf(" Please enter a charcater => ");
ch = getc(stdin);

char_type(ch); /* Figure out the character type */
}


int char_type(char ch)
{
/* returns non zero if A-Z or a-z */
if ( isalpha(ch) != FALSE)
printf("%c is an Alpha character.n",ch);

/* returns non zero if 0-9 */
if ( isdigit(ch) != FALSE)
printf("%c is a numeric character.n",ch);

/* returns non zero if a space, CR, Tab, NL FF */
if ( isspace(ch) != FALSE)
printf("%c is white spacen", ch);

}
Read more Similar C Programs
Searching in C

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 extern variable.

C Program to demonstrate extern variable. Extern variables are very useful in c,when the source code is in the two or more files, you can refer the variables from one file to another using extern variables. Extern variables are also global variable,but in explicit manner. 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 write_extern(void);

extern int count;

void write_extern(void)
{
printf("count is %in", count);
}

int count = 5;

main()
{
write_extern();
}

Read more Similar C Programs
C Strings

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 out the size of the different data types

C Program to find the Size of Different data types. In This program we, find the size of the datatypes in c using sizeof() operator. sizeof() operator returns the its argument memory in bytes. 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>
main()
{

printf(" short int is %2d bytes n", sizeof(short int));
printf(" int is %2d bytes n", sizeof(int));
printf(" int * is %2d bytes n", sizeof(int *));
printf(" long int is %2d bytes n", sizeof(long int));
printf(" long int * is %2d bytes n", sizeof(long int *));
printf(" signed int is %2d bytes n", sizeof(signed int));
printf(" unsigned int is %2d bytes n", sizeof(unsigned int));
printf("n");
printf(" float is %2d bytes n", sizeof(float));
printf(" float * is %2d bytes n", sizeof(float *));
printf(" double is %2d bytes n", sizeof(double));
printf(" double * is %2d bytes n", sizeof(double *));
printf(" long double is %2d bytes n", sizeof(long double));
printf("n");
printf(" signed char is %2d bytes n", sizeof(signed char));
printf(" char is %2d bytes n", sizeof(char));
printf(" char * is %2d bytes n", sizeof(char *));
printf("unsigned char is %2d bytes n", sizeof(unsigned char));
}
Read more  C Programs
Array In C

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 the ‘atof’ and ‘gets’ functions.

C Program to demonstrate the ‘atof’ and ‘gets’ functions. atof function converts the intial nptr string to the double. atof means ASCII to float. gets function reads string from the input stream. puts function prints string to the output stream This program will multiply to floating point numbers. The program will accept invalid data, and give you crap results as a result. 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 <stdlib.h>

main()
{
char str1[80], str2[80]; /* define a couple o' strings. */
double result; /* Result of multiplication. */

puts ("This program will multiply to floating point numbers.");
puts ("The program will accept invalid data, and give you");
puts ("crap results as a result.n");
puts ("Please enter the first number.");
gets(str1);

puts ("And the second.");
gets(str2);

result = atof(str1) * atof(str2);
printf("Answer is %8.2fn", result);
}


Read more Similar C Programs
C Strings

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