1. Your First C Program

    #include <stdio.h>, stdio is the library where the function printf is defined. printf is used for generating output. Before using this function, we have to first include the required file, also known as a header file (.h). You can also create your own functions, group them in header files and declare them at the top of the program to use them. To include a file in a program, use pre-processor directive

     
    #include <stdio.h>
    int main() {
     // printf() displays the string inside quotation
    printf("Hello, World!");
    return 0;
    }
    
        
    
  2. Print integer (User Input)

    In this example, the integer entered by the user is stored in a variable and printed on the screen.

    
     #include <stdio.h>
     int main() {   
     int number;
                                                    
    printf("Enter an integer: ");                                           
    // reads and stores input
     scanf("%d", &number);                                               
    // displays output
     printf("You entered: %d", number);                                    
     return 0;
     }
    
        
    
  3. Addition of two intger.

    The addition of two numbers in C language is the arithmetic operation of adding them and printing their sum on the screen. For example, if the input is 5 and 6, the output is 11.

    
                
     #include <stdio.h>
     int main() {    
                        
    int number1, number2, sum;
                            
        printf("Enter two integers: ");
        scanf("%d %d", &number1, &number2);
                        
        // calculating sum
         sum = number1 + number2;      
                            
        printf("%d + %d = %d", number1, number2, sum);
     return 0;
     }                         
       
        
    
  4. multiply two decimal value (Floting point value)

    In the below program to multiply two floating point numbers, the user is first asked to enter two floating numbers and the input is scanned using the scanf() function and stored in the variables A and B. Then, the variables A and B are multiplied using the arithmetic operator * and the product is stored in the variable product.

    
                
     #include <stdio.h>
     int main() {
    double a, b, product;
         printf("Enter two numbers: ");
         scanf("%lf %lf", &a, &b);  
                     
         // Calculating product
         product = a * b;
                    
         // %.2lf displays number up to 2 decimal point
         printf("Product = %.2lf", product);
                        
     return 0;
    }
    
     
    
  5. swapping of two number using temp veriable

    This program is used to swap values of two variables using the third variable, which is the temporary variable.

    
            
     #include<stdio.h>
     int main() {
     double first, second, temp;
             
        printf("Enter first number: ");
         scanf("%lf", &first);
         printf("Enter second number: ");
         scanf("%lf", &second);
            
         // value of first is assigned to temp
         temp = first;
            
         // value of second is assigned to first
         first = second;
            
         // value of temp (initial value of first) is assigned to second
         second = temp;
            
        // %.2lf displays number up to 2 decimal points
        printf("\nAfter swapping, first number = %.2lf\n", first);
        printf("After swapping, second number = %.2lf", second);
    return 0;
    }
    
    
     
    
  6. Check Even Or Odd.

    If a number is exactly divisible by 2 then its an even number else it is an odd number. In this article we have shared ways( C programs) to check whether the input number is even or odd. 1) Using Modulus operator(%).

    
    #include <stdio.h>
     int main() {
     int num;
         printf("Enter an integer: ");
         scanf("%d", &num);
            
         // true if num is perfectly divisible by 2
         if(num % 2 == 0)
              printf("%d is even.", num);
         else
              printf("%d is odd.", num);
                
     return 0;
     }
                                     
    
     
    
  7. Find the largest among three numbers

    To find the largest number among given three numbers in C programming, you have to ask from the user to enter any three number, now start checking which one is the largest using if-else block as shown in the program given below:

    
     #include <stdio.h>
     int main() {
    
        double n1, n2, n3;
            printf("Enter three different numbers: ");
            scanf("%lf %lf %lf", &n1, &n2, &n3);
            
            // if n1 is greater than both n2 and n3, n1 is the largest
            if (n1 >= n2 && n1 >= n3)
                 printf("%.2f is the largest number.", n1);
            
             // if n2 is greater than both n1 and n3, n2 is the largest
            if (n2 >= n1 && n2 >= n3)
                 printf("%.2f is the largest number.", n2);
            
             // if n3 is greater than both n1 and n2, n3 is the largest
             if (n3 >= n1 && n3 >= n2)
                 printf("%.2f is the largest number.", n3);
            
     return 0;
     }
                       
    
     
    
  8. Leap year check

    A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

    
     #include 
    int main() 
    {
     int year;
            printf("Enter a year: ");
            scanf("%d", &year);
                    
             // leap year if perfectly divisible by 400
            if (year % 400 == 0) {
             printf("%d is a leap year.", year);
             }
             // not a leap year if divisible by 100
             // but not divisible by 400
             else if (year % 100 == 0) {
             printf("%d is not a leap year.", year);
             }
            // leap year if not divisible by 100
            // but divisible by 4
            else if (year % 4 == 0) {
            printf("%d is a leap year.", year);
            }
            // all other years are not leap years
             else {
              printf("%d is not a leap year.", year);
            }
                    
     return 0;
     }
                           
        
         
    
  9. Factorial numbers.

    Factorial numbers.Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.

    
     #include <stdio.h>
    int main()
     {
     int n, i;
     unsigned long long fact = 1;
        printf("Enter an integer: ");
        scanf("%d", &n);
                
        // shows error if the user enters a negative integer
        if (n < 0)
             printf("Error! Factorial of a negative number doesn't exist.");
         else {
        for (i = 1; i <= n; ++i)
         {
            fact *= i;
         }
        printf("Factorial of %d = %llu", n, fact);
         }
                
    return 0;
     }
                
        
         
    
  10. Multipication table.

    The program below takes an integer input from the user and generates the multiplication tables up to 10. Here, the user input is stored in the int variable n. Then, we use a for loop to print the multiplication table up to 10.

    
     #include ≪stdio.h>
    int main() {
        int n, i;
        printf("Enter an integer: ");
        scanf("%d", &n);
         for (i = 1; i <= 10; ++i) {
         printf("%d * %d = %d \n", n, i, n * i);
         }
     return 0;
     }
                       
    
     
    
  11. Fibonacci Sequence

    The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21

    
     #include <stdio.h>
    int main() {
                
    int i, n;
                
     // initialize first and second terms
    int t1 = 0, t2 = 1;
                
     // initialize the next term (3rd term)
     int nextTerm = t1 + t2;
                
     // get no. of terms from user
     printf("Enter the number of terms: ");
     scanf("%d", &n);
                
    // print the first two terms t1 and t2
    printf("Fibonacci Series: %d, %d, ", t1, t2);
                
    // print 3rd to nth terms
    for (i = 3; i <= n; ++i) {
     printf("%d, ", nextTerm);
           t1 = t2;
           t2 = nextTerm;
           nextTerm = t1 + t2;
         }
                
     return 0;
    }
                                   
    
     
    
  12. Palindrome Check

    An integer is a palindrome if the reverse of that number is equal to the original number.

    
    #include <stdio.h>
     int main() {
    int n, reversed = 0, remainder, original;
                    
            printf("Enter an integer: ");
            scanf("%d", &n);
             original = n;
                
            // reversed integer is stored in reversed variable
            while (n != 0) {
                 remainder = n % 10;
                reversed = reversed * 10 + remainder;
                 n /= 10;
                 }
                
            // palindrome if orignal and reversed are equal
             if (original == reversed)
                     printf("%d is a palindrome.", original);
             else
                     printf("%d is not a palindrome.", original);
                
         return 0;
     }
                       
    
     
    
  13. Prime number or Not

    A prime number is a positive integer that is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13, 17.

    
    #include <stdio.h>
    int main() {
    int n, i, flag = 0;
                 
         printf("Enter a positive integer: ");
         scanf("%d", &n);
                
         for (i = 2; i <= n / 2; ++i) {
         // condition for non-prime
         if (n % i == 0) {
            flag = 1;
             break;
            }
         }
                
         if (n == 1) {
        printf("1 is neither prime nor composite.");
                  } 
         else {
             if (flag == 0)
                 printf("%d is a prime number.", n);
             else
                 printf("%d is not a prime number.", n);
              }
                
        return 0;
     }
                       
    
     
    
  14. Armstrong number or Not

    Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let's try to understand why 153 is an Armstrong number.

    
    #include <stdio.h>
    int main() {
    int num, originalNum, remainder, result = 0;
               
        printf("Enter a three-digit integer: ");
        scanf("%d", &num);
        originalNum = num;
            
        while (originalNum != 0) {
        // remainder contains the last digit
         remainder = originalNum % 10;
                    
        result += remainder * remainder * remainder;
                    
        // removing last digit from the orignal number
        originalNum /= 10;
        }
            
        if (result == num)
            printf("%d is an Armstrong number.", num);
        else
             printf("%d is not an Armstrong number.", num);
            
        return 0;
     }