C Program to Check Leap Year

Source code:

#include <stdio.h>

int main()
{
    int year;

    printf("Enter a year: ");
    scanf("%d",&year);

    if(year%4 == 0)
    {
        if( year%100 == 0)
        {
            // year is divisible by 400, hence the year is a leap year
            if ( year%400 == 0)
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
        }
        else
            printf("%d is a leap year.", year );
    }
    else
        printf("%d is not a leap year.", year);
    
    return 0;
}

Output 1:

Enter a year: 1900
1900 is not a leap year.

Output 2:

Enter a year: 2012
2012 is a leap year.

C Program to Calculate Area and Circumference of circle

Source Code:

#include<stdio.h>

int main() {

   int rad;
   float PI = 3.14, area, ci;

   printf("\nEnter radius of circle: ");
   scanf("%d", &rad);

   area = PI * rad * rad;
   printf("\nArea of circle : %f ", area);

   ci = 2 * PI * rad;
   printf("\nCircumference : %f ", ci);

   return (0);
}

Output:

Enter radius of a circle : 1
Area of circle : 3.14
Circumference  : 6.28

Introduction Examples

Program to Display "Hello, World!"

#include <stdio.h>
#include <conio.h>
void main()
{
    // printf() displays the string inside quotation
    printf("tutorialsforcprogramming.blogspot.com");
    getch();
}

Output

tutorialsforcprogramming.blogspot.com

Get differnet value from user & print it on diaplay

#include <stdio.h>
#include <conio.h>
void main()
{
    int in;
    float fn;
    char c; 

    printf("Enter an integer: ");  
    scanf("%d", &in);                //  %d is used for get or print integer
    printf("You entered: %d", in);

    printf("Enter an float: ");  
    scanf("%f", &fn);                //  %f is used for get or print float
    printf("You entered: %f", fn);

    printf("Enter an character: ");  //  %c is used for get or print character
    scanf("%c", &c);                 
    printf("You entered: %c", c);

    getch();
}

Output

Enter an integer: 5
You entered: 5
Enter an float: 5.005
You entered: 5.005
Enter an character: D
You entered: D

Perform some operation on integer

#include <stdio.h>
#include <conio.h>
void main()
{
    int a=5;b=10,c;
    c = b + a;
    printf("b + a = %d", c);

    c = b - a;
    printf("b - a = %d", c);

    c = b * a;
    printf("b * a = %d", c);

    c = b / a;
    printf("b / a = %d", c);

}

Output

b + a = 15
b - a = 5
b * a = 50
b / a = 2


Operators



C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform certain mathematical or logical manipulations. Operators are used in program to manipulate data and variables.

C operators can be classified into following types,

  • Arithmetic operators
  • Relation operators
  • Logical operator
  • Bitwise operators
  • Assignment operators
  • Conditional operators
  • Special operators

Input/Output

C programming has several in-build library functions to perform input and output tasks.

Two commonly used functions for I/O (Input/Output) are printf() and scanf().

The scanf() function reads formatted input from standard input (keyboard) whereas the printf() function sends formatted output to the standard output (screen).





Example #1: C Output

#include <stdio.h>      //This is needed to run printf() function.
int main()
{
    printf("C Programming");  //displays the content inside quotation
    return 0;
}
Output :
C Programming
How this program works?

  • All valid C program must contain the main() function. The code execution begins from the start of main() function.
  • The printf() is a library function to send formatted output to the screen. The printf() function is declared in "stdio.h" header file.
  • Here, stdio.h is a header file (standard input output header file) and #include is a preprocessor directive to paste the code from the header file when necessary. When the compiler encounters printf() function and doesn't find stdio.h header file, compiler shows error.
  • The return 0; statement is the "Exit status" of the program. In simple terms, program ends.




#2: C Integer Output

#include <stdio.h>
int main()
{
    int testInteger = 5;
    printf("Number = %d", testInteger);
    return 0;
}

Output

Number = 5

Inside the quotation of printf() function, there is a format string "%d" (for integer). If the format string matches the argument (testInteger in this case), it is displayed on the screen.





Example #3: C Integer Input/Output

#include <stdio.h>
int main()
{
    int testInteger;
    printf("Enter an integer: ");
    scanf("%d",&testInteger);  
    printf("Number = %d",testInteger);
    return 0;
}

Output

Enter an integer: 4
Number = 4

The scanf() function reads formatted input from the keyboard. When user enters an integer, it is stored in variable testInteger. Note the '&' sign before testInteger; &testInteger gets the address of testInteger and the value is stored in that address





Example #4: C Floats Input/Output

#include <stdio.h>
int main()
{
    float f;
    printf("Enter a number: ");   // %f format string is used in case of floats
    scanf("%f",&f);
    printf("Value = %f", f);
    return 0;
}

Output

Enter a number: 23.45
Value = 23.450000

The format string "%f" is used to read and display formatted in case of floats.





Example #5: C Character I/O

#include <stdio.h>
int main()
{
    char var1;
    printf("Enter a character: ");
    scanf("%c",&var1);     
    printf("You entered %c.",var1);  
    return 0;
} 

Output

Enter a character: g
You entered g.

Format string %c is used in case of character types.


Little bit on ASCII code

When a character is entered in the above program, the character itself is not stored. Instead a numeric value(ASCII value) is stored. And when we displayed that value using "%c" text format, the entered character is displayed.




Example #6: C ASCII Code

#include <stdio.h>
int main()
{
    char var1;
    printf("Enter a character: ");
    scanf("%c",&var1);     

    // When %c text format is used, character is displayed in case of character types
    printf("You entered %c.\n",var1);  

    // When %d text format is used, integer is displayed in case of character types
    printf("ASCII value of %c is %d.", var1, var1);  
    return 0;
}

Output
Enter a character: g
You entered g.
ASCII value of g is 103.

You can display a character if you know ASCII code of that character. This is shown by following example.





Example #7: C ASCII Code

#include <stdio.h>
int main()
{
    int var1 = 69;
    printf("Character having ASCII value 69 is %c.",var1);
    return 0;
} 

Output

Character having ASCII value 69 is E.