Tag: c basic programs

  • C Program to Find the Largest of three Numbers

    In this tutorial, we will write a basic C Program to find the greatest of three numbers. You may go through the following topics in order to understand the C program.

    Algorithm to find the largest among three numbers

    1.  Start.
    2. Read num1, num2 and num3 from user.
    3. Check num > num2, if true go to step 4 else go to step 5
    4. Check num1 > num3.
      • If true, print ‘num1 is the greatest number’.
      • If false, then print ‘num3 as the greatest number’.
      • go to step 6
    5. check if num2 > num3.
      1. If true, print ‘num2 as the greatest number’.
      2. If false, print ‘num3 as the greatest number’.
    6. Stop

    C Program to find the Largest among three Numbers

    We will find the greatest of three using if..else and if…else-if statement.

    //C program to find the biggest of three numbers using if else if statement
    
    #include <stdio.h>
    int main()
    {
        int num1, num2, num3;
        
        printf("Enter three numbers to compare.\n");
        
        printf("First Number: ");
        scanf("%d", &num1);
        printf("Second Number: ");
        scanf("%d", &num2);
        printf("Third Number: ");
        scanf("%d", &num3);
        
        if (num1 > num2)
        {
            if (num1 > num3)
            {
                printf("\nLargest number: %d \n",num1);
            }
            else
            {
                printf("\nLargest number: %d \n",num3);
            }
        }
        else if (num2 > num3)
        {
            printf("\nLargest number: %d \n",num2);
        }
        else
        {
            printf("\nLargest number: %d \n",num3);
        }
        return 0;
    }

    Output:

    Enter three numbers to compare.
    First Number: 32
    Second Number: 65
    Third Number: 19

    Largest number: 65


    Using Ternary operator

    The ternary operator works like an if..else statement in C. Syntax of the ternary operator. (?:)

    variable num1 = (expression) ? value if true : value if false

    //Using ternary Operator
    
    #include <stdio.h>
    int main()
    {
        int num1, num2, num3;
        
        printf("Enter three numbers to compare.\n");
        
        printf("First Number: ");
        scanf("%d", &num1);
        printf("First Number: ");
        scanf("%d", &num2);
        printf("First Number: ");
        scanf("%d", &num3);
        
        int greatestNum = ((num1>num2 && num1>num3) ? num1 : (num2>num3)?num2:num3);
        printf("\nLargest of three is: %d.", greatestNum);
        
        return 0;
    }

    Output:

    Enter three numbers to compare.
    First Number: 45
    Second Number: 98
    Third Number: 66

    Largest of three is: 98


  • C Program to check whether a year is a Leap year or not

    This is the tutorial on the C program to check Leap year. In order to understand the program better, you need to have knowledge of the following topics in C.

    A leap year comes after every 4 years and has 366 days that year instead of 365 days. In the leap year, an additional day is added to the February month has 29 days instead of 28 days.

    Now let us understand through mathematical logic,

    • If a year is divisible by 4 then it is leap year.
    • If a year is divisible by 400 and not divisible by 100 then it is also a leap year.
    • Example: 2000, 2004, 2008, etc are the leap years.

    C Program to Check whether a year is a leap year or not

    #include<stdio.h>  
    #include<conio.h> 
    
    void main() 
    {  
        int year;  
        
        printf("Enter a year: ");  
        scanf("%d", &year);  
        
        //checking all the condition
        if((year%4 == 0 && year%100 != 0 ) || (year%400 == 0)) 
        {  
            printf("%d is a LEAP YEAR", year);  
        } 
        else 
        {  
            printf("%d is NOT a LEAP YEAR", year);  
        }  
        
        getch();  
    }  

    Output:

    //RUN 1
    Enter a year: 1996
    1996 is a LEAP YEAR

    //RUN 2
    Enter a year: 2005
    2005 is NOT a LEAP YEAR


  • C Program to find Cosine value

    In this tutorial, we will write a C Program to find Cosine series. Let us first by understanding what is cosine series.

    Cosine Series:

    It is a series used to find the value of cos(x), where x is the angle in degree which is converted to Radian.

    Formula:

    cosine formula

    For Cos(x) series:

    Cos(x) = 1 – (x*2 / 2!) + (x*4 / 4!) – (x*6 / 6!)……

    Let us understand through a C program to calculate the value of cos(x).

    Example

    Input-:
    x = 4, n = 3
    Output-:
    0.997563

    Input-:
    x = 8, n = 2
    Output-:
    0.990266


    C Program to find Cosine series

    #include <stdio.h>
    
    const double PI = 3.142;
    
    int main()
    {
       float x;
       int n;
       float result = 1;
       float s = 1, fact = 1, p = 1;
    
       printf("Enter x value: ");
       scanf("%f", &x);
    
       printf("Enter n value: ");
       scanf("%d", &n);
    
       //Converting degrees to radians
       x = x *(PI / 180.0);
    
       for (int i = 1; i < n; i++)
       {
          s = s *-1;
          fact = fact *(2 *i - 1) *(2 *i);
          p = p *x * x;
          result = result + s *p / fact;
       }
    
       printf("Result: %lf\n", result);
       return 0;
    }

    Output:

    Enter x value: 10
    Enter n value: 3
    Result: 0.984804


  • C Program to Find the Size of int, float, double and char

    In this tutorial, we will write a program to display the size of every data type using sizeof operator in C. Before that, you may go through the following topics in c as it is used in the program.


    sizeof() operator in C

    The sizeof operator in C is commonly used to determine the size of an expression or data type. It returns the size of the memory allocated to that data type.

    C Program to find the size of data types

    #include<stdio.h>
    
    int main() 
    {
        int intVar;
        float floatVar;
        double doubleVar;
        char charVar;
    
        //size of each variables
        printf("Size of the Variables in bytes:\n");
        printf("int: %zu\n", sizeof(intVar));
        printf("float: %zu\n", sizeof(floatVar));
        printf("double: %zu\n", sizeof(doubleVar));
        printf("char: %zu\n", sizeof(charVar));
        
        return 0;
    }

    Output:

    Size of the Variables in bytes:
    int: 4
    float: 4
    double: 8
    char: 1


  • C Program to check whether a Date is Valid or not

    In this tutorial, we will write a C program to check date is valid or not. Given the date, month and year, the program finds whether it is valid or not. Although before jumping to the main program, you should have knowledge of the following topics in C programming.

    The valid Range for a date, month, and year is 1/1/1800 – 31/12/9999.

    Explanation- What does the following program check for:

    • For Date: The date cannot be less than 1 and more than 31.
    • For Month: Month cannot be less than 1 and more than 12
    • For year: The maximum and minimum year is taken before the main program that is less than 1800 and more than 9999.
    • Leap Year: Checks if the year is a leap year or not.
      • If it is a leap year, the February month will have 29 days.
      • If it is not a leap year, the February month will have 28 days.
    • For 30 days month: The months April, June, September, and November cannot have more than 30 days

    C Program Date is Valid or Not

    #include <stdio.h>
    
    //take constant value for min and max year
    #define max_yr 9999
    #define min_yr 1800
    
    /*function to check the leap year
    if yes returns 1 else 0*/
    int isleapYear(int y)
    {
      if ((y % 4 == 0) && (y % 100 != 0) && (y % 400 == 0))
        return 1;
      else
        return 0;
    }
    
    //separate function to valid date or not
    int isDateValid(int d, int m, int y)
    {
      if (y < min_yr || y > max_yr)
        return 0;
      if (m < 1 || m > 12)
        return 0;
      if (d < 1 || d > 31)
        return 0;
    
      /*for february month
      if leap year feb has 29 days else 28 days*/
      if (m == 2)
      {
        if (isleapYear(y))
        {
          if (d <= 29)
            return 1;
          else
            return 0;
        }
      }
    
      //for 30 days months (April, June, September and November)
      if (m == 4 || m == 6 || m == 9 || m == 11)
        if (d <= 30)
          return 1;
        else
          return 0;
      return 1;
    }
    
    //main function 
    int main(int argc, char
      const *argv[])
    {
      int d, m, y;
    
      printf("Enter date (DD/MM/YYYY format): ");
      scanf("%d/%d/%d", &d, &m, &y);
    
      if (isDateValid(d, m, y))
        printf("Date is valid.");
      else
        printf("Date is not valid.");
      return 0;
    }

    Output:

    Enter date (DD/MM/YYYY format): 13/06/1998
    Date is valid.


  • C Program to Calculate the Area and Circumference of a Circle

    Question:
    Write a C Program to find the area and circumference of a circle.

    The program simply calculates the area and circumference of a circle using its formula. Although the value PI is already defined with preprocessor directives. It takes that value if PI is used anywhere in the program.

    You can check out the following if you do not know about the operators as this program uses operators.


    C Program to Calculate the Area and Circumference of a Circle

    Source Code:

    //Find the area and circumference of a circle
    
    #include <stdio.h>
    #include <conio.h>
    
    //Defining the pi value
    #define PI 3.142857
    
    void main()
    {
      int r;
      float area, circum;
    
      printf("Enter the radius of a circle: ");
      scanf("%d", &r);
    
     //calculate the area
      area = PI * r * r;
      printf("Area of a circle: %f ", area);
    
     //calculate the circumference
      circum = 2 *PI * r;
      printf("\n Circumference of a circle: %f ", circum);
    
      getch();
    }

    The output of calculating the area and circumference of a circle in C.

    Enter the radius of a circle: 5
    Area of a circle: 78.571426
    Circumference of a circle: 31.428570