Blog

  • C Program to Swap Two Numbers

    In this tutorial, we will learn how to swap two numbers in C program. Before that, if you may go through the following topics in C that are used in this program.


    C Program to Swap Two Numbers using temporary variable

    The program takes two number inputs from the user that needed to be swapped. And using a third variable, we swap the value. The third variable used is named temp in the program.

    #include<stdio.h>
    
    int main()
    {
        int a, b, temp;
        
        printf("Enter a: ");
        scanf("%d", &a);
        
        printf("Enter b: ");
        scanf("%d", &b);
        
        temp = a;
        a = b;
        b = temp;
        
        printf("The swapped values a: %d and b: %d", a, b);
        
        return 0;
    }

    Output:

    Enter a: 10
    Enter b: 20
    The swapped values a: 20 and b: 10

    You may want to check the following swapping program in C.


  • C Program to Swap Two Numbers without using Third Variable

    In this tutorial, we will write a C program to swap two numbers without using a temporary variable. There are two ways to swap variables.

    1. Using + and
    2. Using * and /

    Let us go through each of them with a C program. You may check out the following program.


    C Program to Swap Two Numbers without using Third Variable

    1. Using + and –

    #include<stdio.h>
    
    int main()
    {
        int a, b;
        printf("Enter a: ");
        scanf("%d", &a);
        
        printf("Enter b: ");
        scanf("%d", &b);
        
        
        a = a + b;  //a = 10 + 20 = 30
        b = a - b;  //b = 30 - 20 = 10
        a = a - b;  //a = 30 - 10 = 20
        
        printf("The swapped values a: %d and b: %d", a, b);
        
        return 0;
    }
    

    Output:

    Enter a: 10
    Enter b: 20
    The swapped values a: 20 and b: 10


    2. Using * and /

    #include<stdio.h>
    
    int main()
    {
        int a, b;
        printf("Enter a: ");
        scanf("%d", &a);
        
        printf("Enter b: ");
        scanf("%d", &b);
        
        a = a * b; //a = 10 * 20 = 200
        b = a / b; //b = 200 / 20 = 10
        a = a / b; //a = 200 / 10 = 20
        
        printf("The swapped values a: %d and b: %d", a, b);
        
        return 0;
    }
    

    Output:

    Enter a: 10
    Enter b: 20
    The swapped values a: 20 and b: 10

    Note that the swapping using / and * will not work if one of the swapping values is zero (0).


  • C Program to Convert Fahrenheit to Celsius

    In this tutorial, we will write a C Program to convert Fahrenheit into Celsius. Let us start by understanding the formula for the conversion of Fahrenheit into Celsius.

    Fahrenheit and Celsius, both are the unit for measuring the temperature. Fahrenheit is represented by oF and Celsius by oC.

    Formula to convert Fahrenheit into Celsius

    celsius = (fahrenheit – 32)*5/9


    C ProgrC Program to Convert Temperature from Fahrenheit to Celsius

    #include<stdio.h>
     
    void main()
    {
        float c,f;
     
        //user input
        printf("Enter the Fahrenheit value: ");
        scanf("%f",&f);
     
        //conversion
        c = (f - 32) * 5 / 9;
        
        //Display the result
        printf("Celsius: %.3f", c); 
    }

    Output:

    Enter the Fahrenheit value: 64
    Celsius: 17.778


  • Infix to Postfix Conversion in C Program using Stack

    Stack is very useful for storing data in the manner of the stack. It s useful when comes to the conversion of arithmetic expressions in high-level programming languages into machine-readable form. There are some complicated arithmetic expressions such as (A+B)*C(D/(E+D)). This form can be converted into polish notation using stack.

    Infix and Postfix Expression

    Infix expression contains the operator at the middle of the operands such as A+B. Whereas Postfix expression the operator goes to the end of the expression such as AB+.

    Let us go through an example in the C program.


    Infix to Postfix Conversion in C Program using Stack

    #include<stdio.h>
    #include<ctype.h>
    
    char stack[100];
    int top = -1;
    
    void push(char x)
    {
        stack[++top] = x;
    }
    
    char pop()
    {
        if(top == -1)
            return -1;
        else
            return stack[top--];
    }
    
    int priority(char x)
    {
        if(x == '(')
            return 0;
        if(x == '+' || x == '-')
            return 1;
        if(x == '*' || x == '/')
            return 2;
        return 0;
    }
    
    int main()
    {
        char exp[100];
        char *e, x;
        printf("Enter Infix expression : ");
        scanf("%s",exp);
        
        e = exp;
        
        while(*e != '\0')
        {
            if(isalnum(*e))
                printf("%c ",*e);
            else if(*e == '(')
                push(*e);
            else if(*e == ')')
            {
                while((x = pop()) != '(')
                    printf("%c ", x);
            }
            else
            {
                while(priority(stack[top]) >= priority(*e))
                    printf("%c ",pop());
                push(*e);
            }
            e++;
        }
        
        while(top != -1)
        {
            printf("%c ",pop());
        }return 0;
    }

    Output: Run 1

    infix to postfix output

    Output: Run 2

    infix to postfix output

  • Fizz Buzz Program in C

    In this tutorial, we will learn about Fizz Buzz Implementation in C programming. Let us start by understanding what Fizz Buzz is and its implementation in a program.

    Fizz Buzz Program

    A Fizz Buzz program prints the number from the range of 1 to n, where n is the input number taken from the user.

    • The program prints “FizzBuzz” if it is a multiple of 3 and 5.
    • The program prints “Fizz” if it is a multiple of only 3 but not 5.
    • The program prints “Buzz” is it is a multiple pf only 5 but not 3.
    • Lastly, it prints the number itself, if it is none of the above.

    Example:

    Input (n): 10
    Output: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz

    Now let us go through an example in the C program to implement FizzBuzz.


    Fizz Buzz Program in C

    //FizzBuzz program in C
    #include <stdio.h>
    
    int main()
    {
       int i, n;
    
       printf("Enter the number: ");
       scanf("%d", &n);
    
       for (i = 1; i <= n; i++)
       {
          // For FizzBuzz (3 *5 =15)
          if (i % 15 == 0)
             printf("FizzBuzz\t");
    
          // For Fizz (3)
          else if ((i % 3) == 0)
             printf("Fizz\t");
    
          // For Buzz (5)
          else if ((i % 5) == 0)
             printf("Buzz\t");
    
          else	//if none of the above print n
             printf("%d\t", i);
       }
    
       return 0;
    }

    Output:

    Enter the number: 20
    1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz


  • C Program to Find Length of a String Using strlen() Function

    In this section, we will write a C program to find the length of a string using strlen() function. But before, you may go through the following topic in C programming.

    The program will take the user input for the string. To find the length of a string, we use the built-in string function called strlen(). This function is present in the string.h header file.


    C Program to Find Length of a String Using strlen() Function

    // C program to find length of the string using strlen() function
    #include <stdio.h>
    
    int main()
    {
       char str[30];
       int length;
    
       printf("Enter a string: ");
       scanf("%s", str);
    
       length = strlen(str);
    
       printf("Length of %s is %d", str, length);
       return 0;
    }

    Output:

    Enter a string: simple2code
    Length of simple2code is 11

    You can also check the following:


  • C Program to Find the Length of a String without using Function strlen()

    In this section, we will write a C program to find the length of a string without using strlen() function. But before, you may go through the following topic in C programming.

    The program will take the user input for the string and iterate the string. It will count the number of characters present in the string.


    C Program to Find the Length of a String without using Function strlen()

    // C program to find length of the string without using strlen() function
    #include <stdio.h>
    
    int main()
    {
       char str[30];
       int i;
    
       printf("Enter a string: ");
       scanf("%s", str);
    
       for (i = 0; str[i] != '\0'; ++i);
    
       printf("The length of %s is %d", str, i);
       return 0;
    }

    Output:

    Enter a string: simple2code
    The length of simple2code is 11

    You can also check the following:


  • C Program to Find Second Largest Number in an Array

    The following C program finds the second largest element present in an array. The program iterates through an array and compares each element in an array.

    For example:

    Input: arr[] = {5, 85, 19, 6, 99, 45}
    Output: The second largest element is 85.


    C Program to Find Second Largest Number in an Array

    //C Program to find Second largest Number in an Array
    
    #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
       int arr[30], i, size, largest, second;
    
       printf("Enter the size of an array: \n");
       scanf("%d", &size);
    
       printf("Enter %d elements of an Array:\n", size);
    
       for (i = 0; i < size; i++)
       {
          scanf("%d", &arr[i]);
       }
    
       // largest and second largest initialized with minimum possible value
       largest = second = INT_MIN;
    
       //iterate through the array
       for (i = 0; i < size; i++)
       {
          //if arr element is greater than largest then
          //swap the value with largest and second largest with second.
          if (arr[i] > largest)
          {
             second = largest;
             largest = arr[i];
          }
          else if (arr[i] > second && arr[i] < largest)
          {
             second = arr[i];
          }
       }
    
       printf("Largest element: %d", largest);
       printf("\nSecond Largest element: %d", second);
    
       return 0;
    }

    Output:

    Enter the size of an array:
    5
    Enter 5 elements of an Array:
    12
    56
    8
    19
    88
    Largest element: 88
    Second Largest element: 56

    Go through more C programming with various topics:


  • C Program to Check Whether a Given Number is Prime or Not

    In this C programming example, we will learn about the Prime Number program in C. We specifically write a program to check the entered number is prime or not.

    Prime Number:

    A Prime Number is a number that is only divisible by 1 and itself. Example: 2, 3, 5, 7, 11, 13, 17, etc. These numbers are only divisible by 1 and the number itself.

    Note:
    0 and 1 is not a prime number and 2 is the only even and smallest prime number.

    You must know the following topics in C first:


    C Program to Check Whether a Given Number is Prime or Not

    #include <stdio.h>
    
    int main()
    {
      int num, i, flag = 0;
    
      //user input
      printf("Enter the number: ");
      scanf("%d", &num);
    
      for (i = 2; i <= num / 2; ++i)
      {
        // checking for non-prime
        if (num % i == 0)
        {
          flag = 1;
          break;
        }
      }
    
      //checkinh for the entered number is 1 or not
      //because 1 cannot be a prime number
      if (num == 1)
      {
        printf("1 is not a prime number.");
      }
      else
      {
        if (flag == 0)
          printf("%d is a prime number.", num);
        else
          printf("%d is not a prime number.", num);
      }
    
      return 0;
    }

    Output:

    //Run 1
    Enter the number: 13
    13 is a prime number.

    //Run2
    Enter the number: 1
    1 is not a prime number.

    You may go through the following C program on the prime number.


  • .Net Framework

    .Net is a software development platform developed by Microsoft. It was meant to create applications, which would run on a windows platform. The first beta version was released in 2000.

    Although C# is a computer language that can be studied on its own, it has a special relationship to its runtime environment, the .NET Framework.

    The .NET Framework defines an environment that supports the development and execution of highly distributed, component-based applications. It enables differing computer languages to work together and provides for security, program portability, and a common programming model for the Windows platform.

    The .Net Framework supports more than 60 programming languages such as C#, F#, VB.NET, J#, VC++, JScript.NET, APL, COBOL, Perl, Oberon, etc.

    The following are the .NET Framework important entities.

    • Common Language Runtime (CLR)
    • Framework Class Library (FCL)
    • Core Languages (WinForms, ASP.NET, and ADO.NET)
    .net framework

    Common Language Runtime (CLR)

    The Common Language Runtime (CLR) is the execution engine of the .NET Framework that loads and manages the execution of your program.

    All .NET programs execute under the supervision of the CLR, guaranteeing certain properties and behaviors in the areas of memory management, provide security, Platform independent, garbage collection, and exception handling. It acts as an interface between the framework and the operating system.


    Framework Class Library (FCL)

    This library gives your program access to the runtime environment. It includes some set of standard class libraries that contains several properties and methods used for the framework purposes.

    The Base Class Library (BCL) is the core of FCL and provides basic functionalities.


    Core Languages

    Following are the types of applications built in the .Net framework:

    WinForms: It stands for windows form and is used to create form-based applications. It runs on the client end and notepad is an example of WinForms.

    ASP.Net: It is a web framework, developed by Microsoft and is used to develop web-based applications. This framework is made to run on any web browser such as Internet Explorer, Chrome, etc. It was released in the year 2002.

    ADO.Net: This module of the .Net framework was developed to form a connection between application and data sources (Databases). Examples of such modules are Oracle or Microsoft SQL Server. There are several classes present in ADO.Net that are used for the connection of database, insertion, deletion of data, etc.